This documentation is archived and is not being maintained.

Marshal.AddRef Method

Increments the reference count on the specified interface.

[Visual Basic]
Public Shared Function AddRef( _
   ByVal pUnk As IntPtr _
) As Integer
[C#]
public static int AddRef(
 IntPtr pUnk
);
[C++]
public: static int AddRef(
 IntPtr pUnk
);
[JScript]
public static function AddRef(
   pUnk : IntPtr
) : int;

Parameters

pUnk
The interface reference count to increment.

Return Value

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 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 this wrapper type, see COM Callable Wrapper. For additional information about IUnknown::AddRef, see the MSDN Library.

Note   This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace | Release | QueryInterface | GetComInterfaceForObject | GetIUnknownForObject | GetIDispatchForObject

Show: