HandleRef HandleRef HandleRef HandleRef Struct

定義

將保有控制代碼的 Managed 物件包裝至要使用平台叫用傳遞至 Unmanaged 程式碼的資源。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.

如果您使用平台叫用來呼叫 managed 物件, 而且在平台叫用呼叫之後的其他地方未參考該物件, 則垃圾收集行程可能會完成受管理的物件。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. HandleRef包裝控制碼, 可保證在平台叫用呼叫完成之前, 不會對 managed 物件進行垃圾收集。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.

實值型別 ( GCHandle例如) 是 interop 封送處理器所識別的特殊類型。 HandleRefThe HandleRef value type, like GCHandle, is a special type recognized by the interop marshaler. 一般的 nonpinned GCHandle也會防止延誤時間垃圾收集, 但HandleRef卻提供較佳的效能。A normal, nonpinned GCHandle also prevents untimely garbage collection, yet HandleRef provides better performance. 雖然在HandleRef慣用平台叫用呼叫期間使用來保持物件運作, 但您也可以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. Interop 封送處理器只會將控制碼傳遞給非受控碼, 並保證在呼叫期間, 包裝函式 (當做第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)

使用要包裝的物件和 Unmanaged 程式碼所使用的資源控制代碼,初始化 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.

適用於

另請參閱