Marshal.Release(IntPtr) 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.
Decrementa il conteggio dei riferimenti sull'interfaccia specificata.
public:
static int Release(IntPtr pUnk);
[System.Security.SecurityCritical]
public static int Release(IntPtr pUnk);
public static int Release(IntPtr pUnk);
[<System.Security.SecurityCritical>]
static member Release : nativeint -> int
static member Release : nativeint -> int
Public Shared Function Release (pUnk As IntPtr) As Integer
Parametri
- pUnk
-
IntPtr
nativeint
Interfaccia da rilasciare.
Valori restituiti
Nuovo valore del conteggio dei riferimenti sull'interfaccia specificata dal pUnk parametro .
- Attributi
Esempio
Nell'esempio seguente viene illustrato come recuperare un'interfaccia IUnknown per un oggetto gestito usando il GetIUnknownForObject metodo . L'esempio rilascia quindi il puntatore all'interfaccia chiamando il Release 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
Common Language Runtime gestisce automaticamente il conteggio dei riferimenti di un oggetto COM, rendendo superfluo l'uso diretto di questo metodo. Usare questo valore solo a scopo di test. In rari casi, ad esempio il test di un gestore di marshalling personalizzato, potrebbe essere necessario modificare manualmente la durata di un oggetto. Solo i programmi che chiamano Marshal.AddRef devono chiamare Release. La chiamata Release dopo che il conteggio dei riferimenti ha raggiunto zero causa un comportamento non definito.
È possibile chiamare Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObjecto Marshal.GetIDispatchForObject per ottenere un IntPtr valore che rappresenta un puntatore all'interfaccia IUnknown da rilasciare. È anche possibile utilizzare questi metodi e il Release metodo sugli oggetti gestiti per rilasciare le interfacce COM rappresentate dal COM Callable Wrapper dell'oggetto gestito.