3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // Abstract derivations of SafeHandle designed to provide the common
8 // functionality supporting Win32 handles. More specifically, they describe how
9 // an invalid handle looks (for instance, some handles use -1 as an invalid
10 // handle value, others use 0).
12 // Further derivations of these classes can specialise this even further (e.g.
13 // file or registry handles).
17 namespace Microsoft.Win32.SafeHandles
20 using System.Runtime.InteropServices;
21 using System.Runtime.CompilerServices;
22 using System.Security.Permissions;
23 using System.Runtime.ConstrainedExecution;
25 // Class of safe handle which uses 0 or -1 as an invalid handle.
26 [System.Security.SecurityCritical] // auto-generated_required
28 [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
30 public abstract class SafeHandleZeroOrMinusOneIsInvalid : SafeHandle
32 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
33 protected SafeHandleZeroOrMinusOneIsInvalid(bool ownsHandle) : base(IntPtr.Zero, ownsHandle)
38 // A default constructor is needed to satisfy CoreCLR inheritence rules. It should not be called at runtime
39 protected SafeHandleZeroOrMinusOneIsInvalid()
41 throw new NotImplementedException();
43 #endif // FEATURE_CORECLR
45 public override bool IsInvalid {
46 [System.Security.SecurityCritical]
47 get { return handle.IsNull() || handle == new IntPtr(-1); }
51 // Class of safe handle which uses only -1 as an invalid handle.
52 [System.Security.SecurityCritical] // auto-generated_required
54 [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
56 public abstract class SafeHandleMinusOneIsInvalid : SafeHandle
58 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
59 protected SafeHandleMinusOneIsInvalid(bool ownsHandle) : base(new IntPtr(-1), ownsHandle)
64 // A default constructor is needed to satisfy CoreCLR inheritence rules. It should not be called at runtime
65 protected SafeHandleMinusOneIsInvalid()
67 throw new NotImplementedException();
69 #endif // FEATURE_CORECLR
71 public override bool IsInvalid {
72 [System.Security.SecurityCritical]
73 get { return handle == new IntPtr(-1); }
77 // Class of critical handle which uses 0 or -1 as an invalid handle.
78 [System.Security.SecurityCritical] // auto-generated_required
80 [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
82 public abstract class CriticalHandleZeroOrMinusOneIsInvalid : CriticalHandle
84 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
85 protected CriticalHandleZeroOrMinusOneIsInvalid() : base(IntPtr.Zero)
89 public override bool IsInvalid {
90 [System.Security.SecurityCritical]
91 get { return handle.IsNull() || handle == new IntPtr(-1); }
95 // Class of critical handle which uses only -1 as an invalid handle.
96 [System.Security.SecurityCritical] // auto-generated_required
98 [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
100 public abstract class CriticalHandleMinusOneIsInvalid : CriticalHandle
102 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
103 protected CriticalHandleMinusOneIsInvalid() : base(new IntPtr(-1))
107 public override bool IsInvalid {
108 [System.Security.SecurityCritical]
109 get { return handle == new IntPtr(-1); }