我有一个带有这个函数的 C 库,它返回 4 个输出参数:
void __declspec(dllexport) demofun(double a[], double b[], double* output1, double* output2, double res[], double* output3)
我编写了一个 C# 包装器来调用该函数:
namespace cwrapper
{
public sealed class CWRAPPER
{
private CWRAPPER() {}
public static void demofun(double[] a, double[] b, double output1,
double output2, double[] res, double output3)
{ // INPUTS: double[] a, double[] b
// OUTPUTS: double[] res, double output1, double output2
// Arrays a, b and res have the same length
// Debug.Assert(a.length == b.length)
int length = a.Length;
CWRAPPERNative.demofun(a, b, length, ref output1, ref output2,
res, ref output3);
}
}
[SuppressUnmanagedCodeSecurity]
internal sealed class CWRAPPERNative
{
private CWRAPPERNative() {}
[DllImport("my_cwrapper.dll", CallingConvention=CallingConvention.Cdecl,
ExactSpelling=true, SetLastError=false)]
internal static extern void demofun([In] double[] a, [In] double[] b,
int length, ref double output1, ref double output2,
[Out] double[] res, ref double output3);
}
}
CWRAPPERNative.demofun
当我调用该方法时,一切正常。但是,当我调用该CWRAPPER.demofun
方法时,只有double[] res
正确通过。输出参数output1
,output2
和output3
调用后不变。
// ...
// Initializing arrays A and B above here
double[] res = new double[A.Length];
double output1 = 0, output2 = 0, output3 = 0;
// Works partially: output1 to 3 unchanged
CWRAPPER.demofun(A, B, output1, output2, res, output3);
// Works correctly: all outputs are changed
CWRAPPERNative.demofun(A, B, A.Length, ref output1, ref output2, res, ref output3);
我猜我错误地编组了指针参数,但我无法弄清楚修复。有人知道解决方案吗?谢谢!