Hi all, I've been struggling with the WinAPI CreateProcess for some time now.
CreateProcess failed
Error code = 2
How to fix it?
CreateProcess(L"xxx.exe",
cmd,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi)
Hi all, I've been struggling with the WinAPI CreateProcess for some time now.
CreateProcess failed
Error code = 2
How to fix it?
CreateProcess(L"xxx.exe",
cmd,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi)
You can use GetModuleFileName if your .exe is in the main executable directory
Like :
WCHAR wszExe[MAX_PATH] = L"xxx.exe";
WCHAR wszDrive[MAX_PATH], wszDir[MAX_PATH], wszModule[MAX_PATH], wszPath[MAX_PATH];
GetModuleFileName(NULL, wszModule, MAX_PATH);
_wsplitpath(wszModule, wszDrive, wszDir, NULL, NULL);
swprintf(wszPath, L"%s%s%s", wszDrive, wszDir, wszExe);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
CreateProcess(NULL, wszPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
This is a well-known problem with CreateProcess. See Creating Processes; use NULL for the ApplicationName (first parameter) and use the executable path and filename in the second parameter.
The error code 2 means "The system cannot find the file specified". Probably it cannot find "xxx.exe"
Try specifying the path: CreateProcess(L"C:\\MyFiles\\xxx.exe", cmd, ...).
How to use relative path?
Relative to what?
Give an example.
Wayne
Take the time to read the documentation for the Windows API functions that you are trying to use. The documentation of the lpApplicationName and lpCommandLine parameters to CreateProcess discusses how the function uses paths and searches for the executable file.
6 people are following this question.