簡體   English   中英

如何設計一個包裝器 class 來使用 C++ 應用程序以使用 C# Z5884E40D796370BE39AZ109ADF?

[英]How can I design a wrapper class for consuming a C++ application to use a C# DLL?

I followed the following tutorial, https://www.red-gate.com/simple-talk/dotnet/net-development/creating-ccli-wrapper/ , to create an instance of a C++ static library from C# .NET framework console application使用包裝器 class。 在本教程中,ManagedObject.h 文件為從非托管到托管的包裝類創建了一個模板。 How would I create a template to go from managed to unmanaged--if this isn't possible, any links to create a wrapper class to go from a C# DLL to be used by a C++ application would be much appreciated!

就像您提到的鏈接一樣,似乎唯一的方法是創建 3 個項目。

首先,創建一個 C# Class 庫項目並創建一些公共庫,例如:

命令.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;
}

Output:你好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM