C++ DLL 可以调用/加载另一个 DLL 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4370563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:15:05  来源:igfitidea点击:

Can a DLL call/load another DLL?

c++windowsdll

提问by samulisoderlund

I want to call some third party DLL routines from my own DLL. I'm just not sure how to do that in C++.

我想从我自己的 DLL 中调用一些第三方 DLL 例程。我只是不确定如何在 C++ 中做到这一点。

回答by Kirill V. Lyadvinsky

You can use load-time dynamic linkingor run-time dynamic linkingin your DLL in the same way as in the executable. The only restriction is not to call LoadLibraryfrom your DllMainfunction to avoid deadlocks.

您可以在 DLL 中以与在可执行文件中相同的方式使用加载时动态链接运行时动态链接。唯一的限制是不要LoadLibrary从您的DllMain函数中调用以避免死锁。

回答by Goz

LoadLibrary and GetProcAddress are, but one of, your friends ...

LoadLibrary 和 GetProcAddress 只是您的朋友之一...

回答by Abyx

If this dll has .lib file, you just add it to linker input and import its functions statically. If it don't, there are some tools to generate .lib file from .dll.

如果此 dll 具有 .lib 文件,您只需将其添加到链接器输入并静态导入其函数。如果没有,有一些工具可以从 .dll 生成 .lib 文件。

Also you can import functions dynamically, using LoadLibraryand GetProcAddress.
MSDN says that you can't call LoadLibrary from DllMain. But in most cases nothing bad happens.

您也可以使用LoadLibrary和动态导入函数GetProcAddress
MSDN 说你不能从 DllMain 调用 LoadLibrary。但在大多数情况下,没有什么不好的事情发生。

回答by Steve Townsend

Typically you link to the DLL via an export library in your project. Then the DLL functions can be called by your program, provided the DLL is in the program's path at runtime.

通常,您通过项目中的导出库链接到 DLL。然后 DLL 函数可以被您的程序调用,前提是 DLL 在运行时位于程序的路径中。

It's also possible (but a lot more work) to avoid link-time resolution of the required functions by manually loading the DLL and getting the required function addresses, but that should not be necessary if the third-party DLL supports the usual link-time mechanisms.

通过手动加载 DLL 并获取所需函数地址来避免所需函数的链接时解析也是可能的(但需要做更多的工作),但如果第三方 DLL 支持通常的链接时,则不需要这样做机制。