Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically load a C# dll from a C++ DLL

I have a C++ application. 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. 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.

Now I want to extend the plugin DLL's to allow managed DLL's (I care about C# mostly). I want all of the same magic to happen in C# plugin DLL's.

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. 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. 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.

like image 388
AlwaysTraining Avatar asked Jun 01 '11 20:06

AlwaysTraining


2 Answers

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.

A few more links to this approach:

  • Connecting c++ and c# code with a c++/cli bridge
  • .NET to C++ Bridge

The latest link has simple source code for the bridge

like image 75
Padu Merloti Avatar answered Oct 23 '22 21:10

Padu Merloti


What you'd do is basically start up an instance of the CLR in your process. Have a look at this article on CLR hosting

like image 33
aL3891 Avatar answered Oct 23 '22 21:10

aL3891