从C ++到C#整理数组的数组 [英] Marshalling an array of arrays from C++ to C#
问题描述
好的,我已经从一些C ++代码回调到了一些C#代码,以处理数据数组和数据结构.
在C ++中,回调声明为:
ok, i''ve got a callback from some C++ code to some C# code to handle an array of data, and a data structure.
In C++ the callback is declared as:
int MyCallBack(const MyStruct* pMyStruct, // pointer to a struct
unsigned short* pMyData, // array of ushorts
unsigned int nMyDataLength, // length of the previous array
void* pInfo); // some extra data (not relevant to the question)
我声明委托以如下方式在C#中处理此问题:
I declare the delegate to handle this in C# as follows:
public delegate int MyCallBack(
ref MyStruct myStruct,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] ushort[] myData,
uint myDataLength,
IntPtr userInfoStruct);
到目前为止一切正常.
但是,现在我想在一次调用中将多个数据数组传递回C#,以避免某些本机管理的开销
我在C ++中对多个回调的声明如下:
This all works fine so far.
However now i want to pass multiple arrays of data back to the C# in a single call to avoid some of the native->managed overhead
My declaration in C++ for the multiple callbacks is as follows:
int MyCallBack2(const MyStruct* pMyStruct, // array of structs
unsigned short** pMyDataArray, // array of ushort arrays
unsigned int* nMyDataLengthArray, // array of lengths
// corresponding to each of the ushort arrays
unsigned int dataCount, // the count of elements in
// the arrays above
void* pInfo); // some extra data (not relevant to the question)
我在弄清楚C#中此回调的委托声明时遇到了一些麻烦
这是我到目前为止所得到的:
I''m having some trouble figuring out the declaration for the delegate for this callback in the C#
This is what I''ve got so far:
public delegate int MyCallBack2(
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] MyStruct[] myStructArray,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] IntPtr[] myDataArray,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] int[] myDataLengthArray,
uint myDataCount,
IntPtr userInfoStruct);
谁能告诉我如何从上述myDataArray中的IntPtrs中获取每个ushort []数组?
谢谢,
Pete
Can anyone tell me how I get each of the ushort[] arrays from the IntPtrs in myDataArray above?
Thanks,
Pete
推荐答案
好,
我似乎有解决办法.
对上面的MyCallBack2
使用声明:
Ok,
I seem to have a solution.
Using the declaration forMyCallBack2
above:
public ushort ShortToUShort(short val)
{
return (ushort)val;
}
public int MyCallBack2(MyStruct[] myStructArray,
IntPtr[] myDataArray,
uint[] myDataLengthArray,
uint myDataCount,
IntPtr userInfoStruct)
{
for (int i = 0; i < myDataCount; ++i)
{
short[] tmpMyData = new short[myDataLengthArray[i]];
Marshal.Copy(myStructArray[i], tmpMyData, 0, (int)myDataLengthArray[i]);
ushort[] myData = Array.ConvertAll<short, ushort>(tmpMyData,
new Converter<short, ushort>(ShortToUShort));
MyCallBack(ref myStructArray[i],
myData,
myDataLengthArray[i],
userInfoStruct);
}
return 1;
}
这似乎可以运行,但是将short []转换为ushort []看起来很混乱,必须有一种更干净的方法来实现.
同样,当我在调试器中运行此程序时,Visual Studio在停止调试时也会挂在我身上.
This appears to run ok, however it looks messy converting the short[] to a ushort[], there has to be a cleaner way to do this.
Also when i run this in the debugger, Visual Studio hangs on me when I stop debugging.
这篇关于从C ++到C#整理数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!