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

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > GCHandle.FromIntPtr メソッドの意味・解説 

GCHandle.FromIntPtr メソッド

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

マネージ オブジェクト識別するハンドルから作成され新しGCHandle オブジェクト返します

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

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

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

パラメータ

value

GCHandle オブジェクトの作成元のマネージ オブジェクト識別する IntPtr ハンドル

戻り値
パラメータ対応する新しGCHandle オブジェクト

例外例外
例外種類条件

InvalidOperationException

value パラメータの値が Zero です。

使用例使用例

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 名前空間



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

辞書ショートカット

すべての辞書の索引

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

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

   

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



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

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

©2025 GRAS Group, Inc.RSS