Editare

Partajați prin


Marshal.Release(IntPtr) Method

Definition

Decrements the reference count on the specified interface.

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

Parameters

pUnk
IntPtr

nativeint

The interface to release.

Returns

The new value of the reference count on the interface specified by the pUnk parameter.

Attributes

Examples

The following example demonstrates how to retrieve an IUnknown interface for a managed object using the GetIUnknownForObject method. The example then releases the interface pointer by calling the Release method.

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

Remarks

The common language runtime manages the reference count of a COM object for you, making it unnecessary to use this method directly. Use this value only for testing purposes. In rare cases, such as testing a custom marshaler, you might find it necessary to manipulate an object's lifetime manually. Only programs that call Marshal.AddRef should call Release. Calling Release after the reference count has reached zero causes undefined behavior.

You can call Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObject, or Marshal.GetIDispatchForObject to obtain an IntPtr value that represents a IUnknown interface pointer to release. You can also use these methods and the Release method on managed objects to release the COM interfaces represented by the managed object's COM Callable Wrapper.

Applies to

See also