Freigeben über


GCHandle.ToIntPtr-Methode: (GCHandle)

 

Veröffentlicht: Oktober 2016

Gibt die interne ganzzahlige Darstellung der ein GCHandle Objekt.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

Syntax

public static IntPtr ToIntPtr(
    GCHandle value
)
public:
static IntPtr ToIntPtr(
    GCHandle value
)
static member ToIntPtr : 
        value:GCHandle -> nativeint
Public Shared Function ToIntPtr (
    value As GCHandle
) As IntPtr

Parameter

Rückgabewert

Type: System.IntPtr

Ein IntPtr -Objekt, das stellt ein GCHandle Objekt.

Beispiele

Das folgende Codebeispiel zeigt eine App -Klasse, erstellt ein Handle für ein verwaltetes Objekt mit, der GCHandle.Alloc -Methode, die verhindert, dass das verwaltete Objekt erfasst wird. Ein Aufruf der EnumWindows Methode übergibt ein Delegat und ein verwaltetes Objekt (beide sind als verwaltete Typen deklariert, jedoch nicht angezeigt), und wandelt das Handle für ein IntPtr Objekt. Die nicht verwaltete Funktion gibt den Typ an dem Aufrufer als Parameter der Rückruffunktion.

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;
    }
}
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

Versionsinformationen

Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 2.0
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Windows Phone Silverlight
Verfügbar seit 8.0
Windows Phone
Verfügbar seit 8.1

Siehe auch

GCHandle-Struktur
System.Runtime.InteropServices-Namespace

Zurück zum Anfang