What is a safe way to pass an array of arrays to a

2019-05-16 12:42发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-05-16 13:23

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.

查看更多
何必那么认真
3楼-- · 2019-05-16 13:42

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;
查看更多
登录 后发表回答