GCHandle 結構

定義

提供從 Unmanaged 記憶體存取 Managed 物件的方法。

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 方法建立 Managed 物件的 GCHandle.Alloc 控制碼的類別,以防止收集 Managed 物件。 對 方法的 EnumWindows 呼叫會傳遞委派和 Managed 物件, (宣告為 Managed 型別,但未顯示) ,並將控制碼 IntPtr 轉換成 。 Unmanaged 函式會將類型傳回給呼叫端,做為回呼函式的參數。

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 使用,以建立對應至任何 Managed 物件的控制碼。 此控制碼可以是四種類型之一: WeakWeakTrackResurrectionNormalPinned 。 配置控制碼時,您可以使用它來防止垃圾收集行程在 Unmanaged 用戶端保留唯一的參考時收集 Managed 物件。 如果沒有這類控制碼,垃圾收集行程就可以先收集物件,再代表 Unmanaged 用戶端完成其工作。

您也可以使用 GCHandle 來建立固定的物件,以傳回記憶體位址,以防止垃圾收集行程將物件移到記憶體中。

當控制碼超出範圍時,您必須呼叫 Free 方法來明確釋放它,否則可能會發生記憶體流失。 當您釋放釘選的控制碼時,相關聯的物件將會取消釘選,而且如果沒有任何其他參考,就會成為垃圾收集的資格。

屬性

IsAllocated

取得值,指出控制代碼是否已配置。

Target

取得或設定這個控制代碼表示的物件。

方法

AddrOfPinnedObject()

擷取 Pinned 控制代碼中物件資料的位址。

Alloc(Object)

配置 Normal 控制代碼給指定的物件。

Alloc(Object, GCHandleType)

配置指定類型的控制代碼給指定的物件。

Equals(GCHandle)

指出目前的實例是否等於相同類型的另一個實例。

Equals(Object)

判斷指定的 GCHandle 物件是否等於目前的 GCHandle 物件。

Free()

釋放 GCHandle

FromIntPtr(IntPtr)

傳回新的 GCHandle 物件,而此物件是從 Managed 物件的控制代碼所建立。

GetHashCode()

傳回目前 GCHandle 物件的識別項。

ToIntPtr(GCHandle)

傳回 GCHandle 物件的內部整數表示。

運算子

Equality(GCHandle, GCHandle)

傳回值,這個值表示兩個 GCHandle 物件是否相等。

Explicit(GCHandle to IntPtr)

GCHandle 是使用內部整數表示來儲存。

Explicit(IntPtr to GCHandle)

GCHandle 是使用內部整數表示來儲存。

Inequality(GCHandle, GCHandle)

傳回值,指出兩個 GCHandle 物件是否不相等。

適用於

另請參閱