简体   繁体   English

在 C++ 代码中使用 C# dll

[英]Using C# dll in C++ code

I need to integrate this C# dll in my C++ code.我需要在我的 C++ 代码中集成这个C# dll I want to call some functions written in C# from dll and the rest of code write in C++.我想从dll调用一些用C#编写的函数,其余的代码用C++编写。 What is the easiest and quickest way to do it?最简单快捷的方法是什么? The program will be executed only on Windows.该程序将仅在 Windows 上执行。

There are basically two cases to call a .NET DLL from unmanaged code:从非托管代码调用 .NET DLL 基本上有两种情况:

  1. The .NET DLL exposes a COM interface. .NET DLL 公开了一个 COM 接口。 In this case, you can use COM from your C++ code.在这种情况下,您可以在 C++ 代码中使用 COM。
  2. The .NET DLL does not expose a COM interface. .NET DLL 不公开 COM 接口。 In this case, you have two possibilities (to make it simple):在这种情况下,您有两种可能性(为了简单起见):

    2.a. 2.a. host the CLR as described here: Loading the Common Language Runtime into a Process按照此处所述托管 CLR: 将公共语言运行时加载到进程中
    2.b. 2.b. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.编写一段托管 C++ 代码(另一个 DLL - 用 C++/CLI 编写)来包装 .NET DLL 并将“旧方式”DLL 导出公开给非托管客户端。

I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...).我不是特别了解sharpbox系统,但它看起来像纯.NET并且不公开COM接口,所以2.b可能是最好的方法(不是那么容易......)。 Maybe it has a REST/Web easier API you could use.也许它有一个你可以使用的 REST/Web 更简单的 API。

PS: you can also add exports to a .NET DLL. PS:您还可以将导出添加到 .NET DLL。 This is described here: Is is possible to export functions from a C# DLL like in VS C++?这在这里描述: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.但它有点hacky。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM