我需要從C#庫中將結構(類)的數組返回給非託管C++客戶端。這是在C#庫函數:處理從C#服務器返回的SAFEARRAY
[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可見和含有一個雙鍵和一個日期時間字段的陣列。
我從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)。
任何想法?我真的堅持這一點。