C#中如何调用C++写的代码,简单的函数,复杂的类,希望有详细的说明。 100
1个回答
2016-10-08
展开全部
参考:
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;
}
}
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;
}
}
追问
有源代码工程吗?可以发一份吗。
这个不算详细吧,代码怎么放?在同一给工程中还是用dll,原谅小白。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询