The Labs \ Source Viewer \ SSCLI \ Microsoft.Win32.SafeHandles \ SafeUserTokenHandle

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  SafeUserTokenHandle
  18. **
  19. **                                                 
  20. **
  21. ** A wrapper for a user token handle
  22. **
  23. ** Date:  July 8, 2002
  24. **
  25. ===========================================================*/
  26. using System;
  27. using System.Security;
  28. using System.Security.Permissions;
  29. using System.Runtime.InteropServices;
  30. using System.Runtime.CompilerServices;
  31. using Microsoft.Win32;
  32. using Microsoft.Win32.SafeHandles;
  33. using System.Runtime.ConstrainedExecution;
  34. namespace Microsoft.Win32.SafeHandles
  35. {
  36.     [HostProtectionAttribute(MayLeakOnAbort = true)]
  37.     [SuppressUnmanagedCodeSecurityAttribute()]
  38.     internal sealed class SafeUserTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
  39.     {
  40.         // Note that OpenProcess returns 0 on failure.
  41.         internal SafeUserTokenHandle() : base(true)
  42.         {
  43.         }
  44.        
  45.         internal SafeUserTokenHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
  46.         {
  47.             SetHandle(existingHandle);
  48.         }
  49.        
  50.        
  51.         [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  52.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  53.         private static extern bool CloseHandle(IntPtr handle);
  54.        
  55.         protected override bool ReleaseHandle()
  56.         {
  57.             return CloseHandle(handle);
  58.         }
  59.     }
  60. }

Developer Fusion