Marshal.GetComInterfaceForObject Method

Returns an interface pointer that represents the specified interface for an object.

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

public static IntPtr GetComInterfaceForObject (
	Object o,
	Type T
public static IntPtr GetComInterfaceForObject (
	Object o, 
	Type T
public static function GetComInterfaceForObject (
	o : Object, 
	T : Type
) : IntPtr
Not applicable.



The object providing the interface.


The Type of interface that is requested.

Return Value

The interface pointer representing the interface for the object.

Exception typeCondition


The T parameter is not an interface.


The type is not visible to COM.


The T parameter is a generic type.


The o parameter does not support the requested interface.


The o parameter is a null reference (Nothing in Visual Basic)


The T parameter is a null reference (Nothing in Visual Basic)

This method returns an interface pointer that represents the requested interface on the specified object. It is particularly useful if you have an unmanaged method that expects to be passed an interface pointer. 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. You must adhere to the rules defined by COM when using raw COM interface pointers.

GetComInterfaceForObject is useful when calling a method that exposes a COM object parameter as an IntPtr type, or with custom marshaling. Although less common, you can use this method on a managed object to obtain a pointer to the object's COM callable wrapper. For example, you can use GetComInterfaceForObject on a managed object that is exported to COM to obtain an interface pointer for System.Runtime.InteropServices.UCOMIConnectionPointContainer. You cannot obtain a pointer to a class interface since a class interface lacks the corresponding type to pass to the second parameter (t). Instead, use Marshal.GetIDispatchForObject to invoke the members on the default interface of the COM callable wrapper, which is usually an auto-dispatch class interface.

For additional information on runtime callable wrappers and COM callable wrappers, see COM Wrappers. For additional information on class interfaces, see Introducing the Class Interface.


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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0