一种安全的方式来传递在C#中的数组或数组[] []对DLL [英] A SAFE way to pass an array or arrays[][] in C# to a DLL
问题描述
我有我想要传递到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);
编辑:我也愿意,有足够的挫折和痛苦,接受涉及编组的解决方案
I am also willing, with enough discouragement and anguish, to accept a solution involving marshaling.
推荐答案
您可以在双数组转换为一个单一的阵列(即压平)。这可以通过保持宽度和高度的变量,并访问指数作为这样来完成:
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屋!