7

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 ...
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • ComCtl32.dll has existed in two versions since WinXP and you will sometimes see both loaded in a process. They exist in two different activation contexts but it is the most common case you will see in the wild. – Anders Apr 05 '17 at 01:32
  • @Anders - ah yes Activation Contexts. The road to horribly documented insanity :-P – Martin Ba Apr 05 '17 at 09:08

1 Answers1

6

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.

Community
  • 1
  • 1
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • 2
    The Remarks section for [LoadLibrary](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx) also has a clause "If *lpFileName* does not include a path and there is more than one loaded module with the same base name and extension, the function returns a handle to the module that was loaded first.", which would be quite meaningless if it were not possible to load multiple DLLs with the same filename. – Quietust Apr 04 '17 at 21:35
  • @Quietust - ah I read that too, but forgot to mention it. It was actually one of the triggers for this question, as it seemed contradictory :-) – Martin Ba Apr 04 '17 at 22:01
  • There you go. The right highlighting can resolve the trickiest issues ;-) – Martin Ba Apr 04 '17 at 22:02