Marshal.Release(IntPtr) Metodo

Definizione

Riduce il numero di riferimenti nell'interfaccia specificata.

public:
 static int Release(IntPtr pUnk);
[System.Security.SecurityCritical]
public static int Release (IntPtr pUnk);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static int Release (IntPtr pUnk);
public static int Release (IntPtr pUnk);
[<System.Security.SecurityCritical>]
static member Release : nativeint -> int
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
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 liberare.

Restituisce

Nuovo valore del numero di riferimenti nell'interfaccia specificata dal parametro pUnk.

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 devono chiamare Marshal.AddRefRelease. 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.

Si applica a

Vedi anche