简体   繁体   English

如何使C ++程序查找DLL?

[英]How to make a C++ program look for DLLs?

I want to be able to store DLLs in sub-folders without the EXE complaining about a missing DLL. 我希望能够将DLL存储在子文件夹中,而EXE不会抱怨缺少DLL。 How do I make the EXE look in a sub-folder, such as "DLLs"? 如何使EXE在子文件夹(例如“ DLL”)中显示? If this has anything to do with it, my IDE is Code::Blocks. 如果与此有关,那么我的IDE是Code :: Blocks。

To make this work with explicit loading (LoadLibrary/GetProcAddress) is easy enough. 要使之与显式加载(LoadLibrary / GetProcAddress)一起工作很容易。 You are in control of the binding process and simply pass the full path to LoadLibrary. 您可以控制绑定过程,只需将完整路径传递给LoadLibrary。

For implicit loading you are at the mercy of the system. 对于隐式加载,您将受到系统的约束。 Whilst you could augment the PATH variable this is extremely draconian. 尽管您可以增加PATH变量,但这却是极端严厉的。 Faced with the choice of putting all DLLs alongside the executable or modifying PATH, I would always choose the former. 面对将所有DLL置于可执行文件旁边或修改PATH的选择,我总是选择前者。

There is also the option of DLL redirection but even Microsoft seem to advise you to place your DLLs alongside your executable rather than use redirection. 也可以选择DLL重定向,但是即使Microsoft似乎也建议您将DLL放在可执行文件旁边,而不要使用重定向。

There are ways to do this via manifests or redirection, but these are pretty complicated. 有多种方法可以通过清单或重定向来实现,但是这些方法非常复杂。

I would recommend that instead, you use a structure as follows: 我建议您改用如下结构:

myapp.exe
DLLs/
    myapp_internal.exe
    mydll1.dll
    mydll2.dll

In the above example your real application is myapp_internal.exe , and it goes in the DLL sub-folder, so that all the DLLs can be located correctly. 在上面的示例中,您的实际应用程序是myapp_internal.exe ,它位于DLL子文件夹中,因此可以正确定位所有DLL。 The myapp.exe binary is just a stub application that executes myapp_internal.exe . myapp.exe二进制文件只是执行myapp_internal.exe的存根应用程序。

I hope this helps! 我希望这有帮助!

I haven't actually tried this, but there's no reason it shouldn't work (famous last words). 我实际上没有尝试过,但是没有理由不起作用(著名的遗言)。

  1. Turn on delay loading for your DLLs. 打开延迟加载您的DLL。 This is done via a linker option or compiler #pragma. 这是通过链接器选项或编译器#pragma完成的。
  2. Early in your code (before any DLL function is called), modify the process' copy of the PATH environment variable to include the DLL directory. 在代码的早期(在调用任何DLL函数之前),修改PATH环境变量的进程副本以包含DLL目录。 This will enable the process to find the DLL but will not affect the system PATH variable. 这将使进程能够找到DLL,但不会影响系统PATH变量。

If you'd really rather use LoadLibrary/GetProcAddress, do yourself a favor and use a wrapper library to simplify their use. 如果您真的想使用LoadLibrary / GetProcAddress,请帮个忙,并使用包装器库简化其使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM