C# - 将字符串数组转换为 sbyte**(String[] 到 sbyte**)
C# - Converting String array to sbyte** (String[] to sbyte**)
XFunction
是托管 C++ 代码(包装器)的 CLI。
我想在我的 C# 项目中使用 XFunction(int,sbyte**)
并将字符串数组转换为 sbyte**。
sbyte[][] sbytes = new sbyte[7][];
for (int argCounter = 0; argCounter < 7 ; argCounter++)
{
//get the byte array
byte[] bytes = Encoding.ASCII.GetBytes(argument[argCounter]);
//convert it to sbyte array
sbytes[argCounter] = new sbyte[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
sbytes[argCounter][i] = (sbyte)bytes[i];
}
当我打电话时:
XFunction(7,sbytes);
和buid,产生这个错误:
The best overloaded method match for 'XFunction(int, sbyte**)' has
some invalid arguments Argument 2: cannot convert from 'sbyte[][]'
to 'sbyte**'
我该如何解决这个错误???
您需要使用 fixed 获取指向数组的指针并防止垃圾收集重新定位您的变量。
你可能想做这样的事情:
public static unsafe void CallXFunction(int a, sbyte[][] array)
{
var pointerArray = new sbyte*[array.Length];
// Recursive fixing so that whole array get's pinned at same time
// (never use fixed pointer outside of fixed{} statement)
Action<int> fixArray = null;
fixArray = (pos) =>
{
fixed (sbyte* ptr = array[pos])
{
pointerArray[pos] = ptr;
if (pos <= (array.Length - 2))
{
fixArray(pos + 1);
}
else
{
fixed (sbyte** pointer = pointerArray)
{
XFunction(a, pointer);
}
}
}
};
fixArray(0);
}
它解决了:
sbyte[][] sbytes = new sbyte[6][];
for (int argCounter = 0; argCounter < 6 ; argCounter++)
{
get the byte array
byte[] bytes = Encoding.ASCII.GetBytes(argument[argCounter]);
convert it to sbyte array1
sbytes[argCounter] = new sbyte[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
sbytes[argCounter][i] = (sbyte)bytes[i];
}
unsafe
{
fixed (sbyte* junk = &sbytes[0][0])
{
sbyte*[] arrayofptr = new sbyte*[sbytes.Length];
for (int i = 0; i < sbytes.Length; i++)
{
fixed (sbyte* ptr = &sbytes[i][0])
{
arrayofptr[i] = ptr;
}
}
fixed (sbyte** ptrptr = &arrayofptr[0])
{
XFunction(7, ptrptr);
}
}
}
XFunction
是托管 C++ 代码(包装器)的 CLI。
我想在我的 C# 项目中使用 XFunction(int,sbyte**)
并将字符串数组转换为 sbyte**。
sbyte[][] sbytes = new sbyte[7][];
for (int argCounter = 0; argCounter < 7 ; argCounter++)
{
//get the byte array
byte[] bytes = Encoding.ASCII.GetBytes(argument[argCounter]);
//convert it to sbyte array
sbytes[argCounter] = new sbyte[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
sbytes[argCounter][i] = (sbyte)bytes[i];
}
当我打电话时:
XFunction(7,sbytes);
和buid,产生这个错误:
The best overloaded method match for 'XFunction(int, sbyte**)' has some invalid arguments Argument 2: cannot convert from 'sbyte[][]' to 'sbyte**'
我该如何解决这个错误???
您需要使用 fixed 获取指向数组的指针并防止垃圾收集重新定位您的变量。
你可能想做这样的事情:
public static unsafe void CallXFunction(int a, sbyte[][] array)
{
var pointerArray = new sbyte*[array.Length];
// Recursive fixing so that whole array get's pinned at same time
// (never use fixed pointer outside of fixed{} statement)
Action<int> fixArray = null;
fixArray = (pos) =>
{
fixed (sbyte* ptr = array[pos])
{
pointerArray[pos] = ptr;
if (pos <= (array.Length - 2))
{
fixArray(pos + 1);
}
else
{
fixed (sbyte** pointer = pointerArray)
{
XFunction(a, pointer);
}
}
}
};
fixArray(0);
}
它解决了:
sbyte[][] sbytes = new sbyte[6][];
for (int argCounter = 0; argCounter < 6 ; argCounter++)
{
get the byte array
byte[] bytes = Encoding.ASCII.GetBytes(argument[argCounter]);
convert it to sbyte array1
sbytes[argCounter] = new sbyte[bytes.Length];
for (int i = 0; i < bytes.Length; i++)
sbytes[argCounter][i] = (sbyte)bytes[i];
}
unsafe
{
fixed (sbyte* junk = &sbytes[0][0])
{
sbyte*[] arrayofptr = new sbyte*[sbytes.Length];
for (int i = 0; i < sbytes.Length; i++)
{
fixed (sbyte* ptr = &sbytes[i][0])
{
arrayofptr[i] = ptr;
}
}
fixed (sbyte** ptrptr = &arrayofptr[0])
{
XFunction(7, ptrptr);
}
}
}