CreateProcessW function

Creates a new process and its primary thread. The new process is run under the security context specified in the lpSecurityAttributes parameter.

BOOL CreateProcessW( ... );

Parameters

LPCWSTR lpApplicationName [in, optional]
LPWSTR lpCommandLine [in, out, optional]
LPSECURITY_ATTRIBUTES lpProcessAttributes [in, optional]
LPSECURITY_ATTRIBUTES lpThreadAttributes [in, optional]
BOOL bInheritHandles [in]
DWORD dwCreationFlags [in]
LPVOID lpEnvironment [in, optional]
LPCWSTR lpCurrentDirectory [in, optional]
LPSTARTUPINFOW lpStartupInfo [in]
LPPROCESS_INFORMATION lpProcessInformation [out]

Return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The CreateProcessW function creates a new process that starts running the specified executable program. The new process is created with a new message-creation thread. The CreateProcessW function can create a process in a suspended state, allowing the calling process to control the process's initial execution state. The lpCommandLine parameter is required if lpApplicationName is NULL.

If the executable file is a 32-bit application that is expected to run on a 64-bit system, the system's handling of the file depends on the system's configuration. For more information, see "Running 32-bit Applications" on the Microsoft Learn site.

To create a process that is not associated with the calling process's console, set the CREATE_NO_WINDOW flag in the dwCreationFlags parameter.

If the executable file is a script (such as a batch file or VBScript), you must specify the interpreter in the lpCommandLine parameter. For example, to run MyScript.bat, use cmd.exe /c MyScript.bat.

Example

This C++ code snippet demonstrates how to use CreateProcessW to launch Notepad:

#include <windows.h>
#include <iostream>

int main() {
    STARTUPINFOW si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    // The command line to execute
    LPCWSTR commandLine = L"notepad.exe";

    // Create the child process.
    if (!CreateProcessW(
        NULL,           // No module name (use command line)
        (LPWSTR)commandLine, // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory
        &si,            // Pointer to STARTUPINFO structure
        &pi)           // Pointer to PROCESS_INFORMATION structure
    ) {
        std::cerr << "CreateProcess failed (" << GetLastError() << ").\n";
        return 1;
    }

    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);

    // Close process and thread handles.
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    std::cout << "Notepad process has exited.\n";

    return 0;
}

Requirements

Minimum supported client
Windows 2000 Professional [desktop apps only]
Minimum supported server
Windows 2000 Server [desktop apps only]
Header
processthreadsapi.h (include windows.h)
Library
Kernel32.lib
DLL
Kernel32.dll

See also