mscorlib(4.0.0.0) API with additions
SafeHandle.cs
3 using System.Security;
5 
7 {
9  [SecurityCritical]
10  [__DynamicallyInvokable]
11  [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
13  {
15  protected IntPtr handle;
16 
17  private int _state;
18 
19  private bool _ownsHandle;
20 
21  private bool _fullyInitialized;
22 
26  [__DynamicallyInvokable]
27  public bool IsClosed
28  {
29  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
30  [__DynamicallyInvokable]
31  get
32  {
33  return (_state & 1) == 1;
34  }
35  }
36 
40  [__DynamicallyInvokable]
41  public abstract bool IsInvalid
42  {
43  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
44  [__DynamicallyInvokable]
45  get;
46  }
47 
53  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
54  protected SafeHandle(IntPtr invalidHandleValue, bool ownsHandle)
55  {
56  handle = invalidHandleValue;
57  _state = 4;
58  _ownsHandle = ownsHandle;
59  if (!ownsHandle)
60  {
61  GC.SuppressFinalize(this);
62  }
63  _fullyInitialized = true;
64  }
65 
67  [SecuritySafeCritical]
68  [__DynamicallyInvokable]
69  ~SafeHandle()
70  {
71  Dispose(disposing: false);
72  }
73 
74  [MethodImpl(MethodImplOptions.InternalCall)]
75  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
76  private extern void InternalFinalize();
77 
80  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
81  protected void SetHandle(IntPtr handle)
82  {
83  this.handle = handle;
84  }
85 
88  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
90  {
91  return handle;
92  }
93 
95  [SecurityCritical]
96  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
97  public void Close()
98  {
99  Dispose(disposing: true);
100  }
101 
103  [SecuritySafeCritical]
104  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
105  [__DynamicallyInvokable]
106  public void Dispose()
107  {
108  Dispose(disposing: true);
109  }
110 
114  [SecurityCritical]
115  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
116  [__DynamicallyInvokable]
117  protected virtual void Dispose(bool disposing)
118  {
119  if (disposing)
120  {
121  InternalDispose();
122  }
123  else
124  {
125  InternalFinalize();
126  }
127  }
128 
129  [MethodImpl(MethodImplOptions.InternalCall)]
130  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
131  private extern void InternalDispose();
132 
134  [MethodImpl(MethodImplOptions.InternalCall)]
135  [SecurityCritical]
136  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
137  [__DynamicallyInvokable]
138  public extern void SetHandleAsInvalid();
139 
143  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
144  [__DynamicallyInvokable]
145  protected abstract bool ReleaseHandle();
146 
150  [MethodImpl(MethodImplOptions.InternalCall)]
151  [SecurityCritical]
152  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
153  [__DynamicallyInvokable]
154  public extern void DangerousAddRef(ref bool success);
155 
157  [MethodImpl(MethodImplOptions.InternalCall)]
158  [SecurityCritical]
159  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
160  [__DynamicallyInvokable]
161  public extern void DangerousRelease();
162  }
163 }
Describes a set of security permissions applied to code. This class cannot be inherited.
SafeHandle(IntPtr invalidHandleValue, bool ownsHandle)
Initializes a new instance of the T:System.Runtime.InteropServices.SafeHandle class with the specifie...
Definition: SafeHandle.cs:54
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Ensures that all finalization code in derived classes is marked as critical.
Definition: __Canon.cs:3
void SetHandle(IntPtr handle)
Sets the handle to the specified pre-existing handle.
Definition: SafeHandle.cs:81
Represents a wrapper class for operating system handles. This class must be inherited.
Definition: SafeHandle.cs:12
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
void SetHandleAsInvalid()
Marks a handle as no longer used.
void DangerousRelease()
Manually decrements the reference counter on a T:System.Runtime.InteropServices.SafeHandle instance.
SecurityAction
Specifies the security actions that can be performed using declarative security.
abstract bool ReleaseHandle()
When overridden in a derived class, executes the code required to free the handle.
void DangerousAddRef(ref bool success)
Manually increments the reference counter on T:System.Runtime.InteropServices.SafeHandle instances.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
MethodImplOptions
Defines the details of how a method is implemented.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
IntPtr handle
Specifies the handle to be wrapped.
Definition: SafeHandle.cs:15
abstract bool IsInvalid
When overridden in a derived class, gets a value indicating whether the handle value is invalid.
Definition: SafeHandle.cs:42
bool IsClosed
Gets a value indicating whether the handle is closed.
Definition: SafeHandle.cs:28
IntPtr DangerousGetHandle()
Returns the value of the F:System.Runtime.InteropServices.SafeHandle.handle field.
Definition: SafeHandle.cs:89
void Dispose()
Releases all resources used by the T:System.Runtime.InteropServices.SafeHandle class.
Definition: SafeHandle.cs:106
void Close()
Marks the handle for releasing and freeing resources.
Definition: SafeHandle.cs:97
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Runtime.InteropServices.SafeHandle class specif...
Definition: SafeHandle.cs:117