加载一个 DLL,该 DLL 调用加载它的 DLL 中的函数
我有一个将由程序加载的 DLL,该 DLL 将依次加载另一个 DLL 来处理一些不应该在主 DLL 中的内容。但是,第二个 DLL 需要能够与第一个 DLL 通信。 DLL 是否可以使用加载它的 DLL 中的导出函数?例如,
- 程序使用 LoadLibrary 加载 DLL A。
- DLL A 使用 LoadLibrary 加载 DLL B。
- DLL A 使用 GetProcAddress 调用 DLL B 中的一些函数。
- B 反过来对 DLL A 执行 GetProcAddress 并调用一些函数。
- DLL A 与主程序密切相关,而 DLL B 正在执行操作并调用 DLL A 的导出函数。
这会起作用吗?这是正确的方法吗?
I have a DLL that will be loaded by a program, and that DLL will in turn load another DLL to handle some stuff that ought not to be in the main DLL. However, the second DLL needs to be able to talk to the first DLL. Is it possible for a DLL to use exported functions in the DLL that loaded it? For instance
- Program loads DLL A with LoadLibrary.
- DLL A loads DLL B with LoadLibrary.
- DLL A calls some functions in DLL B with GetProcAddress.
- B in turn does GetProcAddress on DLL A and calls some functions.
- DLL A is hobnobbing with the main program as DLL B is doing things and calling DLL A's exported functions.
Will this work, and is it the proper way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它会起作用。 DLL B 可以安全地调用 DLL A 上的 LoadLibrary,以获取其所需函数 GetProcAddress 调用的实例句柄。模块加载器会发现 DLL A 已经在进程内存中,并且只会将其实例句柄返回给 DLL B。
注意:但是,当主程序依赖时,版本控制有一些特定的警告并加载 DLL A 的一个版本,而 DLL B 需要不同的版本。如果是这种情况,DLL B 必须使用其所需的 DLL A 版本的显式路径调用 LoadLibrary,并且 DLL A 必须启用 SxS 以支持在进程内存中加载它的两个版本。
避免这种情况的最简单方法是,如果您同时控制进程和 DLL B,则始终确保它们需要/使用相同版本的 DLL A。
Yes, it will work. DLL B can safely call LoadLibrary on DLL A to get the instance handle for the GetProcAddress call the function it needs. The module loader will figure out that DLL A is already in the process memory and will just return an instance handle for it to DLL B.
Note: there are specific caveats with versioning though, when the main program depends and loads one version of the DLL A, and DLL B needs a different version. If that is the case, DLL B has to call LoadLibrary with explicit path to the version of DLL A it needs, and DLL A has to be SxS-enabled to support loading two versions of it in the process memory.
The easiest way to avoid this would if you control both the process and DLL B, to always ensure they need/use the same version of DLL A.
这不正是Containers的任务吗?还有更多要说的是,让你的依赖关系清晰,提取共享功能并将其移动到另一个 DLL,不要在依赖关系图中形成循环
Isn't it the exact task of Containers? and more to say that make your dependencies clear, extract the shared functionality and move it to another DLL, don't make a loop in dependencies graph
是的,它会起作用(我在几年前开发的商业产品中也遇到过类似的情况)。然而,这是一场半噩梦。我建议修改您的库结构以避免循环依赖。回调或创建另一个库来在其他两个库之间进行编组都可以,并且会让您以后的生活变得更加轻松。版本控制问题只是冰山一角;调试具有循环依赖的架构中的问题可能会让您头晕。
Yes, it will work (I had a similar situation in a commercial product I worked on years ago). However, it IS a nightmare and a half. I would suggest modifying your library structure to avoid the circular dependency. Callbacks or creating another library to marshal between the other 2 both would work and will make your life much easier down the road. Versioning issues are just the tip of the iceberg; debugging issues in an architecture with a circular dependency can make your head spin.