You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessTokenHandle.cs 1.3 KiB

8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. // <copyright file="AccessTokenHandle.cs" company="Nick Lowe">
  2. // Copyright © Nick Lowe 2009
  3. // </copyright>
  4. // <author>Nick Lowe</author>
  5. // <email>nick@int-r.net</email>
  6. // <url>http://processprivileges.codeplex.com/</url>
  7. namespace ProcessPrivileges
  8. {
  9. using System.ComponentModel;
  10. using System.Runtime.ConstrainedExecution;
  11. using System.Runtime.InteropServices;
  12. using Microsoft.Win32.SafeHandles;
  13. /// <summary>Handle to an access token.</summary>
  14. public sealed class AccessTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
  15. {
  16. internal AccessTokenHandle(ProcessHandle processHandle, TokenAccessRights tokenAccessRights)
  17. : base(true)
  18. {
  19. if (!NativeMethods.OpenProcessToken(processHandle, tokenAccessRights, ref handle))
  20. {
  21. throw new Win32Exception(Marshal.GetLastWin32Error());
  22. }
  23. }
  24. /// <summary>Releases the handle.</summary>
  25. /// <returns>Value indicating if the handle released successfully.</returns>
  26. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  27. protected override bool ReleaseHandle()
  28. {
  29. if (!NativeMethods.CloseHandle(handle))
  30. {
  31. throw new Win32Exception(Marshal.GetLastWin32Error());
  32. }
  33. return true;
  34. }
  35. }
  36. }