Baiyan Huang
Baiyan Huang

Reputation: 6791

Could we have 2 DLLs with the same name being loaded in one process

I am talking about win32 dlls, those plain pe files. I am confused after I doing a test compared to what I saw in explorer.exe process.

  1. I wrote a test with following modules:(C++)
    DLLLoader.exe links to A.dll in the same folder.
    B.dll links to A.dll(2) in another folder. (A.dll(2) is a totally different DLL from A.dll, but with the same name)
    DLLLoader.exe will load B.dll explicitly through ::LoadLibrary.
    Now I start DllLoader.exe, firstly, A.dll will be loaded, but then when it tries to load B.dll, It just failed: I suspect that is because B.dll thinks A.dll is already loaded in process, but in fact, the loaded one is not the one B.dll wanted, the import/export table can't match, so B.dll is failed to load.
    This seems to tell us we can't loaded 2 dlls of same name in the same process, even they are of different path.

  2. But when I used process explorer to monitor loaded modules in Windows's explorer.exe process, I could see following 2 dlls being loaded, with same name:
    comctl32.dll User Experience Controls Library C:\WINDOWS\WinSxS...\comctl32.dll
    comctl32.dll Common Controls Library C:\WINDOWS\system32\comctl32.dll

Could any of you shed some lights on this?

Upvotes: 9

Views: 11332

Answers (2)

puetzk
puetzk

Reputation: 10824

See http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/b3eaa07f-7f92-4693-8aa1-b8fee0b92d2f/ for a good discussion on how this can be done implicitly for WinXP and up, by activation context (manifests) to control the loading.

Upvotes: 7

Mattias S
Mattias S

Reputation: 4808

It basically depens on if you load the dll with its full path or only by file name. The LoadLibraryEx docs cover this pretty well:

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.

Upvotes: 9

Related Questions