GCHandle.Target プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > GCHandle.Target プロパティの意味・解説 

GCHandle.Target プロパティ

ハンドルが表すオブジェクト取得または設定します

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

InvalidOperationException

ハンドル解放されたか、または初期化されていません。

使用例使用例

GCHandle.Alloc メソッド使用してマネージ オブジェクトへのハンドル作成する App クラスの例を次に示します。これによって、マネージ オブジェクトガベージ コレクション対象から除外されます。EnumWindows メソッド呼び出しは、デリゲートマネージ オブジェクト (両方ともマネージ型として宣言されていますが、ここでは示されていません) を渡しハンドルを IntPtr にキャストます。このアンマネージ関数は、コールバック関数パラメータとして型を呼び出し元に戻します

Imports System
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


Module LibWrap

    ' passing managed object as LPARAM
    ' BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
    <DllImport("user32.dll")> _
    Function EnumWindows(ByVal cb As
 CallBack, ByVal param As IntPtr) As
 Boolean
    End Function
End Module 'LibWrap


Module App

    Sub Main()
    Run()

    End Sub

    <SecurityPermission(SecurityAction.Demand, UnmanagedCode:=true)> _
    Sub Run()

        Dim tw As TextWriter = System.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
        LibWrap.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
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;

public delegate bool CallBack(int
 handle, IntPtr param);

public class LibWrap
{
    // passing managed object as LPARAM
    // BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);

    [DllImport("user32.dll")]
    public static extern bool
 EnumWindows(CallBack cb, IntPtr param);
}

public class App
{
    public static void Main()
    {
        Run();
    }

        [SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)]
    public static void Run()
        {
        TextWriter tw = System.Console.Out;
        GCHandle gch = GCHandle.Alloc(tw);

        CallBack cewp = new CallBack(CaptureEnumWindowsProc);

        // platform invoke will prevent delegate to be garbage collected
        // before call ends

        LibWrap.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;
    }
}
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
GCHandle 構造体
GCHandle メンバ
System.Runtime.InteropServices 名前空間
GCHandleType



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

GCHandle.Target プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



GCHandle.Target プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS