GCHandle 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供从非托管内存访问托管对象的方法。
public value class GCHandle
public struct GCHandle
[System.Runtime.InteropServices.ComVisible(true)]
public struct GCHandle
type GCHandle = struct
[<System.Runtime.InteropServices.ComVisible(true)>]
type GCHandle = struct
Public Structure GCHandle
- 继承
- 属性
示例
下面的示例演示了一个 App 类,该类使用 GCHandle.Alloc 该方法创建托管对象的句柄,从而阻止收集托管对象。 对 EnumWindows 方法的调用传递委托和托管对象(两者都声明为托管类型,但未显示),并将句柄强制转换为句 IntPtr柄。 非托管函数将类型传递回调用方作为回调函数的参数。
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public delegate bool CallBack(int handle, IntPtr param);
internal static class NativeMethods
{
// passing managed object as LPARAM
// BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
[DllImport("user32.dll")]
internal static extern bool EnumWindows(CallBack cb, IntPtr param);
}
public class App
{
public static void Main()
{
Run();
}
public static void Run()
{
TextWriter tw = Console.Out;
GCHandle gch = GCHandle.Alloc(tw);
CallBack cewp = new CallBack(CaptureEnumWindowsProc);
// platform invoke will prevent delegate to be garbage collected
// before call ends
NativeMethods.EnumWindows(cewp, GCHandle.ToIntPtr(gch));
gch.Free();
}
private static bool CaptureEnumWindowsProc(int handle, IntPtr param)
{
GCHandle gch = GCHandle.FromIntPtr(param);
TextWriter tw = (TextWriter)gch.Target;
tw.WriteLine(handle);
return true;
}
}
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Public Delegate Function CallBack(ByVal handle As Integer, ByVal param As IntPtr) As Boolean
Friend Module NativeMethods
' passing managed object as LPARAM
' BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
<DllImport("user32.dll")>
Friend Function EnumWindows(ByVal cb As CallBack, ByVal param As IntPtr) As Boolean
End Function
End Module
Module App
Sub Main()
Run()
End Sub
<SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)>
Sub Run()
Dim tw As TextWriter = Console.Out
Dim gch As GCHandle = GCHandle.Alloc(tw)
Dim cewp As CallBack
cewp = AddressOf CaptureEnumWindowsProc
' platform invoke will prevent delegate to be garbage collected
' before call ends
NativeMethods.EnumWindows(cewp, GCHandle.ToIntPtr(gch))
gch.Free()
End Sub
Function CaptureEnumWindowsProc(ByVal handle As Integer, ByVal param As IntPtr) As Boolean
Dim gch As GCHandle = GCHandle.FromIntPtr(param)
Dim tw As TextWriter = CType(gch.Target, TextWriter)
tw.WriteLine(handle)
Return True
End Function
End Module
注解
该 GCHandle 结构与枚举一起使用 GCHandleType ,以创建对应于任何托管对象的句柄。 此句柄可以是四种类型之一:Weak、WeakTrackResurrection、或PinnedNormal。 分配句柄后,当非托管客户端保留唯一引用时,可以使用它来防止垃圾回收器收集托管对象。 如果没有此类句柄,则对象可由垃圾回收器收集,然后代表非托管客户端完成其工作。
还可以用于 GCHandle 创建一个固定对象,该对象返回内存地址以防止垃圾回收器在内存中移动对象。
当句柄超出范围时,必须通过调用 Free 该方法显式释放它;否则,可能会发生内存泄漏。 释放固定的句柄时,关联的对象将被取消固定,并且如果没有对它的其他引用,将有资格进行垃圾回收。
属性
| 名称 | 说明 |
|---|---|
| IsAllocated |
获取一个值,该值指示是否分配句柄。 |
| Target |
获取或设置此句柄表示的对象。 |
方法
| 名称 | 说明 |
|---|---|
| AddrOfPinnedObject() |
检索句柄中 Pinned 对象数据的地址。 |
| Alloc(Object, GCHandleType) |
为指定对象分配指定类型的句柄。 |
| Alloc(Object) |
为指定对象分配句 Normal 柄。 |
| Equals(Object) | |
| Free() |
发布一个 GCHandle. |
| FromIntPtr(IntPtr) |
返回从句柄创建到托管对象的新 GCHandle 对象。 |
| GetHashCode() |
返回当前 GCHandle 对象的标识符。 |
| ToIntPtr(GCHandle) |
返回对象的内部整数表示形式 GCHandle 。 |
运营商
| 名称 | 说明 |
|---|---|
| Equality(GCHandle, GCHandle) |
返回一个值,该值指示两 GCHandle 个对象是否相等。 |
| Explicit(GCHandle to IntPtr) |
A GCHandle 使用内部整数表示形式存储。 |
| Explicit(IntPtr to GCHandle) |
A GCHandle 使用内部整数表示形式存储。 |
| Inequality(GCHandle, GCHandle) |
返回一个值,该值指示两 GCHandle 个对象是否不相等。 |