GCHandle 结构

定义

提供用于从非托管内存访问托管对象的方法。

public value class GCHandle
public value class GCHandle : IEquatable<System::Runtime::InteropServices::GCHandle>
public struct GCHandle
public struct GCHandle : IEquatable<System.Runtime.InteropServices.GCHandle>
[System.Runtime.InteropServices.ComVisible(true)]
public struct GCHandle
type GCHandle = struct
[<System.Runtime.InteropServices.ComVisible(true)>]
type GCHandle = struct
Public Structure GCHandle
Public Structure GCHandle
Implements IEquatable(Of GCHandle)
继承
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 以创建与任何托管对象对应的句柄。 此句柄可以是四种类型之一: WeakWeakTrackResurrectionNormalPinned。 分配句柄后,可以使用它来防止在非托管客户端保留唯一引用时由垃圾回收器收集托管对象。 如果没有此类句柄,可以在代表非托管客户端完成其工作之前由垃圾回收器收集对象。

还可以使用 GCHandle 创建可返回内存地址的固定对象,以防止垃圾回收器在内存中移动对象。

当句柄超出范围时,必须通过调用 Free 方法显式释放它;否则,可能会出现内存泄漏。 释放固定句柄时,如果没有其他引用,关联的对象将被取消固定,并且将有资格进行垃圾回收。

属性

IsAllocated

获取一个值,该值指示是否分配了句柄。

Target

获取或设置该句柄表示的对象。

方法

AddrOfPinnedObject()

Pinned 句柄中检索对象数据的地址。

Alloc(Object)

为指定的对象分配 Normal 句柄。

Alloc(Object, GCHandleType)

为指定的对象分配指定类型的句柄。

Equals(GCHandle)

指示当前实例是否等于同一类型的另一个实例。

Equals(Object)

确定指定的 GCHandle 对象是否等同于当前的 GCHandle

Free()

释放 GCHandle

FromIntPtr(IntPtr)

返回从某个托管对象的句柄创建的新 GCHandle 对象。

GetHashCode()

返回当前 GCHandle 对象的标识符。

ToIntPtr(GCHandle)

返回 GCHandle 对象的内部整数表示形式。

运算符

Equality(GCHandle, GCHandle)

返回一个值,该值指示两个 GCHandle 对象是否相等。

Explicit(GCHandle to IntPtr)

GCHandle 以内部整数表示形式存储。

Explicit(IntPtr to GCHandle)

GCHandle 以内部整数表示形式存储。

Inequality(GCHandle, GCHandle)

返回一个值,该值指示两个 GCHandle 对象是否不相等。

适用于

另请参阅