When calling an EXE file using Shellexecutea, specify the working directory to be aware of
The function prototypes are:
HInstance Shellexecutea (
HWND hwnd, LPCTSTR lpoperation, LPCTSTR lpfile, LPCTSTR lpparameters, LPCTSTR Lpdirectory, INT nshowcmd );
This is illustrated in MSDN for Lpdirectory:
[In] A pointer to a null-terminated string this specifies the default (working) directory for the action. If This value is NULL, the current working directory is used. If A relative path is provided at Lpfile, do not use a relative path for lpdirectory.
Note the last sentence: if lpfile provides a relative path, lpdirectory cannot use a relative path.
I think this sentence is: If lpfile provides a relative path, lpdirectory can use absolute path, but the experiment is not so.
Here's a list of programs: C:\\Temp
A folder where the program A.exe,a.exe run will display the directory path, such as:
The following are verified in four cases:
1, Test1.exe
Load A\\a.exe with relative paths and specify the working directory for A.exe
Code to use:
char* Workdir = "c:\\temp\\a\\"; HINSTANCE ret;//uses relative path, specifies working directory: file cannot find ret = Shellexecutea (null, "open", "A\\a.exe", NULL, Workdir, SW_SHOWNORMAL); if (int ret = = error_file_not_found) {std::cout << "Use relative path, specify working directory: File not found" << Std::endl;}
If it is: The hint file cannot be found.
2, Test2.exe
Load A\\a.exe with relative paths, do not specify A.exe working directory
Code to use:
char* Workdir = "c:\\temp\\a\\"; HINSTANCE ret;//uses relative paths and does not specify working directory: File load normal//a.exe working directory is "c:\\temp\\" ret = Shellexecutea (NULL, "open", "A\\a.exe", NULL, NULL, SW_SHOWNORMAL);
As a result, a.exe can load normally, but a.exe displays a working directory of C:\\Temp
3, Test3.exe
load A\\a.exe with relative paths, do not specify a.exe working directory
Code to use:
char* Workdir = "c:\\temp\\a\\"; HINSTANCE ret; Using absolute path, no working directory specified: File load normal//a.exe's working directory is "c:\\temp\\" ret = Shellexecutea (null, "open", "C:\\temp\\a\\a.exe", NULL, NULL, SW_SHOWNORMAL);
As a result, a.exe can load normally, but a.exe displays a working directory of C:\\Temp, as in the second case
4, Test4.exe
Enables loading of a\\a.exe with an absolute path, specifying the working directory of A.exe
Code to use:
char* Workdir = "c:\\temp\\a\\"; HINSTANCE ret;////Use absolute path, specify working directory: File load normal////a.exe working directory for "c:\\temp\\a\\" ret = Shellexecutea (NULL, "open", "c:\\temp\\a\ \a.exe ", NULL, Workdir, SW_SHOWNORMAL);
As a result, a.exe can load normally, but a.exe displays a working directory of c:\\temp\\a
Summary: When using Shellexecutea call EXE file, if you need to specify the working directory of the program, EXE file path to use absolute path as far as possible
Shellexecutea loading EXE file specifies that the working directory cannot find a file