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

  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:  SafeFileMapViewHandle
  18. **
  19. **                                                   
  20. **
  21. ** A wrapper for handles returned from MapViewOfFile, used
  22. ** for shared memory.
  23. **
  24. ** Date:  August 7, 2002
  25. **
  26. ===========================================================*/
  27. using System;
  28. using System.Security;
  29. using System.Security.Permissions;
  30. using System.Runtime.InteropServices;
  31. using System.Runtime.CompilerServices;
  32. using Microsoft.Win32;
  33. using Microsoft.Win32.SafeHandles;
  34. using System.Runtime.ConstrainedExecution;
  35. namespace Microsoft.Win32.SafeHandles
  36. {
  37.     [HostProtectionAttribute(MayLeakOnAbort = true)]
  38.     [SuppressUnmanagedCodeSecurityAttribute()]
  39.     internal sealed class SafeFileMapViewHandle : SafeHandleZeroOrMinusOneIsInvalid
  40.     {
  41.         // Note that MapViewOfFile returns 0 on failure
  42.        
  43.         internal SafeFileMapViewHandle() : base(true)
  44.         {
  45.         }
  46.        
  47.         [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto)]
  48.         static internal extern SafeFileMapViewHandle MapViewOfFile(SafeFileMappingHandle hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
  49.        
  50.         [DllImport(ExternDll.Kernel32, ExactSpelling = true, SetLastError = true)]
  51.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  52.         private static extern bool UnmapViewOfFile(IntPtr handle);
  53.        
  54.         protected override bool ReleaseHandle()
  55.         {
  56.             return UnmapViewOfFile(handle);
  57.         }
  58.        
  59.     }
  60. }

Developer Fusion