-1

我按照以下教程https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper/从 C# .NET 框架控制台创建 C++ 静态库的实例使用包装类的应用程序。在本教程中,ManagedObject.h 文件为从非托管到托管的包装类创建了一个模板。我将如何创建一个从托管到非托管的模板 - 如果这是不可能的,任何创建包装类以从 C# DLL 转到 C++ 应用程序使用的链接将不胜感激!

4

1 回答 1

0

就像您提到的链接一样,似乎唯一的方法是创建 3 个项目。

首先,创建一个 C# 类库项目并创建一些公共库,例如:

命令.cs:

using System;
public static class Commands
{
    public static void PrintMsg() => Console.WriteLine("Hello");
}

然后,创建一个 C++/CLI 项目(包装器项目)。在包装器项目中,创建一些函数来使用您的类并导出它们(我不确定是否extern "C"需要,但我会使用它)。

包装器.cpp:

extern "C" __declspec(dllexport) void printMsg()
{
    Commands::PrintMsg();
}

在您的 C++ 非托管项目中,引用您的包装器项目并导入函数(并使用它们)。

主.cpp:

extern "C" __declspec(dllimport) void printMsg();

int main()
{
    printMsg();
    return 0;
}

输出:你好

于 2020-08-14T23:40:52.587 回答