GCHandle.Alloc-Methode: (Object)
Veröffentlicht: Oktober 2016
Ordnet eine Normal für das angegebene Objekt behandeln.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Syntax
[SecurityCriticalAttribute]
public static GCHandle Alloc(
object value
)
public:
[SecurityCriticalAttribute]
static GCHandle Alloc(
Object^ value
)
[<SecurityCriticalAttribute>]
static member Alloc :
value:Object -> GCHandle
<SecurityCriticalAttribute>
Public Shared Function Alloc (
value As Object
) As GCHandle
Parameter
value
Type: System.ObjectDas Objekt, das GCHandle.
Rückgabewert
Type: System.Runtime.InteropServices.GCHandle
Ein neues GCHandle die das Objekt vor der Garbagecollection schützt. Diese GCHandle müssen freigegeben werden, mit Free Wenn es nicht mehr benötigt wird.
Ausnahmen
Exception | Condition |
---|---|
ArgumentException | Eine Instanz mit nicht primitiven (nicht blitfähigen) Membern kann nicht fixiert werden. |
Hinweise
Normal Handles sind nicht transparent, was bedeutet, dass Sie die Adresse des Objekts nicht auflösen können, die sie über den Handle enthält.
Beispiele
Das folgende Beispiel 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. 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
Sicherheit
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Versionsinformationen
Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1
Siehe auch
GCHandleType
Alloc Überladen
GCHandle-Struktur
System.Runtime.InteropServices-Namespace
Zurück zum Anfang