Your C++/CLI library could handle AppDomain.AssemblyResolve to properly specify the location of the C# assembly. This event is fired by the CLR when it fails to find the assembly at runtime, and gives you a chance to load the assembly yourself.
Loading other .NET libraries from a C++/CLI library called by native code
-
11-04-2022 - |
Question
So I have three parts:
- Lib: A C# library, compiled for AnyCPU.
- BridgeLib: A C++/CLI library, compiled for x86. It exports a native C function called
bridgeEntry
and calls methods in Lib. - NativeExe: A native C++ Win32 application that simply loads BridgeLib.dll and executes
bridgeEntry
.
Now I put them all into one directory and as expected, code from Lib is executed when I run NativeExe.
However, if I rearrange the directory structure like this:
├───exe
│ NativeExe.exe
│
└───libs
BridgeLib.dll
Lib.dll
things are a little bit different. Obviously running NativeExe.exe
in the exe
folder fails because it can't find BridgeLib.dll
. But that can be resolved by going to the libs
folder and running ..\exe\NativeExe.exe
. Now the application loads BridgeLib.dll
and jumps to bridgeEntry
. But now the CLR crashes with a FileNotFoundException
because it's looking for Lib.dll
in the executable's folder (exe
) and not in the bridge library's folder libs
.
This is just an overly simplified example and I can't change the directory structure. But how else could I solve this problem?
Solution