How to dynamically load a C# dll from a C++ DLL
Asked Answered
B

3

7

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.

Berndt answered 1/6, 2011 at 20:41 Comment(0)
C
2

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:

The latest link has simple source code for the bridge

Cavy answered 1/6, 2011 at 21:9 Comment(2)
Would I still need to startup the CLR in the unmanaged process (as per the link in Al3891's response above) to load this Bridge DLL?Berndt
Not really. See the second link in my postCavy
B
2

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

Beau answered 1/6, 2011 at 20:48 Comment(1)
The link is broken now.Septuor
C
2

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:

The latest link has simple source code for the bridge

Cavy answered 1/6, 2011 at 21:9 Comment(2)
Would I still need to startup the CLR in the unmanaged process (as per the link in Al3891's response above) to load this Bridge DLL?Berndt
Not really. See the second link in my postCavy
B
0

Here are some slides that describe my solution.

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. 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. Details were fairly complex, but the job of the end API user is very easy and that was my goal. The managed plugin object writer simply derives from an object, and everything just works.

Essentially I made a pattern that provides "mixed mode pseudo inheritance" Now my c# objects derive from a base class in c++.

Berndt answered 25/4, 2012 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.