Marshal.AddRef(IntPtr) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Increments the reference count on the specified interface.
public:
static int AddRef(IntPtr pUnk);
[System.Security.SecurityCritical]
public static int AddRef (IntPtr pUnk);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static int AddRef (IntPtr pUnk);
public static int AddRef (IntPtr pUnk);
[<System.Security.SecurityCritical>]
static member AddRef : nativeint -> int
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member AddRef : nativeint -> int
static member AddRef : nativeint -> int
Public Shared Function AddRef (pUnk As IntPtr) As Integer
Parameters
- pUnk
-
IntPtr
nativeint
The interface reference count to increment.
Returns
The new value of the reference count on the pUnk
parameter.
- Attributes
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 AddRef, you must decrement the reference count by using a method such as Marshal.Release. Do not rely on the return value of AddRef, as it can sometimes be unstable.
You can call Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObject, or Marshal.GetIDispatchForObject to obtain an IntPtr value that represents an IUnknown interface pointer. You can also use these methods and the AddRef 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 COM Callable Wrapper.