Search code examples
windowswinapivisual-c++dllloadlibrary

Can a process load two DLLs with exactly the same name?


Help interpreting MSDN:

Dynamic-Link Library Search Order

...

If a DLL with the same module name is already loaded in memory, the system checks only for redirection and a manifest before resolving to the loaded DLL, no matter which directory it is in. The system does not search for the DLL.

Note: Multiple DLLs with the same name basically is a bad idea, this is just to get a better picture.

Consider:

...\x\foo.exe
...\x\a\bar.dll ~ no further dependencies
...\x\b\bar.dll ~ no further dependencies

Is it possible to load both of these bar.dll into foo.exe with explicit load library calls? And where/how is this documented and supported (otherwise I'd just try it.)

That is, will the following reliably work on Windows7+ :

// Load using full path:
HANDLE a_mod = LoadLibrary(L"...\x\a\bar.dll");
HANDLE b_mod = LoadLibrary(L"...\x\b\bar.dll");
// now use moth DLLs ...

Solution

  • From the documentation (emphasis mine):

    Desktop applications can control the location from which a DLL is loaded by specifying a full path, using DLL redirection, or by using a manifest. If none of these methods are used, the system searches for the DLL at load time as described in this section.

    Before the system searches for a DLL, it checks the following:

    • If a DLL with the same module name is already loaded in memory, the system uses the loaded DLL, no matter which directory it is in. The system does not search for the DLL.

    So, the clause you're worried about doesn't apply when a full path is provided.