GCHandle.ToIntPtr メソッドとは? わかりやすく解説

GCHandle.ToIntPtr メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

GCHandle オブジェクト内部整数表現返します

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

Public Shared Function ToIntPtr
 ( _
    value As GCHandle _
) As IntPtr
Dim value As GCHandle
Dim returnValue As IntPtr

returnValue = GCHandle.ToIntPtr(value)
public static IntPtr ToIntPtr (
    GCHandle value
)
public:
static IntPtr ToIntPtr (
    GCHandle value
)
public static IntPtr ToIntPtr (
    GCHandle value
)
public static function ToIntPtr
 (
    value : GCHandle
) : IntPtr

パラメータ

value

内部整数表現取得元の GCHandle オブジェクト

戻り値
GCHandle オブジェクトを表す IntPtr オブジェクト

使用例使用例

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;
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
GCHandle 構造体
GCHandle メンバ
System.Runtime.InteropServices 名前空間



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

辞書ショートカット

すべての辞書の索引

「GCHandle.ToIntPtr メソッド」の関連用語

GCHandle.ToIntPtr メソッドのお隣キーワード
検索ランキング

   

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



GCHandle.ToIntPtr メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS