Windows: Reordering DLLs for Exe

I have C ++ Exe in an application directory that contains the DLLs used by it. Now, for some testing purposes, I need to modify an existing DLL and use this instead of the original one. But in order not to modify the existing installation, I cannot back up the existing DLL and replace it with the changed one or move the existing one to another location. I also can not change Exe. Two DLLs must exist side by side. The only change should be that Exe should transparently load the modified DLL, which is in another folder, and not in the existing DLL, which is in the same folder as Exe. Is there an elegant way to do this?

I looked through some MSDN articles but couldn't find a way to do this. The solution should work on Windows XP and higher.

+4
source share
4 answers

Windows will load no more than one version of each DLL name for each process. If it loads the DLL specified in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs , it will not load the DLL with the same name later. But in AppInit_DLLs you can list DLLs with an explicit path, overriding the normal order of LoadLibrary ().

Therefore, temporarily put your test DLL in AppInit_DLLs and override any other DLL with the same name.

+2
source

According to MSDN , it always starts with the application directory (unless you change it using the alternative search method) ..), so it seems difficult. You can still copy the executable file and its other dependencies elsewhere. It is not so elegant though.

Or you can run the executable file that you copied elsewhere along with the new DLL from the source directory. According to the search order, it should work too, although I must admit that I have never tried.

+1
source

From the very beginning, you can intercept LoadLibrary () calls for your process. When your patched version of LoadLibrary () sees your DLL, it calls the original LoadLibrary () with the modified DLL pool. Even if you do not use the LoadLibrary () call to load your DLLs, Windows CRT does. Therefore, this method should work.

0
source

The only way I know would be to use the LoadLibrary API, including the path, but you say that you cannot modify the exe.

-one
source

All Articles