将数组的数组传递到C#中的DLL的安全方法是什么? [英] What is a safe way to pass an array of arrays to a DLL in C#?
问题描述
我有一个要传递到DLL中的数组的数组。我遇到了错误嵌套数组没有封送支持。
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);
编辑:我也很乐意和痛苦地接受涉及封送处理的解决方案。 / p>
I am also willing, with enough discouragement and anguish, to accept a solution involving marshaling.
推荐答案
您可以将double数组转换为单个数组(即,将其展平)。这可以通过保留宽度和高度变量并按如下方式访问索引来完成:
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;
这篇关于将数组的数组传递到C#中的DLL的安全方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!