Billabong73
Billabong73

Reputation: 1

DLL redirection for multiple static-linked dll's

I am trying to load multiple instances of the same DLL on the VS2010 C++ project (x32 Configuration).

so I have main dll - Parent.dll that composite with some others kids.dll (kid1.dll, kid2.dll, etc..., kid8.dll) static-linked - when I load the Parent.dll, the kids.dll's are automatically loaded (they located in the same path so they can be loaded successfully).

in my code, I use LoadLibraryEx(Path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) to load the Parent.dll instances. my problem occurs when I try to create multiple instances of Parent.dll: I make a copy of all the DLL's into another path, renamed the Parent name(Parnet_1.dll for example), about the others DLL's - they can't be renamed cause they are static-linked. (i can't access the Parnet.dll source code and change it to dynamic-load).

With 1 instance - The Parent.dll and all his kids are loading successfully. when I'm trying to load another instance, Parent_1.dll, the Parent_1.dll loaded successfully but the other kids DLLs are not loaded: they have the exact same names which are already loaded from the 1st instance - that's what causes my program to crash.

what I am trying to do to is to dynamically load multiple Instance of Parent.dll that each Parent will have his own kids.dll loaded. in other words - recursive redirection

I've been searching all over the internet and couldn't find a solution for my case. I saw there this article: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-redirection and tried the MyCode.exe.local and it didn't work.

I also tried DLL redirection using manifest but couldn't understand how to do it correctly.

Thanks for any effort to help!

Upvotes: 0

Views: 257

Answers (1)

SoronelHaetir
SoronelHaetir

Reputation: 15164

As long as the files are the same this will not work.

A DLL is shared across the process, if a DLL has already been loaded a new call to LoadLibrary will only ever return a reference to that already-loaded module. It would defeat the point of DLLs to do differently.

If you absolutely must have different loads you could rename the files (although this would quickly become unwieldy( and even then I am not absolutely certain it would work (libraries have a name encoded as part of the binary and I am not sure that windows will continue loading if a new file is LoadLibrary'd and that internal name matches the name of an already loaded library).

If you can work it so that parent1.dll gets unloaded (along with all children) before parent2.dll is loaded then none of the above should come into play.

Upvotes: -1

Related Questions