Marshal.GetIUnknownForObject(Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce un'interfaccia IUnknown da un oggetto gestito.
public:
static IntPtr GetIUnknownForObject(System::Object ^ o);
[System.Security.SecurityCritical]
public static IntPtr GetIUnknownForObject (object o);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static IntPtr GetIUnknownForObject (object o);
public static IntPtr GetIUnknownForObject (object o);
[<System.Security.SecurityCritical>]
static member GetIUnknownForObject : obj -> nativeint
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetIUnknownForObject : obj -> nativeint
static member GetIUnknownForObject : obj -> nativeint
Public Shared Function GetIUnknownForObject (o As Object) As IntPtr
Parametri
- o
- Object
Oggetto di cui è richiesta l'interfaccia IUnknown
.
Restituisce
-
IntPtr
nativeint
Puntatore IUnknown
per il parametro o
.
- Attributi
Esempio
Nell'esempio seguente viene illustrato come recuperare un'interfaccia IUnknown per un oggetto gestito usando il GetIUnknownForObject metodo .
using System;
using System.Runtime.InteropServices;
class Program
{
static void Run()
{
// Create an int object
int obj = 1;
Console.WriteLine("Calling Marshal.GetIUnknownForObject...");
// Get the IUnKnown pointer for the Integer object
IntPtr pointer = Marshal.GetIUnknownForObject(obj);
Console.WriteLine("Calling Marshal.Release...");
// Always call Marshal.Release to decrement the reference count.
Marshal.Release(pointer);
}
static void Main(string[] args)
{
Run();
}
}
Imports System.Runtime.InteropServices
Module Program
Sub Run()
' Dim an Integer object
Dim IntegerObject As Integer = 1
' Dim a pointer
Dim pointer As IntPtr
Console.WriteLine("Calling Marshal.GetIUnknownForObject...")
' Get the IUnKnown pointer for the Integer object
pointer = Marshal.GetIUnknownForObject(IntegerObject)
Console.WriteLine("Calling Marshal.Release...")
' Always call Marshal.Release to decrement the reference count.
Marshal.Release(pointer)
End Sub
Sub Main(ByVal args() As String)
Run()
End Sub
End Module
Commenti
Nel codice gestito si lavora raramente direttamente con l'interfaccia IUnknown
. Tuttavia, GetIUnknownForObject è utile quando si chiama un metodo che espone un parametro di oggetto COM come IntPtr tipo o con marshalling personalizzato. Se si chiama un oggetto con questo metodo, il conteggio dei riferimenti viene incrementato sul puntatore dell'interfaccia prima che venga restituito il puntatore. Usare Marshal.Release sempre per decrementare il conteggio dei riferimenti al termine dell'operazione con il puntatore. Questo metodo fornisce la funzionalità opposta del Marshal.GetObjectForIUnknown metodo .
È anche possibile utilizzare questo metodo su un oggetto gestito per ottenere un puntatore di interfaccia al Wrapper chiamabile COM per l'oggetto .