Marshal.GetTypedObjectForIUnknown Method

Returns a managed object of a specified type that represents a COM object.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

public static Object GetTypedObjectForIUnknown (
	IntPtr pUnk,
	Type t
public static Object GetTypedObjectForIUnknown (
	IntPtr pUnk, 
	Type t
public static function GetTypedObjectForIUnknown (
	pUnk : IntPtr, 
	t : Type
) : Object



A pointer to the IUnknown interface of the unmanaged object.


The Type of the requested managed class.

Return Value

An instance of the class corresponding to the Type object that represents the requested unmanaged COM object.

Exception typeCondition


t is not attributed with System.Runtime.InteropServices.ComImportAttribute.

The t parameter must be either a COM-imported type or a subtype of a COM-imported type. In addition, t must be a type whose metadata was imported by the Type Library Importer (Tlbimp.exe). This type must be a class and not an associated coclass interface, which carries the name of the COM class. For example, suppose Tlbimp.exe imports Myclass as a class called MyclassClass and a coclass interface called Myclass. Be sure to use MyclassClass, not Myclass with this method. For additional information about imported classes and coclass interfaces, see Imported Type Conversion.

If an object has already been obtained for the pUnk parameter, then t is ignored and the existing object is returned. 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 GetTypedObjectForIUnknown is a runtime callable wrapper, which the common language runtime manages as it does any other managed object. For additional information, see Runtime Callable Wrapper.


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.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0