3

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.

progyammer
  • 1,498
  • 3
  • 17
  • 29
QueueHammer
  • 10,515
  • 12
  • 67
  • 91

2 Answers2

1

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;
Jwosty
  • 3,497
  • 2
  • 22
  • 50
Rantaak
  • 188
  • 1
  • 8
  • To generalize that idea, you can put your 2D array into any serializable form - like a 1D array, or even a string. – Matt Ball Nov 13 '09 at 22:29
  • I have serialized some data in a int array before and even did some strut casting on the C++ side so you could read what your were assigning the data to (o[i].value v.s. o[i][7]). When a string is passed into a DLL it is marshaled with a Null on the end of it. If I serialize it my self I will have to add those in. Not a big deal, but the code as it is makes the techs around me squirm. The other requirement is that all code be "safe" in C#. Another solution would be to actually martial the data manully :( – QueueHammer Nov 13 '09 at 23:06
0

I just stumbled across what may not be as safe but a lot faster since only the pointers are getting allocated.

http://www.mycsharp.de/wbb2/thread.php?threadid=82380

It is in German, but the code at the end of the page is ready for copy/paste. In my case I just made the class generic to not only support doubles.

Mind you, I do not know about string[][] since I would assume string to be marshalled to char*, so you would have char[][]* rather than double[][] as in the example.

Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104