System.Byte[] 是您可能正在寻找的。p>
抱歉没有看到您有 BYTE* ...[]。
一些代码
extern "C" UNMANAGEDCPP_API int fnUnmanagedCpp(BYTE* test[], int nRows, int nCols)
{
//do stuff
std::cout << "called!" << std::endl;
for ( int i = 0; i < nRows; ++i )
{
for ( int j = 0; j < nCols; ++j )
{
std::cout << int ( test[i][j] ) << std::endl;
}
}
test[0][0] = 23;
return 0;
}
在 C# 中:
[DllImport("UnmanagedCpp.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int fnUnmanagedCpp(IntPtr[] buffer, int nRows, int nCols );
public static IntPtr[] Marshall2DArray(byte[][] inArray)
{
IntPtr[] rows = new IntPtr[inArray.Length];
for ( int i = 0; i < inArray.Length; ++i )
{
rows[i] = Marshal.AllocHGlobal(inArray[i].Length * Marshal.SizeOf(typeof(byte)));
Marshal.Copy( inArray[i], 0, rows[i], inArray[i].Length );
}
return rows;
}
public static void Copy2DArray( IntPtr[] inArray, byte[][] outArray )
{
Debug.Assert(inArray.Length == outArray.Length);
int nRows = Math.Min( inArray.Length, outArray.Length );
for (int i = 0; i < nRows; ++i)
{
Marshal.Copy(inArray[i], outArray[i], 0, outArray[i].Length);
}
}
public static void Free2DArray(IntPtr[] inArray)
{
for (int i = 0; i < inArray.Length; ++i)
{
Marshal.FreeHGlobal(inArray[i]);
}
}
static void Main(string[] args)
{
byte[][] bTest = new byte[2][] { new byte[2] { 1, 2 }, new byte[2] { 3, 4 } };
IntPtr[] inArray = Marshall2DArray(bTest);
fnUnmanagedCpp(inArray, 2, 2);
Copy2DArray(inArray, bTest);
Free2DArray(inArray);
System.Console.WriteLine(bTest[0][0]);
}
我希望这会有所帮助,也许还有另一种更好/更简单的方法可以做到这一点。请注意,代码仅用于“说明”,可能包含错误。
基本上一个传入 IntPtrs 数组并手动编组...