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

定义

返回一个接口指针,该接口指针表示对象的接口。Returns an interface pointer that represents an interface for an object.

重载

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

返回一个指向 IUnknown 接口的指针,该指针表示指定对象上的指定接口。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)

返回一个指向 IUnknown 接口的指针,该指针表示指定对象上的指定接口。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)

[在 .NET Framework 4.5.1 和更高版本中受支持][Supported in the .NET Framework 4.5.1 and later versions] 返回一个指向 IUnknown 接口的指针,该指针表示指定类型的对象上的指定接口。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)

警告

此 API 现已过时。

返回一个指向 IUnknown 接口的指针,该指针表示指定对象上的指定接口。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

参数

o
Object Object Object Object

提供接口的对象。The object that provides the interface.

T
Type Type Type Type

所请求接口的类型。The type of interface that is requested.

返回

表示对象的指定接口的接口指针。The interface pointer that represents the specified interface for the object.

异常

T 参数不是接口。The T parameter is not an interface.

- 或 --or- 该类型对 COM 不可见。The type is not visible to COM.

- 或 --or- T 参数是泛型类型定义。The T parameter is a generic type definition.

o 参数不支持请求的接口。The o parameter does not support the requested interface.

o 参数为 nullThe o parameter is null.

- 或 --or- T 参数为 nullThe T parameter is null.

注解

此方法返回表示指定对象上请求的接口的接口指针。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. 始终使用Marshal.Release要递减引用计数,完成后使用指针。Always use Marshal.Release to decrement the reference count once you have finished with the pointer. 您必须遵守的规则由 COM 使用原始 COM 接口指针时。You must adhere to the rules defined by COM when using raw COM interface pointers.

GetComInterfaceForObject(Object, Type) 当调用方法公开为 COM 对象参数非常有用IntPtr类型,或使用自定义封送处理。GetComInterfaceForObject(Object, Type) is useful when calling a method that exposes a COM object parameter as an IntPtr type, or with custom marshaling. 尽管不太常见,您可以托管对象上使用此方法以获取指向对象的 COM 可调用包装器。Although less common, you can use this method on a managed object to obtain a pointer to the object's COM callable wrapper. 例如,可以使用GetComInterfaceForObject(Object, Type)的托管对象的导出到 COM,若要获取的接口指针System.Runtime.InteropServices.UCOMIConnectionPointContainerFor 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. 无法获取类接口的指针,因为类接口中没有要传递给第二个参数的相应类型 (t)。You cannot obtain a pointer to a class interface since a class interface lacks the corresponding type to pass to the second parameter (t). 请改用Marshal.GetIDispatchForObject来调用在 COM 可调用包装器,这通常是一个自动调度类接口的默认接口成员。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.

GetComInterfaceForObject(Object, Type)方法重载默认情况下允许查询界面自定义。The GetComInterfaceForObject(Object, Type) method overload allows query interface customization by default. 若要指定是否要应用查询界面自定义,请使用GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode)方法重载。To specify whether to apply query interface customization, use the GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) method overload.

有关其他信息,请参阅COM 可调用包装器运行时可调用包装器,并类接口简介主题。For additional information, see the COM Callable Wrapper, Runtime Callable Wrapper, and Introducing the Class Interface topics.

安全性

SecurityCriticalAttribute
直接调用方需要完全信任。requires full trust for the immediate caller. 不能由部分受信任的或透明的代码使用此成员。This member cannot be used by partially trusted or transparent code.

另请参阅

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

警告

此 API 现已过时。

返回一个指向 IUnknown 接口的指针,该指针表示指定对象上的指定接口。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

参数

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

枚举值之一,用于指示是否要应用 ICustomQueryInterface 提供的 IUnknown::QueryInterface 自定义。One of the enumeration values that indicates whether to apply an IUnknown::QueryInterface customization that is supplied by an ICustomQueryInterface.

返回

表示对象的接口的接口指针。The interface pointer that represents the interface for the object.

异常

T 参数不是接口。The T parameter is not an interface.

- 或 --or- 该类型对 COM 不可见。The type is not visible to COM.

- 或 --or- T 参数是泛型类型定义。The T parameter is a generic type definition.

对象 o 不支持请求的接口。The object o does not support the requested interface.

o 参数为 nullThe o parameter is null.

- 或 --or- T 参数为 nullThe T parameter is null.

注解

GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) 可以指定是否要应用查询界面自定义。GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) lets you specify whether to apply query interface customization. 使用GetComInterfaceForObject(Object, Type)重载默认情况下应用查询界面自定义。Use the GetComInterfaceForObject(Object, Type) overload to apply query interface customization by default.

安全性

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)

[在 .NET Framework 4.5.1 和更高版本中受支持][Supported in the .NET Framework 4.5.1 and later versions] 返回一个指向 IUnknown 接口的指针,该指针表示指定类型的对象上的指定接口。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

类型参数

T

o 的类型。The type of o.

TInterface

要返回的接口的类型。The type of interface to return.

参数

o
T T T T

提供接口的对象。The object that provides the interface.

返回

表示 TInterface 接口的接口指针。The interface pointer that represents the TInterface interface.

异常

TInterface 参数不是接口。The TInterface parameter is not an interface.

- 或 --or- 该类型对 COM 不可见。The type is not visible to COM.

- 或 --or- T 参数是开放式泛型类型。The T parameter is an open generic type.

o 参数不支持 TInterface 接口。The o parameter does not support the TInterface interface.

注解

此方法返回表示的接口指针TInterface上指定的对象的接口。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. 始终使用Marshal.Release方法以递减引用计数后的指针。Always use the Marshal.Release method to decrement the reference count when you have finished with the pointer. 您必须遵守的规则由 COM 使用原始 COM 接口指针时。You must adhere to the rules defined by COM when using raw COM interface pointers.

GetComInterfaceForObject<T,TInterface>(T) 当调用方法公开为 COM 对象参数非常有用IntPtr类型,或使用自定义封送处理。GetComInterfaceForObject<T,TInterface>(T) is useful when calling a method that exposes a COM object parameter as an IntPtr type, or with custom marshaling. 您还可用于此方法在托管对象上获取指向对象的 COM 可调用包装器,尽管这并不常见。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. 例如,可以使用GetComInterfaceForObject<T,TInterface>(T)的托管对象的导出到 COM,若要获取的接口指针System.Runtime.InteropServices.UCOMIConnectionPointContainerFor 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.

GetComInterfaceForObject<T,TInterface>(T)方法重载默认情况下允许查询界面自定义。The GetComInterfaceForObject<T,TInterface>(T) method overload allows query interface customization by default. 若要指定是否要应用查询界面自定义,请使用GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode)方法重载。To specify whether to apply query interface customization, use the GetComInterfaceForObject(Object, Type, CustomQueryInterfaceMode) method overload.

有关其他信息,请参阅COM 可调用包装器运行时可调用包装器,并类接口简介主题。For additional information, see the COM Callable Wrapper, Runtime Callable Wrapper, and Introducing the Class Interface topics.

安全性

SecurityCriticalAttribute
直接调用方需要完全信任。requires full trust for the immediate caller. 不能由部分受信任的或透明的代码使用此成员。This member cannot be used by partially trusted or transparent code.

适用于