Load DLL Library with dependencies multiple times on Windows

427 views Asked by At

Imagine the following scenario :

My Program -> lib A -> lib B -> lib C 

I have my library A, which loads library B, which loads library C. I have the source code and can modify library A, but I cannot modify libraries B and C. Library C has an issue : it contains a singleton, that provides the functionality I need (network connection to device). However, if I want to connect to more devices at once, I cannot, since the library was not made for it.

When I spawn multiple processes, each with their own A.DLL handle, everything works (I have concurent connections to multiple devices from one machine). However, I would like to connect to multiple devices from one process, since there needs to be communication between my program and my lib A, and I dont want to solve it via expensive inter-process comms ( although it should be possible,just slow and complicated).

I found out, that Windows loads a DLL multiple times, if is in different places. So I copied my libraries in two directories, libs1 and libs2. I then load libs1/A.DLL and libs2/A.DLL. When I check process explorer, I see that A.DLL was actualy loaded twice, which is great. The main issue I am trying to solve is that when I load the second A.DLL, Windows wont load B.DLL and C.DLL again from the second dir, since they are loaded already. And I need multiple instances of C.DLL.

To load my A.DLL lib from my program, I use LoadLibrary() and SetDllDirectory() to force Windows to look in the libs1/libs2 folders.

1

There are 1 answers

0
273K On

All three A.DLL, B.Dll and C.DLL should be located in a different location. Specify this different location in SetDllDirectory() every time you load A.DLL.