Wraps a managed object holding a handle to a resource that is passed to unmanaged code using platform invoke.
Assembly: mscorlib (in mscorlib.dll)
Initializes a new instance of theclass with the object to wrap and a handle to the resource used by unmanaged code.
Indicates whether this instance and a specified object are equal.(Inherited from ValueType.)
Returns the hash code for this instance.(Inherited from ValueType.)
Returns the internal integer representation of aobject.
Returns the fully qualified type name of this instance.(Inherited from ValueType.)
|Explicit(HandleRef to IntPtr)|
Returns the handle to a resource of the specifiedobject.
If you use platform invoke to call a managed object, and the object is not referenced elsewhere after the platform invoke call, it is possible for the garbage collector to finalize the managed object. This action releases the resource and invalidates the handle, causing the platform invoke call to fail. Wrapping a handle with Consuming Unmanaged DLL Functions.guarantees that the managed object is not garbage collected until the platform invoke call completes. For a description of platform invoke services, see
The GCHandle, is a special type recognized by the interop marshaler. A normal, nonpinned GCHandle also prevents untimely garbage collection, yet provides better performance. Although using to keep an object alive for the duration of a platform invoke call is preferred, you can also use the GC.KeepAlive method for the same purpose.value type, like
The Object representing the wrapper, and an IntPtr representing the unmanaged handle. The interop marshaler passes only the handle to unmanaged code, and guarantees that the wrapper (passed as the first parameter to the constructor of the HandleRef) remains alive for the duration of the call.constructor takes two parameters: an
Available since 1.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.