SafeHandle.DangerousGetHandle 方法


傳回 handle 欄位的值。Returns the value of the handle field.

 IntPtr DangerousGetHandle();
public IntPtr DangerousGetHandle ();
member this.DangerousGetHandle : unit -> nativeint
Public Function DangerousGetHandle () As IntPtr



IntPtr,代表 handle 欄位的值。An IntPtr representing the value of the handle field. 如果這個控制代碼已經用 SetHandleAsInvalid() 標記為無效,則這個方法仍會傳回原始的控制代碼值,不過這個值可能為過時的值。If the handle has been marked invalid with SetHandleAsInvalid(), this method still returns the original handle value, which can be a stale value.


您可以使用這個方法,從衍生類別的實例中取出實際的控制碼值 SafeHandleYou can use this method to retrieve the actual handle value from an instance of the SafeHandle derived class. 這是回溯相容性所需的方法,因為 .NET Framework 中有許多屬性會傳回 IntPtr 控制碼類型。This method is needed for backwards compatibility because many properties in the .NET Framework return IntPtr handle types. IntPtr 控制碼類型是用來表示指標或控制碼的平臺特定類型。IntPtr handle types are platform-specific types used to represent a pointer or a handle.


使用 DangerousGetHandle 方法可能會造成安全性風險,因為如果控制碼已標示為無效,則 SetHandleAsInvalid 仍會傳回 DangerousGetHandle 原始的或可能過時的控制碼值。Using the DangerousGetHandle method can pose security risks because, if the handle has been marked as invalid with SetHandleAsInvalid, DangerousGetHandle still returns the original, potentially stale handle value. 傳回的控制碼也可以在任何時間點回收。The returned handle can also be recycled at any point. 最棒的是,這表示控制碼可能突然停止運作。At best, this means the handle might suddenly stop working. 在最糟的情況下,如果控制碼所代表的控制碼或資源已公開給不受信任的程式碼,這可能會導致重複使用或傳回的控制碼發生回收安全性攻擊。At worst, if the handle or the resource that the handle represents is exposed to untrusted code, this can lead to a recycling security attack on the reused or returned handle. 例如,未受信任的呼叫端可以查詢剛剛傳回的控制碼上的資料,並接收完全不相關資源的資訊。For example, an untrusted caller can query data on the handle just returned and receive information for an entirely unrelated resource. 如需 DangerousAddRef 使用 methodsafely 的詳細資訊,請參閱和 DangerousRelease 方法 DangerousGetHandleSee the DangerousAddRef and the DangerousRelease methods for more information about using the DangerousGetHandle methodsafely.