【发布时间】:2012-03-01 18:34:20
【问题描述】:
好的,基本上有一个大型 C++ 项目 (Recast) 我想打包,以便在我的 C# 项目中使用它。
我一直在尝试这样做一段时间,这就是我目前所拥有的。我正在使用 C++/CLI 来包装我需要的类,以便我可以在 C# 中使用它们。
但是,我的 C# 项目中还需要大量结构和枚举。那么我该如何包装这些呢?
我现在使用的基本方法是将 dllexport 调用添加到本机 c++ 代码,编译到 dll/lib,将此库添加到我的 C++/CLI 项目并导入 c++ 头文件,然后将 CLI 项目编译为dll,最后将此 dll 添加为对我的 C# 项目的引用。感谢您的帮助。
这是一些代码。由于 C++ 项目太大,我需要一种易于管理的方式。
//**Native unmanaged C++ code
//**Recast.h
enum rcTimerLabel
{
A,
B,
C
};
extern "C" {
class __declspec(dllexport) rcContext
{
public:
inline rcContect(bool state);
virtual ~rcContect() {}
inline void resetLog() { if(m_logEnabled) doResetLog(); }
protected:
bool m_logEnabled;
}
struct rcConfig
{
int width;
int height;
}
} // end of extern
// **Managed CLI code
// **MyWrappers.h
#include "Recast.h"
namespace Wrappers
{
public ref class MyWrapper
{
private:
rcContect* _NativeClass;
public:
MyWrapper(bool state);
~MyWrapper();
void resetLog();
void enableLog(bool state) {_NativeClass->enableLog(state); }
};
}
//**MyWrapper.cpp
#include "MyWrappers.h"
namespace Wrappers
{
MyWrapper::MyWrapper(bool state)
{
_NativeClass = new rcContext(state);
}
MyWrapper::~MyWrapper()
{
delete _NativeClass;
}
void MyWrapper::resetLog()
{
_NativeClass->resetLog();
}
}
// **C# code
// **Program.cs
namespace recast_cs_test
{
public class Program
{
static void Main()
{
MyWrapper myWrapperTest = new MyWrapper(true);
myWrapperTest.resetLog();
myWrapperTest.enableLog(true);
}
}
}
【问题讨论】:
-
您可能想要更新链接描述 (:
标签: c# c++ dll c++-cli wrapper