可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答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;
回答2:
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.