gpt4 book ai didi

c# - 如何获取指向接口(interface)的指针?

转载 作者:行者123 更新时间:2023-11-30 16:31:05 25 4
gpt4 key购买 nike

我正在尝试执行此功能:

public static int QueryInterface(
IntPtr pUnk,
ref Guid iid,
out IntPtr ppv
)

在哪里

pUnk
Type: System.IntPtr
The interface to be queried.

基本上,Marshal.QueryInterface 从 COM 对象请求指向指定接口(interface)的指针。我想查询许多接口(interface)(全部来自 IPersist),那么我该如何获取这些接口(interface)的引用指针呢?

注意:IPersistStorage 就是其中之一。

编辑(有效):

// Use the CLSID to instantiate the COM object using interop.
Type type = Type.GetTypeFromCLSID(myGuid);
Object comObj = Activator.CreateInstance(type);

// Return a pointer to the objects IUnknown interface.
IntPtr pIUnk = Marshal.GetIUnknownForObject(comObj);
IntPtr pInterface;
Int32 result = Marshal.QueryInterface(pIUnk, ref myGuid, out pInterface);

最佳答案

阅读 Marshal.QueryInterface() 上备注部分的最后一行页面。

The QueryInterface method exposes the IUnknown::QueryInterface method of a COM object, which attempts to obtain a specific interface pointer. [...] To obtain an IntPtr value that represents a IUnknown interface pointer, you can call Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObject, or Marshal.GetIDispatchForObject.

我相信您正在寻找 Marshal.GetComInterfaceForObject()方法。

关于c# - 如何获取指向接口(interface)的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5088177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com