Marshal.GetComInterfaceForObject Marshal.GetComInterfaceForObject Marshal.GetComInterfaceForObject Marshal.GetComInterfaceForObject Method

Definition

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

Overloads

GetComInterfaceForObject(Object, Type) GetComInterfaceForObject(Object, Type) GetComInterfaceForObject(Object, Type) GetComInterfaceForObject(Object, Type)

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

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) GetComInterfaceForObject<T,TInterface>(T) GetComInterfaceForObject<T,TInterface>(T) GetComInterfaceForObject<T,TInterface>(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.

GetComInterfaceForObject(Object, Type) GetComInterfaceForObject(Object, Type) GetComInterfaceForObject(Object, Type) GetComInterfaceForObject(Object, Type)

Warning

This API is now 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.

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);
static member GetComInterfaceForObject : obj * Type -> nativeint
Public Shared Function GetComInterfaceForObject (o As Object, T As Type) As IntPtr
Parameters
o
Object Object Object Object

The object that provides the interface.

T
Type Type Type Type

The type of interface that is requested.

Returns

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

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, Runtime Callable Wrapper, and Introducing the Class Interface topics.

Security

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

See Also

GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode)

Warning

This API is now 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.

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);
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 Object Object Object

The object that provides the interface.

T
Type Type Type Type

The type of interface that is requested.

mode
CustomQueryInterfaceMode CustomQueryInterfaceMode CustomQueryInterfaceMode CustomQueryInterfaceMode

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

Returns

The interface pointer that represents the interface for the object.

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.

Security

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

GetComInterfaceForObject<T,TInterface>(T) GetComInterfaceForObject<T,TInterface>(T) GetComInterfaceForObject<T,TInterface>(T) GetComInterfaceForObject<T,TInterface>(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.

public:
generic <typename T, typename TInterface>
 static IntPtr GetComInterfaceForObject(T o);
[System.Security.SecurityCritical]
public static IntPtr GetComInterfaceForObject<T,TInterface> (T o);
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

The object that provides the interface.

Returns

The interface pointer that represents the TInterface interface.

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.

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, Runtime Callable Wrapper, and Introducing the Class Interface topics.

Security

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

Applies to