Export (0) Print
Expand All

Marshal.GetComInterfaceForObject(Of T, TInterface) Method (T)

[Supported in the .NET Framework 4.5.1 and later versions]

Returns a pointer to an IUnknown interface that represents the specified interface on an object of the specified type. Custom query interface access is enabled by default.

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

'Declaration
Public Shared Function GetComInterfaceForObject(Of T, TInterface) ( _
	o As T _
) As IntPtr

Type Parameters

T

The type of o.

TInterface

The type of interface to return.

Parameters

o
Type: T

The object that provides the interface.

Return Value

Type: System.IntPtr
The interface pointer that represents the TInterface interface.

ExceptionCondition
ArgumentException

The TInterface parameter is not an interface.

-or-

The type is not visible to COM.

-or-

The T parameter is an open generic type.

InvalidCastException

The o parameter does not support the TInterface interface.

ArgumentNullException

The o parameter is Nothing.

This method returns an interface pointer that represents the TInterface 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 the Marshal.Release method to decrement the reference count when you have finished with the pointer. You must adhere to the rules defined by COM when using raw COM interface pointers.

GetComInterfaceForObject(Of T, TInterface)(T) is useful when calling a method that exposes a COM object parameter as an IntPtr type, or with custom marshaling. You can also use this method on a managed object to obtain a pointer to the object's COM callable wrapper, although this is less common. For example, you can use GetComInterfaceForObject(Of T, TInterface)(T) on a managed object that is exported to COM to obtain an interface pointer for System.Runtime.InteropServices.UCOMIConnectionPointContainer.

The GetComInterfaceForObject(Of T, TInterface)(T) method overload allows query interface customization by default. To specify whether to apply query interface customization, use the GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) method overload.

For additional information, see the COM Callable Wrapper, Runtime Callable Wrapper, and Introducing the Class Interface topics.

.NET Framework

Supported in: 4.5.1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

  • SecurityCriticalAttribute 

    requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows Phone 8.1

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

Show:
© 2014 Microsoft