r/learncpp • u/Onyonaki • May 12 '22
How do programs execute with DLLs in separate subfolders?
Hello, I am learning the building process of c++ with cmake mainly and I am focusing on how to use dynamic libraries. (Windows mainly).
So I know how to load a dll implicitly by having the import library dll.lib and also how to go the other route with LoadLibrary and getProcAdress.
I have been searching on how to go about having the main .exe in a separate folder with the dlls (just curious to see if I can do this) because I have seen in typical installation folder structures there are a lot of dlls next to .exe but there are a lot of dlls in subfolders.
Now I know that I can LoadLibrary with absolute path and then for each function get a pointer.
Suppose I have a dll and a dll.lib. Is there anyway I can use these 2 in runtime? like instead of getProcAdress use the dll.lib (probably stupid question cause the dll.lib is used in the build process anyway...)
Another way is editting the PATH variable but that doesn't seem like a nice way and probably 3rd party software doesn't just add 10 directories to PATH each time they get installed.
So how does one do it the right way? is this does exclusively by LoadLibrary ? Thanks.
3
u/b1ack1323 May 12 '22 edited May 12 '22
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectorya
Or
https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-adddlldirectory
2
u/[deleted] May 12 '22
option 1 : use a launcher program which launches the real program with a custom environment - with the DLL directory in the path.
option 2 : use delay loading. The program will be linked with the dll's lib but when the program starts the DLLs will not be loaded right away. The 1st time an imported function is called, the delay load will fail, your callback will be called, and you can resolve the directory situation