开发者

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

开发者 https://www.devze.com 2023-02-25 10:59 出处:网络
I am trying to wrap a C++ function that has a reference parameter with C# code. My C# wrapper class has

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);
    Con开发者_StackOverflowsole.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消