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 対応するハンドルを作成するために列挙と共に使用されます。 このハンドルには、次の 4 種類のいずれかを指定できます。 Weak``WeakTrackResurrection``Normal``Pinned ハンドルが割り当てられている場合は、それを使用して、アンマネージド クライアントが唯一の参照を保持しているときに、ガベージ コレクターによってマネージド オブジェクトが収集されないようにすることができます。 このようなハンドルがない場合、オブジェクトは、アンマネージ クライアントの代わりに作業を完了する前に、ガベージ コレクターによって収集できます。

また、メモリ アドレスを返すピン留めされたオブジェクトを作成して、ガベージ コレクターがメモリ内でオブジェクトを移動できないようにすることもできます 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 の 2 つのオブジェクトが等しいかどうかを示す値を返します。

Explicit(GCHandle to IntPtr)

GCHandle は、内部整数表現を使用して格納されます。

Explicit(IntPtr to GCHandle)

GCHandle は、内部整数表現を使用して格納されます。

Inequality(GCHandle, GCHandle)

GCHandle の 2 つのオブジェクトが等しくないかどうかを示す値を返します。

適用対象

こちらもご覧ください