LoadLibrary 是否创建不同的实例?
如果我使用 Win32 API LoadLibrary 连续加载同一个 DLL 3 次,它应该返回 3 个不同的句柄,并且每个库中的函数都应该有不同的地址,正确吗? (或者它会做一些“智能”的事情并检测 dll 是否已经为进程加载并仅指向同一个模块?)
If I use the Win32 API LoadLibrary to load the same DLL 3 times in a row it should return 3 distinct handles and the functions in each library should all have different addresses correct? (Or does it do something "smart" and detect if the dll has already been loaded for the process and just point to the same module?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它做了一些聪明的事情。 Windows 为通过 LoadLibrary 加载的每个 DLL 保留一个引用计数。这就是为什么您必须为每个相应的 LoadLibrary 调用调用一次 FreeLibrary。假设您不首先释放 DLL,则每次调用 LoadLibrary 都会给您相同的句柄。
来自 FreeLibrary 的 MSDN 文档:
It does something smart. Windows keeps a reference count for each DLL loaded through LoadLibrary. That's why you have to call FreeLibrary once for each corresponding LoadLibrary call. Assuming you don't free the DLL first, each call to LoadLibrary will give you the same handle.
From the MSDN documentation for FreeLibrary:
如果它们是相同的 DLL,则不会再次加载它。
http://msdn.microsoft.com/en-us/ library/ms684175(VS.85).aspx
"如果指定的模块是尚未为调用进程加载的 DLL,系统将使用 DLL_PROCESS_ATTACH 值调用 DLL 的 DllMain 函数。如果 DllMain 返回 TRUE,则 LoadLibrary如果 DllMain 返回 FALSE,则系统从进程地址空间卸载 DLL,并且 LoadLibrary 返回 NULL。有关详细信息,请参阅 DllMain 中的“备注”部分。
“如果 lpFileName 不包含路径,并且存在多个具有相同基本名称和扩展名的已加载模块,则该函数将返回第一个加载的模块的句柄。”
If they are the same DLL, then it won't load it again.
http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx
"If the specified module is a DLL that is not already loaded for the calling process, the system calls the DLL's DllMain function with the DLL_PROCESS_ATTACH value. If DllMain returns TRUE, LoadLibrary returns a handle to the module. If DllMain returns FALSE, the system unloads the DLL from the process address space and LoadLibrary returns NULL. It is not safe to call LoadLibrary from DllMain. For more information, see the Remarks section in DllMain."
"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."
不,事实并非如此。为了解决这个问题,您可以将 .dll 复制到临时文件(根据加载 .dll 的需要多次),然后在完成后删除这些文件。
No, it doesn't. To get around this, you can copy the .dll to a temporary file (as many times as you need to load the .dll) and then delete the files once you're done.