SafeHandle.DangerousGetHandle SafeHandle.DangerousGetHandle SafeHandle.DangerousGetHandle SafeHandle.DangerousGetHandle Method


返回 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.


您可以使用此方法从SafeHandle派生类的实例中检索实际的句柄值。You 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.


使用方法可能会带来安全风险,因为如果句柄已使用SetHandleAsInvalid标记为无效, DangerousGetHandle则仍将返回原始的、可能过时的句柄值。 DangerousGetHandleUsing 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. 有关使用methodsafelyDangerousGetHandleDangerousRelease详细信息,请参阅和方法。DangerousAddRefSee the DangerousAddRef and the DangerousRelease methods for more information about using the DangerousGetHandle methodsafely.


用于调用非托管代码的权限。for permission to call unmanaged code. 安全操作: LinkDemandSecurity action: LinkDemand. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode