CreateProcessA

Windows API

The CreateProcessA function creates a new process and its primary thread in the virtual address space of the calling process.

BOOL CreateProcessA( LPCSTR lpApplicationName, LPSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation );

Parameters

lpApplicationName [in, optional]

A pointer to a null-terminated string that specifies the module to be executed.

The string can be a fully qualified path to a module or a path and filename. If this parameter is NULL, the module name is inferred from the first token in the lpCommandLine string.

Note: For the ANSI version of this function (CreateProcessA), this parameter should be a pointer to an ANSI string. For the Unicode version (CreateProcessW), this parameter must be a pointer to a Unicode string.

lpCommandLine [in, out, optional]

A pointer to a null-terminated string that specifies the command line executed by this process. This string can contain a path and executable name, or a quoted path and executable name, followed by optional arguments.

This parameter can be NULL. If it is NULL, the lpApplicationName parameter must specify the module to be executed.

If this parameter is not NULL, the entire command line is converted to Unicode if it is a Unicode string and then parsed by the CreateProcess function. The first Unicode character following the module name that is not an argument is set to NULL to terminate the module name, and characters following that are passed as arguments.

Note: For the ANSI version of this function (CreateProcessA), this parameter should be a pointer to an ANSI string. For the Unicode version (CreateProcessW), this parameter must be a pointer to a Unicode string.

lpProcessAttributes [in, optional]

A pointer to a SECURITY_ATTRIBUTES structure that specifies the security descriptor for the new process. If NULL, the process gets a default security descriptor.

lpThreadAttributes [in, optional]

A pointer to a SECURITY_ATTRIBUTES structure that specifies the security descriptor for the new thread. If NULL, the thread gets a default security descriptor.

bInheritHandles [in]

A boolean value that specifies whether the new process inherits the handles of the calling process. If TRUE, each handle in the calling process is inherited; otherwise, the handles are not inherited. However, if this parameter is TRUE and certain handles in the calling process were not duplicated using the DuplicateHandle function, the new process may have access to them.

If the process is created with the CREATE_NEW_PROCESS_GROUP flag, the handles are not inherited.

dwCreationFlags [in]

Flags that control the priority class and behavior of the new process.

This parameter can be a combination of the following values:

  • CREATE_BREAK_FROM_JOB
  • CREATE_DEFAULT_ERROR_MODE
  • CREATE_FOR_WINDOWS_FUNCTION
  • CREATE_FULL_SCREEN
  • CREATE_MODIFY_SEPARATE_WOW_VDM
  • CREATE_NO_WINDOW
  • CREATE_PROTECTED_PROCESS
  • CREATE_RESTRICTED_FUNCTION
  • CREATE_SEPARATE_WOW_VDM
  • CREATE_SHARED_WOW_VDM
  • CREATE_SUSPENDED
  • CREATE_UNICODE_ENVIRONMENT
  • DEBUG_ONLY_THIS_PROCESS
  • DEBUG_PROCESS
  • DETACHED_PROCESS
  • HIGH_PRIORITY_CLASS
  • IDLE_PRIORITY_CLASS
  • NORMAL_PRIORITY_CLASS
  • REALTIME_PRIORITY_CLASS
  • BELOW_NORMAL_PRIORITY_CLASS
  • ABOVE_NORMAL_PRIORITY_CLASS
lpEnvironment [in, optional]

A pointer to a null-terminated string that specifies the environment block for the new process. If NULL, the new process inherits the environment of the calling process.

If dwCreationFlags specifies CREATE_UNICODE_ENVIRONMENT, this parameter must point to a Unicode environment string.

If dwCreationFlags does not specify CREATE_UNICODE_ENVIRONMENT, this parameter must point to an ANSI environment string.

The block is a list of null-terminated strings, terminated by a second null character. For example: "key1=value1\0key2=value2\0\0"

lpCurrentDirectory [in, optional]

A pointer to a null-terminated string that specifies the full path for the current directory for the new process. If NULL, the new process inherits the current directory of the calling process.

Note: For the ANSI version of this function (CreateProcessA), this parameter should be a pointer to an ANSI string. For the Unicode version (CreateProcessW), this parameter must be a pointer to a Unicode string.

lpStartupInfo [in]

A pointer to a STARTUPINFO structure that specifies the window station, standard handles, and appearance of the main window for the new process.

lpProcessInformation [out]

A pointer to a PROCESS_INFORMATION structure that receives identification information about the new process and its primary thread, such as the process handle and thread handle.

Return Value

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 CreateProcessA function provides access to the process and thread creation attributes. It allows you to specify the executable file, command line, security attributes, environment variables, and more for the new process.

When the new process is created, it inherits copies of the handles of the calling process. The new process has its own virtual address space, but it can share resources with the calling process.

You can use the CREATE_SUSPENDED flag to create a process in a suspended state. This allows you to modify its attributes or inject code before it starts executing.

The STARTUPINFO structure is used to specify the initial appearance of the new process's main window, including the console buffer, standard input, output, and error handles, as well as window size and position.

The PROCESS_INFORMATION structure receives handles to the new process and its primary thread, along with their unique identifiers.

Code Example

// Example of creating a new process
BOOL bSuccess;
STARTUPINFOA si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof( si ) );
si.cb = sizeof( si );
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW; // Show window

// TODO: Set lpApplicationName or lpCommandLine
// For example, to launch Notepad:
char commandLine[] = "notepad.exe";

bSuccess = CreateProcessA(
    NULL,         // lpApplicationName
    commandLine, // lpCommandLine
    NULL,         // lpProcessAttributes
    NULL,         // lpThreadAttributes
    FALSE,        // bInheritHandles
    0,            // dwCreationFlags
    NULL,         // lpEnvironment
    NULL,         // lpCurrentDirectory
    &si,          // lpStartupInfo
    &pi );       // lpProcessInformation

// If CreateProcess succeeds, the process handles are no longer needed.
// CloseHandle(pi.hProcess);
// CloseHandle(pi.hThread);

Requirements

Minimum supported client: Windows XP [desktop apps only]

Minimum supported server: Windows Server 2003 [desktop apps only]

Header: processthreadsapi.h

Library: Kernel32.lib

DLL: Kernel32.dll

Unicode and ANSI Versions: The table below shows the versions of Windows that support the function. For Unicode and ANSI versions of the function, see CreateProcess.