1

我有一个带有这个函数的 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,output2output3调用后不变。

// ...
// 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);

我猜我错误地编组了指针参数,但我无法弄清楚修复。有人知道解决方案吗?谢谢!

4

2 回答 2

1

您忘记在 demofun 中通过引用传递值:

public static void demofun(double[] a, double[] b, ref double output1,
   ref double output2, double[] res, ref double output3)

值在方法内部发生变化,但未修改为原始调用者。

于 2012-12-10T04:53:48.997 回答
0

所有double参数CWRAPPER.demofun都是非易失性的,即函数不能更改值。要解决此问题,您需要进行如下更改:

public static void demofun(double[] a, double[] b,
  out double output1, out double output2, double[] res, out double output3)

不过还有一个问题。C 函数的第 5 个参数double res[]是输入参数,但您的 P/Invoke 声明具有[Out] double[] res(输出参数)。我会像这样重写 P/Invoke 声明:

[DllImport("my_cwrapper.dll", CallingConvention=CallingConvention.Cdecl,
  ExactSpelling=true, SetLastError=false)]
static extern void demofun([In] double[] a, [In] double[] b, [In] int length,
  [Out] double output1, [Out] double output2, [In] double[] res, [Out] double output3);

out带参数的示例方法实现;OutOutTester表明带out参数的方法不需要直接赋值:

public static void OutTester(out int a)
{
    a = 1;
}

public static void OutOutTester(out int a, out int b)
{
    OutTester(out a);
    b = 1;
}
于 2012-12-10T05:08:49.837 回答