如何从C#代码中调用具有int*类型参数的C++ DLL中定义的函数?

Ash*_*osh 2 c# c++-cli

我有一个原生的常规C++ Dll,我想从C#代码调用,所以我创建了C++/CLI类(如这里这里所述),它将包含托管C++代码,可以直接调用任何C#代码,哪些可以生成调用inturn到本机非托管C++.

本机C++ dll中的一个函数具有int*类型的参数.如何在包装函数中声明,如何将其转换为int*?

Han*_*ant 5

它是通过引用传递值的C/C++方式.您应该使用refout关键字:

[DllImport("something.dll")]
private static extern void Foo(ref int arg);
Run Code Online (Sandbox Code Playgroud)

在C++/CLI中看起来大致如下:

public ref class Wrapper {
private:
    Unmanaged* impl;
public:
    void Foo(int% arg) { impl->Foo(&arg); }
    // etc..
};
Run Code Online (Sandbox Code Playgroud)