This documentation is archived and is not being maintained.

Marshal.GetObjectForIUnknown Method

Returns an instance of a type that represents a COM object by a pointer to its IUnknown interface.

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


A pointer to the IUnknown interface.

Return Value

An object representing the specified unmanaged COM object.


This method wraps IUnknown in a managed object. This has the effect of incrementing the reference count of the COM component. The reference count will be decremented when the runtime performs garbage collection on the managed object representing the COM object.

The first parameter, pUnk, represents an IUnknown interface pointer; however, because all COM interfaces derive directly or indirectly from IUnknown, you can pass any COM interface to this method. The object returned by GetObjectForIUnknown is a runtime callable wrapper, which the common language runtime manages as it does any other managed object. The type of this wrapper is often a generic System.__ComObject type, which is a hidden type used when the wrapper type is ambiguous. You can still make late-bound calls to such a generic type as long as the COM object implements the IDispatch interface. Likewise, you can cast the returned object to an appropriate COM interface. For additional information, see Runtime Callable Wrapper.

For an object to be wrapped with a specific managed class type (and not a generic wrapper type), you must adhere to the following requirements:

  • Implement the IProvideClassInfo interface for the COM object.
  • Register the containing assembly with the Assembly Registration Tool (Regasm.exe).

Alternatively, you can avoid these requirements and still get an object that is wrapped with a specific managed class type by using the Marshal.GetTypedObjectForIUnknown method.

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 | AddRef | Release | GetTypedObjectForIUnknown