C++托管代码不需要导出函数声明 __declspec(dllexport)
// Service.cpp
using namespace System; //引用名称空间
namespace MsgLib //声明命名空间
{
public ref class Service //声明托管类
{
public:
String ^SendMsg(String ^msg) //函数成员
{
return msg;
}
};
}
编译:
#VS2015 x86 本机工具命令提示符
cl /clr /LDd Service.cpp
测试,新建C#控制台工程,添加引用 Service.dll
namespace CLRDemo
{
class Program
{
static void Main(string[] args)
{
var service = new Service(); // 托管C++ 对象
Console.WriteLine(service.SendMsg("Hello World"));
Console.ReadKey();
}
}
}