I just tried to run a new process by resolving its location via PATH env. var. Since I use Qt this means I added
X:\folder\
to my PATH variable and in my starter application I am calling
QProcess::startDetached("test.exe")
which actually works.
However test.exe writes data to its working directory. This data ends up in the directory of the starter application instead of X:\folder\ which is not what I want.
I tested the behaviour directly in the windows command line by typing "test.exe" in the CLI and it is the same there (having the data written to the current directory).
Is there a way (in C++ or command line) to start a process using PATH while also using the directory found in PATH as working directory of the new process?
I could search PATH for my own, analyze the finding and start the program by another QProcess::startDetached() overload but I wonder if there is an easier way.
There are at least 3 options without complicated configurations.
1 - From your calling application change current directory to where your
test.exe
program is located. That way, files will go to the desired directory. BUT then it is possible that the calling application will have problems or generate output where it should not, so a new change of current directory in the calling application is needed2 - Pass as parameter to your
test.exe
where it should generate its files.3 - Determine from your
test.exe
where it is located and use this information to change current directory for this process, or, knowing the path, generate the files in the same directory it is located.This is the standard windows way of retrieving the location of the current process. Reference here: GetModuleFileName