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

  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:  SafeLocalMemHandle
  18. **
  19. **                                                 
  20. **
  21. ** A wrapper for handle to local memory
  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 SafeLocalMemHandle : SafeHandleZeroOrMinusOneIsInvalid
  39.     {
  40.         internal SafeLocalMemHandle() : base(true)
  41.         {
  42.         }
  43.        
  44.         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  45.         internal SafeLocalMemHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
  46.         {
  47.             SetHandle(existingHandle);
  48.         }
  49.        
  50.        
  51.         [DllImport(ExternDll.Kernel32)]
  52.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  53.         private static extern IntPtr LocalFree(IntPtr hMem);
  54.        
  55.         protected override bool ReleaseHandle()
  56.         {
  57.             return LocalFree(handle) == IntPtr.Zero;
  58.         }
  59.        
  60.     }
  61. }

Developer Fusion