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

  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. // Abstract derivations of SafeHandle designed to provide the common
  17. // functionality supporting Win32 handles. More specifically, they describe how
  18. // an invalid handle looks (for instance, some handles use -1 as an invalid
  19. // handle value, others use 0).
  20. //
  21. // Further derivations of these classes can specialise this even further (e.g.
  22. // file or registry handles).
  23. //
  24. //
  25. using System;
  26. using System.Runtime.InteropServices;
  27. using System.Runtime.CompilerServices;
  28. using System.Security.Permissions;
  29. using System.Runtime.ConstrainedExecution;
  30. namespace Microsoft.Win32.SafeHandles
  31. {
  32.     // Class of safe handle which uses 0 or -1 as an invalid handle.
  33.     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  34.     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  35.     public abstract class SafeHandleZeroOrMinusOneIsInvalid : SafeHandle
  36.     {
  37.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  38.         protected SafeHandleZeroOrMinusOneIsInvalid(bool ownsHandle) : base(IntPtr.Zero, ownsHandle)
  39.         {
  40.         }
  41.        
  42.         public override bool IsInvalid {
  43.             get { return handle.IsNull() || handle == new IntPtr(-1); }
  44.         }
  45.     }
  46.    
  47.     // Class of safe handle which uses only -1 as an invalid handle.
  48.     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  49.     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  50.     public abstract class SafeHandleMinusOneIsInvalid : SafeHandle
  51.     {
  52.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  53.         protected SafeHandleMinusOneIsInvalid(bool ownsHandle) : base(new IntPtr(-1), ownsHandle)
  54.         {
  55.         }
  56.        
  57.         public override bool IsInvalid {
  58.             get { return handle == new IntPtr(-1); }
  59.         }
  60.     }
  61.    
  62.     // Class of critical handle which uses 0 or -1 as an invalid handle.
  63.     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  64.     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  65.     public abstract class CriticalHandleZeroOrMinusOneIsInvalid : CriticalHandle
  66.     {
  67.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  68.         protected CriticalHandleZeroOrMinusOneIsInvalid() : base(IntPtr.Zero)
  69.         {
  70.         }
  71.        
  72.         public override bool IsInvalid {
  73.             get { return handle.IsNull() || handle == new IntPtr(-1); }
  74.         }
  75.     }
  76.    
  77.     // Class of critical handle which uses only -1 as an invalid handle.
  78.     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
  79.     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  80.     public abstract class CriticalHandleMinusOneIsInvalid : CriticalHandle
  81.     {
  82.         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  83.         protected CriticalHandleMinusOneIsInvalid() : base(new IntPtr(-1))
  84.         {
  85.         }
  86.        
  87.         public override bool IsInvalid {
  88.             get { return handle == new IntPtr(-1); }
  89.         }
  90.     }
  91.    
  92. }

Developer Fusion