HandleRef HandleRef HandleRef HandleRef Struct

定义

将包含句柄的托管对象包装到通过平台调用传递到非托管代码的资源中。Wraps a managed object holding a handle to a resource that is passed to unmanaged code using platform invoke.

public value class HandleRef
[System.Runtime.InteropServices.ComVisible(true)]
public struct HandleRef
type HandleRef = struct
Public Structure HandleRef
继承
属性

注解

重要

从.NET Framework 2.0 的功能HandleRef已由类SafeHandle类和其派生的类,作为和CriticalHandle类。Starting with the .NET Framework 2.0, the functionality of the HandleRef class has been replaced by the SafeHandle class and its derived classes, as well as by the CriticalHandle class.

如果使用平台调用来调用托管的对象,并在平台 invoke 调用之后,不其他位置引用的对象,就可以为垃圾回收器来完成的托管的对象。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. 此操作将释放资源并使无效句柄,从而导致平台 invoke 调用失败。This action releases the resource and invalidates the handle, causing the platform invoke call to fail. 包装的句柄与HandleRef保证托管的对象不是垃圾回收,直至在平台 invoke 调用完成。Wrapping a handle with HandleRef guarantees that the managed object is not garbage collected until the platform invoke call completes. 有关说明的平台调用服务,请参阅使用非托管 DLL 函数For a description of platform invoke services, see Consuming Unmanaged DLL Functions.

HandleRef值类型,如GCHandle,是一种特殊类型可识别的互操作封送处理程序。The HandleRef value type, like GCHandle, is a special type recognized by the interop marshaler. 正常、 nonpinnedGCHandle还可以防止不及时垃圾回收,尚HandleRef提供更好的性能。A normal, nonpinned GCHandle also prevents untimely garbage collection, yet HandleRef provides better performance. 尽管使用HandleRef使对象保持活动状态的持续时间的平台 invoke 调用的是首选,还可以使用GC.KeepAlive实现同一目的的方法。Although using HandleRef 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.

HandleRef构造函数采用两个参数:Object表示在包装器和一个IntPtr表示非托管句柄。The HandleRef constructor takes two parameters: an Object representing the wrapper, and an IntPtr representing the unmanaged handle. 互操作封送处理程序将仅将句柄传递给非托管代码,并保证的包装器 (作为第一个参数传递给构造函数的HandleRef) 调用的持续时间内保持活动状态。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.

构造函数

HandleRef(Object, IntPtr) HandleRef(Object, IntPtr) HandleRef(Object, IntPtr) HandleRef(Object, IntPtr)

用要包装的对象和由非托管代码使用的资源的句柄初始化 HandleRef 类的新实例。Initializes a new instance of the HandleRef class with the object to wrap and a handle to the resource used by unmanaged code.

属性

Handle Handle Handle Handle

获取资源的句柄。Gets the handle to a resource.

Wrapper Wrapper Wrapper Wrapper

获取保存资源句柄的对象。Gets the object holding the handle to a resource.

方法

ToIntPtr(HandleRef) ToIntPtr(HandleRef) ToIntPtr(HandleRef) ToIntPtr(HandleRef)

返回 HandleRef 对象的内部整数表示形式。Returns the internal integer representation of a HandleRef object.

操作员

Explicit(HandleRef to IntPtr) Explicit(HandleRef to IntPtr) Explicit(HandleRef to IntPtr) Explicit(HandleRef to IntPtr)

返回指定的 HandleRef 对象的资源的句柄。Returns the handle to a resource of the specified HandleRef object.

适用于

另请参阅