How can I automatically load DLLs from a subdirectory?

In Visual Studio, you create a .dll project and create DLL files and .lib files. You are statically bound to .lib, and if .dll is in the same folder as .exe, everything works.

I suspect that everything will work if the .dll is located in System32 or in any other PATH folder (confirm or correct, please).

But here is the question: I want my exe to find .dll in the. / DLLS / folder, that is, if my exe is in ...... /MyApp/MyApp.exe, then it should look for .dll in .. ... /MyApp/DLLS/MyDll.dll. I DO NOT want to include the dlls folder in the path. Is there any way to do this?

Please note that I do not want to explicitly use LoadLibrary, so I cannot specify the path there.

Thanks in advance for any help.

+3
source share
2 answers

You can use SetDllDirectory. The loader will use the additional directory that you specified when loading the libraries. However, there can only be one additional directory, so you need to make sure that there are no other calls elsewhere in your application, otherwise the directory you specify will be ignored.

If this API does not allow relative directories (I'm not sure), you can always call GetModuleFileNamewith the NULLfirst parameter to get the file name of the current executable program. With a little string manipulation, you can get the absolute path to your DLL folder.

+2
source

, Win32 DLL:

http://msdn.microsoft.com/en-us/library/7d83bc18(VS.80).aspx

, SetCurrentDirectory SetDllDirectory. Delay Loaded Library ( Visual Studio). , DLL , , .

+3

Source: https://habr.com/ru/post/1768835/


All Articles