2

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?

main--
  • 3,873
  • 1
  • 16
  • 37

1 Answers1

2

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • This is just an overly simplified example, actually I don't know anything about the directory structure except the fact that the native executables and the .NET assemblies are in different directories. How would I determine the directory in the AssemblyResolve event? – main-- Apr 19 '13 at 16:59
  • @main-- You'd need to know where the .NET assembly is - *something* needs to provide that location, and if it's not in the normal paths, you need to be that something. – Reed Copsey Apr 19 '13 at 17:01