我们可以在一个进程中加载 2 个同名的 DLL吗?
我说的是 win32 dll,那些普通的 pe 文件。与我在 explorer.exe 进程中看到的相比进行测试后,我感到很困惑。
我用以下模块编写了一个测试:(C++)
DLLLoader.exe 链接到同一文件夹中的 A.dll。
B.dll 链接到另一个文件夹中的 A.dll(2)。 (A.dll(2) 是与 A.dll 完全不同的 DLL,但名称相同)
DLLLoader.exe 将通过 ::LoadLibrary 显式加载 B.dll。
现在我启动DllLoader.exe,首先,A.dll将被加载,但是当它尝试加载B.dll时,它失败了:我怀疑这是因为B.dll认为A.dll已经在进程中加载,但是事实上,加载的不是B.dll想要的,导入/导出表不匹配,所以B.dll加载失败。
这似乎告诉我们,我们不能在同一进程中加载两个同名的 dll,即使它们的路径不同。但是当我使用进程资源管理器监视 Windows 的 explorer.exe 进程中加载的模块时,我可以看到以下 2 个 dll 正在加载,且名称相同:
comctl32.dll 用户体验控制库 C:\WINDOWS\WinSxS...\comctl32.dll
comctl32.dll 通用控件库 C:\WINDOWS\system32\comctl32.dll
你们中有人能解释一下吗?
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.
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.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它基本上取决于您是否使用完整路径或仅通过文件名加载 dll。 LoadLibraryEx 文档很好地介绍了这一点:
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:
请参阅 http://social .msdn.microsoft.com/Forums/en-US/vcgeneral/thread/b3eaa07f-7f92-4693-8aa1-b8fee0b92d2f/ 很好地讨论了如何通过激活上下文为 WinXP 及更高版本隐式完成此操作(清单)来控制加载。
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.