简体   繁体   English

如何从 C++ Z5884E40D596370BE5406F2711AD9E3 动态加载 C# dll

[英]How to dynamically load a C# dll from a C++ DLL

I have a C++ application.我有一个 C++ 应用程序。 This supports users' C++ plugin DLL's, it will dynamically load these DLL's and then be able to create and use the user's types dynamically.这支持用户的 C++ 插件 DLL,它将动态加载这些 DLL,然后能够动态创建和使用用户的类型。 These user types derive from base types and interfaces defined in the main application's core library, so I hold user's objects as pointers to the base class and call the user's virtual functions to make their magic happen.这些用户类型派生自主应用程序核心库中定义的基本类型和接口,因此我将用户对象作为指向基本 class 的指针,并调用用户的虚拟函数来实现它们的魔力。

Now I want to extend the plugin DLL's to allow managed DLL's (I care about C# mostly).现在我想扩展插件 DLL 以允许托管 DLL(我主要关心 C#)。 I want all of the same magic to happen in C# plugin DLL's.我希望在 C# 插件 DLL 中发生所有相同的魔法。

How can I dynamically load these dll's, some how I think win32's LoadLibrary which I am currently using is going to be happy with a managed DLL.我如何动态加载这些 dll,我认为我目前正在使用的 win32 的 LoadLibrary 会对托管 DLL 感到满意。 I will not have access to these libraries at compile/link time, they come from the user.我将无法在编译/链接时访问这些库,它们来自用户。

After I get the library loaded, unfortunately I suspect COM in my future as the way to call the derived functions.加载库后,不幸的是,我怀疑 COM 在我的未来是调用派生函数的方式。 Possibly I could use the CLI/C++ wrapper I have been reading about but I am very inexperienced here and would appreciate any advice or links to appropriate articles.可能我可以使用我一直在阅读的 CLI/C++ 包装器,但我在这里非常缺乏经验,如果有任何建议或链接到适当的文章,我将不胜感激。

What you'd do is basically start up an instance of the CLR in your process.你要做的基本上是在你的进程中启动一个 CLR 实例。 Have a look at this article on CLR hosting看看这篇关于CLR 托管的文章

Another way of doing this would be creating a C++/CLI project that hosts your C# classes and use it as a bridge in your C++ project.另一种方法是创建一个 C++/CLI 项目来托管您的 C# 类,并将其用作 C++ 项目中的桥梁。

A few more links to this approach:此方法的更多链接:

The latest link has simple source code for the bridge最新链接有桥的简单源代码

Here are some slides that describe my solution.这是一些描述我的解决方案的幻灯片。

https://docs.google.com/presentation/pub?id=1YoJRGnveh4By7ym4GL19L7OzYOFORZQB6RgJEPVfFb8&start=false&loop=false&delayms=3000 https://docs.google.com/presentation/pub?id=1YoJRGnveh4By7ym4GL19L7OzYOFORZQB6RgJEPVfFb8&start=false&loop=false&delayms=3000

My solution was to have a win32 plugin dll, loaded through normal means (LoadLibrary) that links to mixed C++/CLI dll, which links to pure managed C# code.我的解决方案是有一个 win32 插件 dll,通过正常方式(LoadLibrary)加载,链接到混合 C++/CLI dll,它链接到纯托管 C# 代码。 I allowed calls to happen both ways using A LOT of boiler plate, and a double c++/cli bridge pattern based off of the link given by Padu.我允许使用 A LOT 样板和基于 Padu 给出的链接的双 c++/cli 桥接模式以两种方式进行调用。 Details were fairly complex, but the job of the end API user is very easy and that was my goal.细节相当复杂,但最终 API 用户的工作非常简单,这就是我的目标。 The managed plugin object writer simply derives from an object, and everything just works.托管插件 object 编写器只是从 object 派生而来,一切正常。

Essentially I made a pattern that provides "mixed mode pseudo inheritance" Now my c# objects derive from a base class in c++.本质上,我创建了一个提供“混合模式伪继承”的模式现在我的 c# 对象从 c++ 中的基础 class 派生。

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

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