CreateProcess

winrt_capture 21 Reputation points
2021-09-13T07:20:10.933+00:00

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)

Windows development | Windows API - Win32
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 125.7K Reputation points
    2021-09-13T07:41:06.883+00:00

    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, ...).

    2 people found this answer helpful.

  2. Sam of Simple Samples 5,571 Reputation points
    2021-09-13T18:58:15.447+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

  3. Castorix31 91,501 Reputation points
    2021-09-15T09:52:23.88+00:00

    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);
    
    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.