在 C++ 代码中使用 C# dll [英] Using C# dll in C++ code

查看:33
本文介绍了在 C++ 代码中使用 C# dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要集成这个 C# dll在我的 C++ 代码中.我想从 dll 调用一些用 C# 编写的函数,其余的代码用 C++ 编写.最简单快捷的方法是什么?该程序将仅在 Windows 上执行.

I need to integrate this C# dll in my C++ code. I want to call some functions written in C# from dll and the rest of code write in C++. What is the easiest and quickest way to do it? The program will be executed only on Windows.

推荐答案

从非托管代码调用 .NET DLL 基本上有两种情况:

There are basically two cases to call a .NET DLL from unmanaged code:

  1. .NET DLL 公开了一个 COM 接口.在这种情况下,您可以在 C++ 代码中使用 COM.
  2. .NET DLL 不公开 COM 接口.在这种情况下,您有两种可能性(为了简单起见):

  1. The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
  2. The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):

2.a.按照此处所述托管 CLR:将公共语言运行时加载到一个过程
2.b.编写一段托管 C++ 代码(另一个 DLL - 用 C++/CLI 编写)来包装 .NET DLL 并将旧方式"DLL 导出公开给非托管客户端.

2.a. host the CLR as described here: Loading the Common Language Runtime into a Process
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.

我不是特别了解sharpbox系统,但它看起来像是纯.NET并且不公开COM接口,所以2.b可能是最好的方法(不是那么容易......).也许它有一个你可以使用的 REST/Web 更简单的 API.

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...). Maybe it has a REST/Web easier API you could use.

PS:您还可以将导出添加到 .NET DLL.这在这里描述:是否可以像在 VS C++ 中一样从 C# DLL 中导出函数? 但它有点 hacky.

PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.

这篇关于在 C++ 代码中使用 C# dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆