I have a C library with this function that returns 4 output parameters:
void __declspec(dllexport) demofun(double a[], double b[], double* output1, double* output2, double res[], double* output3)
And I wrote a C# wrapper to call the function:
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);
}
}
Everything works fine when I call the CWRAPPERNative.demofun method. However, when I call the CWRAPPER.demofun method, only the double[] res is passed through correctly. The output parameters output1, output2 and output3 are unchanged after the call.
// ...
// 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);
I’m guessing that I’m marshalling the pointer arguments wrongly, but I cannot figure out the fix. Anyone knows a solution? Thanks!
You are forgetting to pass the values by reference inside of demofun:
The values is changing inside of the method, but not being modified to the original caller.