简体   繁体   中英

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. How do I make the EXE look in a sub-folder, such as "DLLs"? If this has anything to do with it, my IDE is Code::Blocks.

To make this work with explicit loading (LoadLibrary/GetProcAddress) is easy enough. You are in control of the binding process and simply pass the full path to LoadLibrary.

For implicit loading you are at the mercy of the system. Whilst you could augment the PATH variable this is extremely draconian. Faced with the choice of putting all DLLs alongside the executable or modifying PATH, I would always choose the former.

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.

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. The myapp.exe binary is just a stub application that executes 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. This is done via a linker option or compiler #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. This will enable the process to find the DLL but will not affect the system PATH variable.

If you'd really rather use LoadLibrary/GetProcAddress, do yourself a favor and use a wrapper library to simplify their use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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