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

  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:  SafeViewOfFileHandle
  18. **
  19. **
  20. ** A wrapper for file handles
  21. **
  22. **
  23. ===========================================================*/
  24. using System;
  25. using System.Security;
  26. using System.Security.Permissions;
  27. using System.Runtime.InteropServices;
  28. using System.Runtime.CompilerServices;
  29. using System.Runtime.ConstrainedExecution;
  30. using System.Runtime.Versioning;
  31. using Microsoft.Win32;
  32. using Microsoft.Win32.SafeHandles;
  33. namespace Microsoft.Win32.SafeHandles
  34. {
  35.     internal sealed class SafeViewOfFileHandle : SafeHandleZeroOrMinusOneIsInvalid
  36.     {
  37.         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  38.         internal SafeViewOfFileHandle() : base(true)
  39.         {
  40.         }
  41.        
  42.         // 0 is an Invalid Handle
  43.         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  44.         internal SafeViewOfFileHandle(IntPtr handle, bool ownsHandle) : base(ownsHandle)
  45.         {
  46.             SetHandle(handle);
  47.         }
  48.        
  49.         [ResourceExposure(ResourceScope.Machine)]
  50.         [ResourceConsumption(ResourceScope.Machine)]
  51.         protected override bool ReleaseHandle()
  52.         {
  53.             if (Win32Native.UnmapViewOfFile(handle)) {
  54.                 handle = IntPtr.Zero;
  55.                 return true;
  56.             }
  57.            
  58.             return false;
  59.         }
  60.     }
  61. }

Developer Fusion