简体   繁体   中英

Wrapping C++ reference parameter in C# when using DllImport

I am trying to wrap a C++ function that has a reference parameter with C# code.
My C# wrapper class has

[DllImport(TestCppDLL.dll)]
public static extern void foo(out int a, out int b, out double c);

public void main()
{
    int a;
    int b;
    double c;

    this.foo(out a, out b, out c);
    Console.WriteLine(a + b + c);
}

And my C++ code is

extern void foo(int &a, int &b, double &c)
{
     a = 1;
     b = 2;
     c = 3;
}

So I expect the output to be "123" but I get "000".
How do I wrap C++ reference parameter?

Thank you in advance,

Your C++ code returns a double but your C# code declares the function as having void return value.

You also may have a calling convention mismatch. C++ default is cdecl, C# default is stdcall.

Otherwise it's fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM