我编写了一个DLL的C#函数,它返回一个指向指向结构的指针数组的字节指针。我的C#代码如下: unsafe public void myCSharpFunc(uint ipaddress,string starttime,string finishtime,ref byte data,ref int count) { byte * bytePointer = stackalloc byte [iCount];
// get data
for (int i = 0; i < iCount; i++)
{
sElementName = utf8Encoding.GetString(results[i].Column.Name);
bElementValue = results[i].Column.Value;
size = (bElementValue.Length);
elmtPointer = Marshal.AllocHGlobal(size);
try
{
// copy array to pointer
Marshal.Copy(bElementValue, 0, elmtPointer, size);
bytePointer[i] = (byte)elmtPointer;
}catch(Exception ex){
Console.WriteLine(("kvs_getTotal() Exception 1 message: " + ex.Message + "; string: " + ex.ToString() +
"; source: " + ex.Source));
}
}
data = bytePointer[0];
count = iCount;
}
data是一个指向指针数组的字节指针。数组的每个指针都指向一个结构。
在C ++ DLL中,我有以下功能: void myCPlusFunc(DWORD ipaddress,BYTE * starttime,BYTE * endtime,BYTE * data,int * count) { byte bData; long iCount = 0L;
//Initialize COM.
HRESULT hr = CoInitialize(NULL);
CString sStTime((char*)starttime);
CString sFnTime((char*)endtime);
IDrvMgr* pDrvMgr = NULL;
hr = CoCreateInstance(__uuidof(CKvsDrvMgr_Total),NULL,CLSCTX_INPROC_SERVER,__uuidof(IDrvMgr),(void**)&pDrvMgr);
pDrvMgr->myCSharpFunc(ipaddress, (LPCWSTR)sStTime, (LPCWSTR)sFnTime, &bData, &iCount);
*count = (int)iCount;
*data = bData;
// 結果の処理
wchar_t strBuf[30];
wsprintf(strBuf,(LPCWSTR)L"*record count =%d",*count);
MessageBox(NULL, (LPCWSTR)strBuf ,(LPCWSTR)L"◇kvs_getTotal() C++ 結果" , MB_OK);
// Uninitialize COM.
CoUninitialize();
return;
}
然后,在C测试程序中如下:
typedef struct {
DWORD ip;
char datetime [DATETIME_LEN + 1];
int num;
} T_TOTAL;
int main(int argc,char * argv []) { T_TOTAL tttest [TEST_SIZE] = { {21539008,“20110712120101”,100}, {21539008,“20110712120102”,200}, {21539008,“20110712120103”,300}, {21539008,“20110712120104”,400}, {21539008,“20110712120105”,500} }; int iCount; BYTE bData;
// insert data
insDataFunc(tttest[0].ip, (BYTE*)tttest[0].datetime, (BYTE*)(&tttest[0]), sizeof(tttest[0]));
insDataFunc(tttest[1].ip, (BYTE*)tttest[1].datetime, (BYTE*)(&tttest[1]), sizeof(tttest[1]));
insDataFunc(tttest[2].ip, (BYTE*)tttest[2].datetime, (BYTE*)(&tttest[2]), sizeof(tttest[2]));
insDataFunc(tttest[3].ip, (BYTE*)tttest[3].datetime, (BYTE*)(&tttest[3]), sizeof(tttest[3]));
insDataFunc(tttest[4].ip, (BYTE*)tttest[4].datetime, (BYTE*)(&tttest[4]), sizeof(tttest[4]));
// search data
myCPlusFunc(tttest[0].ip, (BYTE*)tttest[0].datetime, (BYTE*)tttest[TEST_SIZE-1].datetime, &bData , &iCount);
return 0;
}
调用搜索功能(myCPlusFunc()函数)后,我想检查返回的数据。你能否告诉我操纵指向结构指针数组的字节指针(&amp; bData)的方法。 我还不知道访问指向结构的指针数组的方法。 任何帮助都非常感谢!
答案 0 :(得分:0)
我访问如下:
BYTE* p = &bData;
BYTE* p1 = p;
BYTE* p2 = (BYTE*)p++;
BYTE* p3 = (BYTE*)p++;
T_TOTAL* t1 = (T_TOTAL*)p1;
T_TOTAL* t2 = (T_TOTAL*)p2;
T_TOTAL* t3 = (T_TOTAL*)p3;
但结构t1,t2,t3的数据与用于插入的数据不同。 你有什么评论吗?