C#中如何调用C++写的代码,简单的函数,复杂的类,希望有详细的说明。

2024-12-30 21:14:55
推荐回答(1个)
回答1:

参考:
Native C++

C/C++ code?

class CNative
{
public:
int NativeFunction(int a) {}
}

C++/CLI

C/C++ code?

public ref class CNativeWrapper
{
private:
CNative *m_pNative;
public:
CNativeWrapper() { m_pNative = new CNative; }
int ManagedMethod(int a) { return m_pNative->NativeFunction(a); }
~CNativeWrapper() { delete m_pNative; }
protected:
!CNativeWrapper() { delete m_pNative; }
}

C#

C# code?

class Program
{
int Main()
{
var nativeWrapper = new CNativeWrapper();
nativeWrapper.ManagedMethod();
return 0;
}
}