Decrements the reference count on the specified interface.
[Visual Basic] Public Shared Function Release( _ ByVal pUnk As IntPtr _ ) As Integer [C#] public static int Release( IntPtr pUnk ); [C++] public: static int Release( IntPtr pUnk ); [JScript] public static function Release( pUnk : IntPtr ) : int;
- The interface to release.
The new value of the reference count on the interface specified by the pUnk parameter.
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. If you are not familiar with the details of this wrapper type, see COM Callable Wrapper.
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.
[Visual Basic, C#] The following code example demonstrates the use of the Release method. This example uses GetIUnknownForObject to obtain an IntPtr value representing the COM object's IUnknown interface pointer.
[Visual Basic] Dim pUnk As IntPtr = Marshal.GetIUnknownForObject(myComObject) Dim refCount As Integer = Marshal.Release(pUnk) [C#] IntPtr pUnk = Marshal.GetIUnknownForObject(myComObject); int refCount = Marshal.Release(pUnk);
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page.
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:
- SecurityPermission for operating with unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode.