Método GCHandle.FromIntPtr (IntPtr)
Publicado: octubre de 2016
Devuelve un nuevo objeto GCHandle creado a partir de un identificador a un objeto administrado.
Espacio de nombres: System.Runtime.InteropServices
Ensamblado: mscorlib (en mscorlib.dll)
Sintaxis
[SecurityCriticalAttribute]
public static GCHandle FromIntPtr(
IntPtr value
)
public:
[SecurityCriticalAttribute]
static GCHandle FromIntPtr(
IntPtr value
)
[<SecurityCriticalAttribute>]
static member FromIntPtr :
value:nativeint -> GCHandle
<SecurityCriticalAttribute>
Public Shared Function FromIntPtr (
value As IntPtr
) As GCHandle
Parámetros
value
Type: System.IntPtrIdentificador IntPtr a un objeto administrado del que se va a crear un objeto GCHandle.
Valor devuelto
Type: System.Runtime.InteropServices.GCHandle
Nuevo objeto GCHandle que corresponde al parámetro de valor.
Excepciones
Exception | Condition |
---|---|
InvalidOperationException | El valor del parámetro value es Zero. |
Ejemplos
The following code example shows an App class that creates a handle to a managed object using the GCHandle.Alloc method, which prevents the managed object from being collected. A call to the EnumWindows method passes a delegate and a managed object (both declared as managed types, but not shown), and casts the handle to an T:System.IntPtr object. The unmanaged function passes the type back to the caller as a parameter of the callback function.
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
Seguridad
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Información de versión
Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 2.0
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Windows Phone Silverlight
Disponible desde 8.0
Windows Phone
Disponible desde 8.1
Ver también
Estructura GCHandle
Espacio de nombres System.Runtime.InteropServices
Volver al principio