GetIUnknownForObject Method
Collapse the table of content
Expand the table of content

Marshal.GetIUnknownForObject Method

Returns an IUnknown interface from a managed object.

[Visual Basic]
Public Shared Function GetIUnknownForObject( _
   ByVal o As Object _
) As IntPtr
public static IntPtr GetIUnknownForObject(
 object o
public: static IntPtr GetIUnknownForObject(
 Object* o
public static function GetIUnknownForObject(
   o : Object
) : IntPtr;


The object whose IUnknown interface is requested.

Return Value

The IUnknown pointer for the o parameter.


In managed code, you seldom work directly with the IUnknown interface. However, GetIUnknownForObject is useful when calling a method that exposes a COM object parameter as an IntPtr type, or with custom marshaling. Calling an object with this method causes the reference count to increment on the interface pointer before the pointer is returned. Always use Marshal.Release to decrement the reference count once you have finished with the pointer. This method provides the opposite functionality of the Marshal.GetObjectForIUnknown method.

You can also use this method on a managed object to obtain an interface pointer to the COM callable wrapper for the object. For additional information, 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.


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 | GetObjectForIUnknown

© 2015 Microsoft