المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Pass a struct array to C++ to get data



C# Programming
12-03-2009, 01:00 PM
Hi, guys, I have a C# struct array and I need to pass it to C++ code and get it filled with necessary data. Here is my C# code:

public struct DataStruct
{
public string dataName;
public int slot;
public int quad;

public DataStruct(string name, int slot, int quadrant)
{
this.dataName= name;
this.slot = slot;
this.quad = quadrant;
}
}

// C++ function
[DllImport("CppCode.dll")]
public static extern int GetDataList([In, Out] DataStruct[] dataList);

// Create the struct array
DataStruct[] dataList = new DataStruct[512];
// Call C++ function
GetDataList(dataList);

C++ code in CppCode.dll:

struct DataStruct
{
char * dataName;
int slot;
int quad;
};

DLLEXPORT int GetDataList(DataStruct* dataList)
{
for (int i = 0; i < 100; i++)
{
dataList[i].dataName = "qqq";
dataList[i].slot = i;
dataList[i].quad = i+1;
}
return 0;
}

Please help to find out if I did something wrong. The most strange thing is that this code works fine in some computers but in other computers, it crashes when return back from C++ code.
Thanks a lot!!