tags:

views:

247

answers:

4

Hi,

I have a C++ Exe in an application directory which contains the DLLs used by it. Now, for some testing purpose I need to modify an existing DLL and use that instead of the original one. But in order to not modify the existing installation I cannot backup the existing DLL and replace it with the modified one or move the existing one elsewhere. I also cannot change the Exe. The 2 DLLs need to exist side by side. The only change should be that the Exe should transparently load the modified DLL which is in some other folder rather than the existing DLL which is in the same folder as the Exe. Is there some elegant way of doing it?

I looked at some MSDN articles but could not find a way of doing this. The solution should work on Windows XP and up.

Thanks, gg

A: 

The only way I know would use LoadLibrary API including the path, but you say you can not change the exe.

lsalamon
A: 

According to MSDN, it will always start by the application directory (unless you modify it with the alternate search order method...) so it seems to be difficult. You can still copy the executable and its other dependencies elsewhere. It is not that elegant though.

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

RedGlyph
A: 

You can hook LoadLibrary() calls for your process from the beginning. When your patched version of LoadLibrary() sees your DLL's it calls original LoadLibrary() with modified DLL's path.Even if you don't use LoadLibrary() call to load your DLLs, Windows CRT does. So this technique must work.

Göktürk Gezer
Can you please explain with an example of how it works, thanks - gg
gg
The way MSalters described is the first way you should try. It is the easiest way to do DLL injection.If it not works (I don't see a reason for this) then you should implement a real API hooking. This is a complex topic to describe. You can get some example codes and utility libraries by searching "Windows API Hooking" topic on the web.
Göktürk Gezer
+1  A: 

Windows will load at most one version of each DLL name per process. If it loads a DLL listed in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs, it won't load a similarly-named DLL later. But in AppInit_DLLs you can list a DLL with an explicit path, overriding the normal LoadLibrary() order.

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

MSalters