Sign in to follow this  

[.net] Extending unmanaged app with managed plugin dll?

This topic is 4344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have an unmanaged application that wasn't written by me, that can load plugin dlls that follows a certain API (it uses LoadLibrary for this).. All of this works fine, and I've written some (unmanaged) plugins.. Now, I wish to write a plugin that makes use of the .NET framework.. I'm using C++/CLI for this.. Everything compiles fine and so on, but when I try to use the functionality of my plugin, I get an error (System.IO.FileNotFoundException), saying it can't find my dll.. I find this wierd, since it's already running my dll? Does anyone have any idea of how to fix this? Did I forget something? Is there a workaround? Is there a better place to ask? (I couldn't really find a forum for .NET things?) All help is greatly appreciated!

Share this post


Link to post
Share on other sites
GetProcAddress is your problem, I imagine. The CLI compiler isn't going to generate the standard DLL header, since there are no exported functions per se. What you need to do is have GetProcAddress return delegates and if there's a way to do that, I sure don't know what it is.

What you could do, I imagine, is write a non-CLI interop layer. This could interact with a COM interface exported from the CLI dll and map its member functions to the plug-in architecture's functions. Properly parameterized, this non-CLI interop DLL could work with any arbitrary CLI plugin.

Share this post


Link to post
Share on other sites
Are you sure about that? I'm pretty sure my plugin is loaded fine.. It crashes when I call a function that makes use of the framework.. It doesn't crash on load/hook-registering.. Umm.. My plugin needs to export like a managed callback object or something like that.. Or rather, the functionality I'm trying to use has to create an object made by me, and to do that, I'm supposed to pass the type of my class to a function, and then get a handle to it.. Things crash on that function call, saying the framework can't find my dll (which contains the class)..

Share this post


Link to post
Share on other sites
well, obviously I don't know how the app connects to the plugin. Probably it goes

hPlugin = LoadLibrary(szPluginPath);
pFuncLoadMe = GetProcAddress(hPlugin, "LoadMe");
pFuncDoIt = GetProcAddress(hPlugin, "DoIt");
pFuncClose = GetProcAddress(hPlugin, "CloseDown");

or something along those lines. LoadLibrary will work on your DLL (since it is a DLL) but the GetProcAddress calls won't (since the DLL doesn't contain machine code). I'd guess the application just checks the returned function pointers for NULL and throws a generic error indicating it can't use the DLL as a plugin.

I don't know exactly what a managed DLL exports. I assume its the standard COM class factory functions and then the .Net runtime goes from there using Reflection. These are not the functions your application's plugin architecture are expecting. Delegates are what turn .Net methods into proper function pointers - addresses that can be used as jump targets. So your challenge is to figure out how to get these delegates into an application that has never before heard of .Net

I suggested an intermediate non-managed DLL that does know about .Net and also knows about the app's plugin system.

Share this post


Link to post
Share on other sites
I don't know if you've already seen this or not:

Converting managed extensions...

I used this to help develope a small plug-in for Milkshape 3D (which is obviously unmanaged). It allows me to run my 3D engine (which is managed) from within Milkshape.

What you're looking for is mixed mode DLLs. Which is basically a combination of managed and unmanaged code, and provides a proxy between the two worlds. However the only caveat is that it has to be done in C++ (that is, I don't think C#/VB.Net can do this since they really don't emit unmanaged code to any great degree).

There should be a some information about all that out there. However, this all depends on your version of .NET that you're using. If you're using .NET 2.0, the rules have changed (and have gotten much simpler).

My plug-in works flawlessly using this, and my strategy is as follows:
Milkshape 3D (Unmanaged)
|
|
PlugIn DLL (Mixed Mode)
|
|
3DEngine (Managed)

Hopefully this will help you get started.

[Edited by - Tape_Worm on January 26, 2006 4:22:33 PM]

Share this post


Link to post
Share on other sites
Hmm.. It is indeed a mixed mode dll, which does export the function (a struct containing a list of functions accutually).. It works that far.. I wrote another function in the same file, which didn't contain any unmanaged code, and it ran fine.. Then, I added a System::String, and converted that to a C-string, which I then returned.. Still, everything works just fine..

I'll admitt what I'm trying to do is very silly, but I still want to do it.. I want Apache2 to run an ASP.NET page, without using mono, and I can't use IIS.. Now, there is functionality in the .NET framework, which runs an ASP.NET script, and places the output in a file or a string.. I think I got the code right already.. However, the problem is that System.Web.dll can't find the dll file containing a class it needs.. So I thought "ahh, must be because it's a mixed mode dll!", and then I moved my class into a new, fully managed dll, and added a reference to it.. Still, I get a System.IO.FileNotFoundException, saying something like "System.Web.Dll can't load MyAspNetClass or one of it's dependries: Filen kan inte hittas" (last part is swedish for file not found).. So, I tried to put my newly created dll in every folder I had on my drive, and still I got the same message.. So.. Any ideas? And again, if you know a better place to ask, please tell me, cause this is the only .NET releated forum I could find..

Share this post


Link to post
Share on other sites
Quote:
Original post by DvDmanDT
Hmm.. It is indeed a mixed mode dll, which does export the function (a struct containing a list of functions accutually).. It works that far.. I wrote another function in the same file, which didn't contain any unmanaged code, and it ran fine.. Then, I added a System::String, and converted that to a C-string, which I then returned.. Still, everything works just fine..

I'll admitt what I'm trying to do is very silly, but I still want to do it.. I want Apache2 to run an ASP.NET page, without using mono, and I can't use IIS.. Now, there is functionality in the .NET framework, which runs an ASP.NET script, and places the output in a file or a string.. I think I got the code right already.. However, the problem is that System.Web.dll can't find the dll file containing a class it needs.. So I thought "ahh, must be because it's a mixed mode dll!", and then I moved my class into a new, fully managed dll, and added a reference to it.. Still, I get a System.IO.FileNotFoundException, saying something like "System.Web.Dll can't load MyAspNetClass or one of it's dependries: Filen kan inte hittas" (last part is swedish for file not found).. So, I tried to put my newly created dll in every folder I had on my drive, and still I got the same message.. So.. Any ideas? And again, if you know a better place to ask, please tell me, cause this is the only .NET releated forum I could find..


Are you referencing your dll in a round-about way (e.g. binding it dynamically)?

I've only ever gotten that message when I was trying to load assemblies dynamically and once when I screwed something up with the GAC.

In regards to apache running ASP.NET, I managed to get that going by downloading a module from the apache website (or link from there, I can't remember) without requiring Mono. The only caveat was that I couldn't connect to the web server through the IDE like you can with IIS (and I couldn't get web services going either, but I didn't work too hard at that). And I think it was limited to ASP.NET 1.0 or 1.1. But if anything, there's possibly source code available with that plug-in and you might be able to compare it to what you're doing.



Share this post


Link to post
Share on other sites

This topic is 4344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this