簡體   English   中英

指向結構的指針數組的指針

[英]Pointer of array of pointers that point to structures

我寫了一個 DLL 的 C# function ,它返回一個字節指針,該指針指向指向結構的指針數組。 我的 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,我有以下 function: void myCPlusFunc(DWORD ipaddress, BYTE* starttime, BYTE 長 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 測試程序中,如下所示:

定義 DATETIME_LEN 14

定義 TEST_SIZE 5

typedef struct { DWORD ip; 字符日期時間[DATETIME_LEN+1];
整數; } T_TOTAL;

int main(int argc, char* argv[]) { T_TOTAL tttest[TEST_SIZE] ={ {21539008, "20110712120101", 100}, {21539008, "20110712120102", 200}, {21539008, "201107121},20103" {21539008, "20110712120104", 400}, {21539008, "20110712120105", 500} }; int iCount; 字節 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;

}

調用搜索function(myCPlusFunc()函數)后,想查看返回的數據。 請告訴我如何操作指向指向結構的指針數組的字節指針(&bData)。 我還不知道訪問指向結構的指針數組的方法。 非常感謝任何幫助!

我訪問如下:

    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的數據與用於插入的數據不同。 你有什么評論為什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM