This documentation is archived and is not being maintained.

Marshal.GetTypedObjectForIUnknown Method

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

[Visual Basic]
Public Shared Function GetTypedObjectForIUnknown( _
   ByVal pUnk As IntPtr, _
   ByVal t As Type _
) As Object
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 Type Condition
ArgumentException 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.

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 | ComImportAttribute | Type Library Importer (Tlbimp.exe)