Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
like image 850
Martin Ba Avatar asked Apr 04 '17 20:04

Martin Ba


People also ask

Can you load a DLL twice?

You can not load the same DLL multiple times into a single process (or not and have any effect). If you make the DLL a COM host and use COM objects then this will be automatically handled by each class instance.

How does DLL loading work?

Every process that loads the DLL maps it into its virtual address space. After the process loads the DLL into its virtual address, it can call the exported DLL functions. The system maintains a per-process reference count for each DLL. When a thread loads the DLL, the reference count is incremented by one.

Can a DLL load a DLL?

Threads in DllMain hold the loader lock so no additional DLLs can be dynamically loaded or initialized. The entry-point function should perform only simple initialization or termination tasks.

Are DLLs shared?

DLLs, or dynamic link libraries, are like the building blocks of Windows. Shared DLLs are basically DLL files that are shared between many different apps. Additionally, there are many common DLLs that are used by most, if not all, Windows applications.


1 Answers

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.

like image 112
Harry Johnston Avatar answered Sep 30 '22 02:09

Harry Johnston