Link qt dlls to multiple executables - c++

I have four different .exes in differend subdirs of one specific directory. All of the programs need the exact same .dlls (Qt5Core, Qt5Gui and Qt5Widgets). Is there a way of having theese three .dlls just in the root directory insted of having to copy them into each subdirectory?

Here you find the order in which DLL paths are searched on Windows:
Since Safe DLL search mode is enabled by default, the order is
The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
So the easiest thing to do is to ensure that you run your app from the working directory where you put the DLLs (5.). That could be done by writing a starter .bat file that navigates there first and then runs the application relative to the DLL direcory.
The other way would be to add the DLL directory to PATH in a starter .bat file (6.).
In both cases you need a wrapper script for each .exe that you want to run.
You could avoid that if you can make sure that links to the application set the working directory properly. This would work if you have an installer creating the shortcuts for the user and you know the user will not create one himself by just right-clicking the .exe.
There might also be a way using hardlinks or junctions but I don't know if you can copy and deploy those like symbolic links on Linux or if you need to create them on the target system.

Yes, you are using Windows and you can add your root directory as a path in Environment Varibles from
Computer-> Properties->Advanced System Settings -> Environment Variables

Related

SDL2.dll was not found

I'm trying to set up SDL2 in C++ Visual Studio but when I run the code(just some starter code I copied) it pops up with an error box box that talks about "SDL2.dll cannot be found" I tried switching to x64 but that was no help. I can see that the dll is right next to the lib files but it just won't work.
Your problem is the lib folder is not a place that your OS will search for dependent dlls by default. To fix this you would have to help your OS find the dll. There are several methods you can use to tell your OS where to look. One is adding an entry to your PATH environment variable that contains the full path to the folder containing the dll.
This site can help with setting the PATH: https://www.computerhope.com/issues/ch000549.htm
As second method is to put the dll in the same folder as the executable.
By default your OS probably is using the safe search option described here:
The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.***

How does the linker know where to find a dll file

I am working with Visual Studio and I am trying to get into dlls. I'm wondering how the linker knows where to find a DLL just from the lib file alone.
I specify the lib file and its location in the project settings but where isthe location of the associated dll file specified?
Or maybe i don't understand the topic correctly.
The Standard Search Order for Desktop Applications from the Microsoft Dll Search Order documentation:
If SafeDllSearchMode is enabled, the search order is as follows:
The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
If SafeDllSearchMode is disabled, the search order is as follows:
The directory from which the application loaded.
The current directory.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

C++ Moving DLL out of root directory into a subfolder, visual studio

I'm a beginner with building software using C++. In my project I link to DLLs and I keep them stored in the root folder. I do this because I want the project to be portable from one machine to another, and I also want the release builds to have dependence from installing things into system32 and what not.
The problem is all the DLLs in the root folder is messy, so I want to organize them into subfolders. But I can't do that because putting the DLL in a root subfolder instead of the root, you get errors. I think because the DLL is copied to the output at the wrong location, not where the exe is, but in a subfolder, just like the source structure. Am I right about that?
What is a solution that will allow me to have the project be still be copy-pastable/portable between machines?
Windows searches for DLLs in predefined locations (see Dynamic-Link Library Search Order). Subdirectories of the directory where the application resides are not part of the search order.
To implement your requirement, you will need to explicitly add the directories to the searched locations. This process consists of two steps:
Call AddDllDirectory for each directory you want searched, in addition to the default search locations on application startup.
By default, DLL imports are resolved prior to starting a process' primary thread. To allow your application to change the DLL search path, import resolution needs to be postponed. The easiest way to do this is by using the /DELAYLOAD (Delay Load Import) linker option (see Linker Support for Delay-Loaded DLLs for additional information).
While it is possible, to segregate DLLs into subdirectories, it is best to keep them all alongside the executable image.
If you put the DLLs into subfolders instead of keeping them in the same directory as the executable, you would either have to modify PATH environment variable in Windows to point to every subfolder, or use the DLLs in LoadLibrary+GetProcAddress way instead of linking them to the executable via import libs.

CreateProcess ignores lpCurrentDirectory / parent working directory

I'm trying to use CreateProcess to execute an external process from a c++ application.
The launched application fails when looking for dll's that exist in it's directory.
Inspecting the executwd process with Process Explorer (from sysinternals) shows that the process working directory is c:\windows, and not the parents working directory.
Using lpCurrentDirectory to hard code the working directory also doesn't help.
The process does execute properly qhen using the system command.
EDIT
The problematic directory isn't the module directory, but the current working directory.
The modules directory is searched as expected, but doesn't contain the dll
(Copying the sll to the module directory works -but it,s a workaround)
When you use CreateProcess function, the first directory searched is the directory containing the image file used to create the calling process.This allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable. If Dll files are not found, system searches these files in system folders, such as system32 or %windir%.
The search path can be altered using the SetDllDirectory function. This solution is recommended instead of using SetCurrentDirectory or hard-coding the full path to the DLL.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682600%28v=vs.85%29.aspx

Infernal Libraries (aka DLL Hell)

In a Project of mine, I use a Delphi Application which dynamically loads a wrapper DLL (exporting C-Style functions) which in turn is statically link against a bunch of 3rd party DLLs.
It works fine on my test machines, but on my customers computer it failed to initialize with an error Message like "Couldn't find entrypoint _somefunction#4AKKZ in TMYlibrary.dll".
After some investigation with sysinternal's process monitor, I realized that Windows would look fror DLLs in windows/sytem32 first, so if a DLL named similar to my DLL was present in system32, windows would pick that one and try to find my function entry points in it - which would fail.
Do you know of a possiblity to change windows' DLL the searching behaviour?
Additional Information
[Update] The .exe file is located on the top level of the application's folder tree.
The Wrapper and the 3rd-party-DLLs ar e both located in the Subfolder /bin of my apps Folder
Dev platform is windows XP/7, using VS2008 for the dlll and Delphi 2010 for the application
I found another solution myself:
SetDllDirectory adds an additional search path to the list of locations to look at.
From http://msdn.microsoft.com/en-us/library/ms686203%28v=VS.85%29.aspx
After calling SetDllDirectory, the DLL search path is:
The directory from which the application loaded.
The directory specified by the lpPathName parameter.
The system directory. Use the GetSystemDirectory function to get the
path of this directory. The name of
this directory is System32.
The 16-bit system directory. There is no function that obtains the
path of this directory, but it is
searched. The name of this directory
is System.
The Windows directory. Use the GetWindowsDirectory function to get
the path of this directory.
The directories that are listed in the PATH environment variable.
(maybe i should do my googling before I post on SO ;)
Ship the DLL in your program's folder. (same as the exe file).
Then Windows should try your version first.