QueueHammer QueueHammer - 1 year ago 89
C++ Question

What is a safe way to pass an array of arrays to a DLL in C#?

I have an array of arrays that I want to pass into a DLL. I am running into the error "There is no marshaling support for nested arrays."

I can pass a single array in fine but if I stack them up it fails. I need/want a "safe" way of passing in the array of arrays.

private static extern int PrintStuff(string[][] someStringsInGroups, int numberOfGroups, int[] lengthSetsInGroups);

EDIT: I am also willing, with enough discouragement and anguish, to accept a solution involving marshaling.

Answer Source

You could convert the double array to a single array (i.e. flatten it). This can be done by keeping width and height variables, and accessing the indices as such:

string atXY = someStringsInSingleArray[(y * width) + x];

The array can then be converted as such:

string * array = new string[width * height];

for (unsigned int y = 0; y < height; ++y)
    for (unsigned int x = 0; x < width; ++x)
        array[(y * width) + x] = someStringsInGroups[x][y];

// (pass single array to dll)

delete [] array;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download