System.Runtime.InteropServices.Marshal.AddRef Method

Increments the reference count on the specified interface.

Syntax

public static int AddRef (IntPtr pUnk)

Parameters

pUnk
The interface reference count to increment.

Returns

The new value of the reference count on the pUnk parameter.

Remarks

The common language runtime manages the reference count of a COM object for you, making it unnecessary to use this method directly. In rare cases, such as testing a custom marshaler, you might find it necessary to manipulate an object's lifetime manually. After calling Marshal.AddRef(IntPtr), you must decrement the reference count by using a method such as Marshal.Release(IntPtr). Do not rely on the return value of Marshal.AddRef(IntPtr), as it can sometimes be unstable.

You can call Marshal.GetComInterfaceForObject(object, Type), Marshal.GetIUnknownForObject(object), or Marshal.GetIDispatchForObject(object) to obtain an IntPtr value that represents an tp://go.microsoft.com/fwlink/?LinkId=148003 interface pointer. You can also use these methods and the Marshal.AddRef(IntPtr) method on managed objects to obtain the COM interfaces represented by the managed object's COM callable wrapper. If you are not familiar with the details of this wrapper type, see [<topic://cpconcomcallablewrapper>].

Requirements

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0