2009-10-09 17 views
1

我需要從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)。

任何想法?我真的堅持這一點。

回答

2

事實證明,我只是需要「另一層間接」。也就是說,指向一個簡單指針的指針。

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

這沒有把戲。