Marshal.GetComInterfaceForObject Method

Definition

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

Overloads

GetComInterfaceForObject(Object, Type)
Obsolete.

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

GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode)
Obsolete.

Returns a pointer to an IUnknown interface that represents the specified interface on the specified object. Custom query interface access is controlled by the specified customization mode.

GetComInterfaceForObject<T,TInterface>(T)

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.

GetComInterfaceForObject(Object, Type)

Caution

GetComInterfaceForObject(Object, Type) may be unavailable in future releases. Instead, use GetComInterfaceForObject<T,T2>(T). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296509

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

public:
 static IntPtr GetComInterfaceForObject(System::Object ^ o, Type ^ T);
[System.Obsolete("GetComInterfaceForObject(Object, Type) may be unavailable in future releases. Instead, use GetComInterfaceForObject<T,T2>(T). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296509")]
[System.Security.SecurityCritical]
public static IntPtr GetComInterfaceForObject (object o, Type T);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static IntPtr GetComInterfaceForObject (object o, Type T);
[System.Security.SecurityCritical]
public static IntPtr GetComInterfaceForObject (object o, Type T);
public static IntPtr GetComInterfaceForObject (object o, Type T);
[<System.Obsolete("GetComInterfaceForObject(Object, Type) may be unavailable in future releases. Instead, use GetComInterfaceForObject<T,T2>(T). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296509")>]
[<System.Security.SecurityCritical>]
static member GetComInterfaceForObject : obj * Type -> nativeint
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetComInterfaceForObject : obj * Type -> nativeint
[<System.Security.SecurityCritical>]
static member GetComInterfaceForObject : obj * Type -> nativeint
static member GetComInterfaceForObject : obj * Type -> nativeint
Public Shared Function GetComInterfaceForObject (o As Object, T As Type) As IntPtr

Parameters

o
Object

The object that provides the interface.

T
Type

The type of interface that is requested.

Returns

IntPtr

nativeint

The interface pointer that represents the specified interface for the object.

Attributes

Exceptions

The T parameter is not an interface.

-or-

The type is not visible to COM.

-or-

The T parameter is a generic type definition.

The o parameter does not support the requested interface.

The o parameter is null.

-or-

The T parameter is null.

Remarks

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(Object, Type) 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(Object, Type) 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.

The GetComInterfaceForObject(Object, Type) 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 and Runtime Callable Wrapper articles.

See also

Applies to

GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode)

Caution

GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) and support for ICustomQueryInterface may be unavailable in future releases.

Returns a pointer to an IUnknown interface that represents the specified interface on the specified object. Custom query interface access is controlled by the specified customization mode.

public:
 static IntPtr GetComInterfaceForObject(System::Object ^ o, Type ^ T, System::Runtime::InteropServices::CustomQueryInterfaceMode mode);
[System.Obsolete("GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) and support for ICustomQueryInterface may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static IntPtr GetComInterfaceForObject (object o, Type T, System.Runtime.InteropServices.CustomQueryInterfaceMode mode);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static IntPtr GetComInterfaceForObject (object o, Type T, System.Runtime.InteropServices.CustomQueryInterfaceMode mode);
[System.Security.SecurityCritical]
public static IntPtr GetComInterfaceForObject (object o, Type T, System.Runtime.InteropServices.CustomQueryInterfaceMode mode);
public static IntPtr GetComInterfaceForObject (object o, Type T, System.Runtime.InteropServices.CustomQueryInterfaceMode mode);
[<System.Obsolete("GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) and support for ICustomQueryInterface may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member GetComInterfaceForObject : obj * Type * System.Runtime.InteropServices.CustomQueryInterfaceMode -> nativeint
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetComInterfaceForObject : obj * Type * System.Runtime.InteropServices.CustomQueryInterfaceMode -> nativeint
[<System.Security.SecurityCritical>]
static member GetComInterfaceForObject : obj * Type * System.Runtime.InteropServices.CustomQueryInterfaceMode -> nativeint
static member GetComInterfaceForObject : obj * Type * System.Runtime.InteropServices.CustomQueryInterfaceMode -> nativeint
Public Shared Function GetComInterfaceForObject (o As Object, T As Type, mode As CustomQueryInterfaceMode) As IntPtr

Parameters

o
Object

The object that provides the interface.

T
Type

The type of interface that is requested.

mode
CustomQueryInterfaceMode

One of the enumeration values that indicates whether to apply an IUnknown::QueryInterface customization that is supplied by an ICustomQueryInterface.

Returns

IntPtr

nativeint

The interface pointer that represents the interface for the object.

Attributes

Exceptions

The T parameter is not an interface.

-or-

The type is not visible to COM.

-or-

The T parameter is a generic type definition.

The object o does not support the requested interface.

The o parameter is null.

-or-

The T parameter is null.

Remarks

GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) lets you specify whether to apply query interface customization. Use the GetComInterfaceForObject(Object, Type) overload to apply query interface customization by default.

Applies to

GetComInterfaceForObject<T,TInterface>(T)

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.

public:
generic <typename T, typename TInterface>
 static IntPtr GetComInterfaceForObject(T o);
[System.Security.SecurityCritical]
public static IntPtr GetComInterfaceForObject<T,TInterface> (T o);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static IntPtr GetComInterfaceForObject<T,TInterface> (T o);
public static IntPtr GetComInterfaceForObject<T,TInterface> (T o);
[<System.Security.SecurityCritical>]
static member GetComInterfaceForObject : 'T -> nativeint
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetComInterfaceForObject : 'T -> nativeint
static member GetComInterfaceForObject : 'T -> nativeint
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
T

The object that provides the interface.

Returns

IntPtr

nativeint

The interface pointer that represents the TInterface interface.

Attributes

Exceptions

The TInterface parameter is not an interface.

-or-

The type is not visible to COM.

-or-

The T parameter is an open generic type.

The o parameter does not support the TInterface interface.

The o parameter is null.

Remarks

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<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<T,TInterface>(T) on a managed object that is exported to COM to obtain an interface pointer for System.Runtime.InteropServices.UCOMIConnectionPointContainer.

The GetComInterfaceForObject<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 and Runtime Callable Wrapper articles.

Applies to