gpt4 book ai didi

c# - 处理从 C# 服务器返回的 SAFEARRAY

转载 作者:行者123 更新时间:2023-11-30 04:40:21 25 4
gpt4 key购买 nike

我需要将结构(类)数组从 C# 库返回到非托管 C++ 客户端。这是 C# 库中的函数:

[ComVisible(true)]
[Serializable]
public sealed class RetrieverProxy : IRetrieverProxy
{
public IMyRecord[] RetrieveMyRecords(long[] ids)
{
IList<IMyRecord> result = new List<IMyRecord>();
for (int i = 0; i < ids.Count(); i++)
{
result.Add(new MyRecord()
{
// some test data
});
}

return result.ToArray();
}
}

MyRecord 本身包含一个结构数组,这些结构也是 COM 可见的,并且包含一个 double 和一个 DateTime 字段。

我从 regasm 得到了以下包装器:

inline SAFEARRAY * IRetrieverProxy::RetrieveMyRecords (SAFEARRAY * ids) {
SAFEARRAY * _result = 0;
HRESULT _hr = raw_RetrieveMyRecords(ids, &_result);
if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
return _result;
}

我从客户端代码中调用库,如下所示:

SAFEARRAY *pMyRecordsSA;
SAFEARRAY *pIds;
// omitting pIds initialization, because from the library I can see
// that they are ok
pMyRecordsSA = pIRetrieverProxy->RetrieveMyRecords(pIds);

我遇到的问题是如何检索现在存储在 pMyRecordsSA 中的结果。我尝试了以下方法,但没有用:

    IMyRecordPtr pIMyRecords(__uuidof(MyRecord));
HRESULT hr = SafeArrayAccessData(pMyRecordsSA, (void**)&pIMyRecords);

但随后尝试使用 pIMyRecords 指针会导致访问冲突(hr 为 0K)。

有什么想法吗?我真的坚持这一点。

最佳答案

事实证明,我只是需要“另一层间接”。也就是说,指向指针的指针与简单的指针。

IMyRecords** pIMyRecords; 
HRESULT hr = SafeArrayAccessData(pMyRecordsSA, (void**)&pIMyRecords);

这成功了。

关于c# - 处理从 C# 服务器返回的 SAFEARRAY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1544351/

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