问题描述
我遵循以下教程https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper/,使用包装程序类从C#.NET框架控制台应用程序创建C ++静态库的实例。在本教程中,ManagedObject.h文件为从非托管到托管的包装器类创建模板。我将如何创建一个从托管到非托管的模板-如果不可能,那么任何创建包装类的链接都可以从C#DLL转换为C ++应用程序使用!
解决方法
就像您提到的链接一样,似乎唯一的方法是创建3个项目。
首先,创建一个C#类库项目并创建一些公共库,例如:
Commands.cs:
using System;
public static class Commands
{
public static void PrintMsg() => Console.WriteLine("Hello");
}
然后,创建一个C ++ / CLI项目(包装项目)。
在包装项目中,创建一些函数以使用您的类并导出它们(我不确定是否需要extern "C"
,但我会使用它)。
wrapper.cpp:
extern "C" __declspec(dllexport) void printMsg()
{
Commands::PrintMsg();
}
在您的C ++非托管项目中,引用包装器项目并导入函数(并使用它们)。
main.cpp:
extern "C" __declspec(dllimport) void printMsg();
int main()
{
printMsg();
return 0;
}
输出:您好