Question

I have an application environment that is essentially a plug in where:

host application calls into unmanaged c++ dll which calls into managed C++ dll which calls into C# dll

This is a fairly known way of calling into C# from unamanaged C++ by using a bridge of managed C++. This all works well and good under most circumstances except in a plug in type architecture where my dlls(unmanaged C++, managed C++ and C#) are not in the same directory as the application calling into the dll. When the application calls into the unmanaged C++ all is well and good because the app knows the directory to call into to load up that dll. However, the first time the unmanaged C++ calls into the managed C++ we get a FileNotFoundException. It turns out that it is not finding the C# dll(noting that all three dlls are in the same directory, just not in the app directory). If I drop all of the dlls into the runtime directory of the exe then everything works perfectly and we don't get the FileNotFoundException. But when I deploy I will have no control over the calling application and thus cannot drop my dlls in the runtime directory.

So, the question is how can I in the unmanaged C++ code programmatically set the load directory for the C# dll when managed C++ dll loads up? I have tried SetDllDirectory and setting the path variable on the system to no success for the directory where my dlls reside.

Was it helpful?

Solution

The CLR looks in only two places by default for an assembly: the GAC first, the directory from which the EXE was started second. Getting it to look elsewhere requires extra work. One possibility is off the table, you cannot subscribe an event handler for AppDomain.AssemblyResolve in this scenario. Which leaves an app.exe.config file with the <probing> or <codebase> elements. You must give it the same name as the unmanaged EXE and store it in the same folder as the EXE.

This tends to be unwise if the EXE is not yours and doesn't exactly address the goal of keeping stuff out of the EXE directory. The GAC is the simple workaround.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top