Reputation: 18171
I need to start the external program c:\pro\prog1.exe
from my project. The external program has its configuration file in the same directory c:\pro\prog1.ini
. I do:
ShellExecute(NULL,L"open",L"c:\pro\prog1.exe" ,NULL,NULL,SW_SHOWDEFAULT);
Program c:\pro\prog1.exe
starts, but it does not load its configuration file c:\pro\prog1.ini
. It looks like I need to place the .ini
file in the same directory where my host application is run from. This is not acceptable. So, how to start an external program and ask Windows to run it from its directory?
Upvotes: 1
Views: 828
Reputation: 31599
The 5th parameter in ShellExecute
is the startup directory.
Alternatively "prog.exe"
can use GetModuleFileName
and PathRemoveFileSpec
to find its own directory, as suggested in comments.
Note that some directories like "c:\\Program Files"
and "c:\\Program Files (x86)"
require elevated access to create/modify/delete files (for example during installation). A process without elevated access, can access files in protected directories with read-only flag. Otherwise Windows will redirect the path to a different directory if write-access is requested.
For normal execution, the *.exe should use "Documents"
or "AppData"
folder to read/write data.
Upvotes: 2