I’m attempting to launch a process from a different process. The mechanism in which this is achieved is not subject to change. Both the launcher and the original process are located in C:\dir.
I’m starting my launcher from a cmd file. The cmd file itself is located somewhere else, and in order for it to find the launcher executable, I’m setting the PATH variable:
set PATH=C:\dir;%PATH%;
launcher.exe
The launcher starts the child process with the following code:
STARTUPINFO startupInfo;
startupInfo.cb = sizeof (STARTUPINFO);
startupInfo.lpReserved = 0;
startupInfo.lpDesktop = NULL;
startupInfo.lpTitle = NULL;
startupInfo.dwX = 0;
startupInfo.dwY = 0;
startupInfo.dwXSize = 0;
startupInfo.dwYSize = 0;
startupInfo.dwXCountChars = 0;
startupInfo.dwYCountChars = 0;
startupInfo.dwFillAttribute = 0;
startupInfo.dwFlags = _showInForeground ? STARTF_USESHOWWINDOW : 0;
startupInfo.wShowWindow = _showInForeground ? 1 : 0;
startupInfo.cbReserved2 = 0;
startupInfo.lpReserved2 = 0;
PROCESS_INFORMATION processInfo;
BOOL retVal = CreateProcess("child.exe", "", NULL, NULL, FALSE,
_showInForeground ? (CREATE_NEW_CONSOLE | CREATE_DEFAULT_ERROR_MODE) : CREATE_DEFAULT_ERROR_MODE,
NULL, NULL, &startupInfo,&processInfo);
It returns 0 and last error is 2, which is File not found.
If it helps, GetCurrentDirectory returns the directory where the cmd is located, not C:\dir. I’m guessing CreateProcess can’t find child.exe because PATH is not available to it.
Any ideas how to get this to work?
EDIT: Some good comments with answers (as comments are sometimes overlooked):
Suggestion: set statupInfo.lpDirectory to “c:\dir”
Answer: can’t. I’m starting from the cmd because the directory may change.
According to MSDN, CreateProcess actually does search PATH, but only if
lpApplicationNameisNULLand the executable is the first token inlpCommandLine.In other words it should work if you call
CreateProcess(NULL, "child.exe", ...I haven’t tried it though, so YMMV and so on.