Export (0) Print
Expand All
Expand Minimize

Marshal.GetComInterfaceForObject Method

Returns an IUnknown pointer representing the specified interface for an object.

[Visual Basic]
Public Shared Function GetComInterfaceForObject( _
   ByVal o As Object, _
   ByVal T As Type _
) As IntPtr
[C#]
public static IntPtr GetComInterfaceForObject(
 object o,
 Type T
);
[C++]
public: static IntPtr GetComInterfaceForObject(
 Object* o,
 Type* T
);
[JScript]
public static function GetComInterfaceForObject(
   o : Object,
 T : Type
) : IntPtr;

Parameters

o
The object providing the interface.
T
The Type of interface that is requested.

Return Value

The IUnknown pointer representing the interface for the object.

Exceptions

Exception Type Condition
ArgumentException t is not an interface.

-or-

The type is not visible to COM.

InvalidCastException o does not support the requested interface.

Remarks

This method returns an IUnknown pointer that represents the requested interface on the specified object. 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.

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.

Requirements

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 | Release | UCOMIConnectionPointContainer | GetIDispatchForObject

Show:
© 2014 Microsoft