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

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

问题描述

我需要在我的C ++代码中集成这个 C#dll
。我想从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: / p>

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。这在这里描述:是可以从C#DLL中导出函数,如VS C ++吗?,但它有点黑客。

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天全站免登陆