开发者

Pointer of array of pointers that point to structures

开发者 https://www.devze.com 2023-03-19 09:40 出处:网络
I wrote a C# function of DLL that returns a byte pointer that points to an array of pointers that point to structures. My C# code as below:

I wrote a C# function of DLL that returns a byte pointer that points to an array of pointers that point to structures. My C# code as below: 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 is a byte pointer that points to an array of pointers. Each pointer of the array points to a structure.

In C++ DLL, I have the following function: 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;

}

Then, in C test program like below:

define DATETIME_LEN 14

define TEST_SIZE 5

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;

}

After calling the search function (myCPlusFunc() function), I want to check the data returned. Would you please tell me the way to manipulate the byte pointer (&bData) that points to array of pointers that point to structures. I haven't known the way to access the array of pointers that point to structures yet. Any help is highly appreciated!


I acess like below:

    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;

but the data of structure t1, t2, t3 is not the same with data used for inserting. Do you have any comment why ?

0

精彩评论

暂无评论...
验证码 换一张
取 消