CreateProcess Function

This topic describes the CreateProcess function, which creates a new process and its primary thread. The new process runs in the security context of the calling process.

BOOL CreateProcess(
  [in, optional]      LPCTSTR               lpApplicationName,
  [in, out, optional] LPTSTR                lpCommandLine,
  [in, optional]      LPSECURITY_ATTRIBUTES lpProcessAttributes,
  [in, optional]      LPSECURITY_ATTRIBUTES lpThreadAttributes,
  [in]                BOOL                  bInheritHandles,
  [in]                DWORD                 dwCreationFlags,
  [in, optional]      LPVOID                lpEnvironment,
  [in, optional]      LPCTSTR               lpCurrentDirectory,
  [in]                LPSTARTUPINFO         lpStartupInfo,
  [out]               LPPROCESS_INFORMATION lpProcessInformation
);

Parameters

Parameter Description
lpApplicationName

The name of the module to be executed. This string must be a fully qualified path. If this parameter is NULL, the module name is taken from the first token in the lpCommandLine string. This parameter must include the extension (.exe).

The string must be Unicode (wchar_t*) or ANSI (char*).

lpCommandLine

The command line string for the new process. This string must be NULL terminated.

This parameter can be NULL if lpApplicationName is not NULL. If lpApplicationName is NULL, this parameter should be NULL. If both are NULL, the function fails.

The string must be Unicode (wchar_t*) or ANSI (char*).

lpProcessAttributes

A pointer to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the new process. If NULL, the child process gets the same security attributes as the parent process.

lpThreadAttributes

A pointer to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the new process's primary thread. If NULL, the thread gets the same security attributes as the parent process.

bInheritHandles

If this parameter is TRUE, the calling process's handles are inherited by the new process. Otherwise, they are not inherited.

dwCreationFlags

Flags that control the priority class and behavior of the new process. This parameter can be zero or a combination of the following values:

  • CREATE_BREAK_OUTPUT_DEBUG
  • CREATE_DEFAULT_ERROR_MODE
  • CREATE_NEW_CONSOLE
  • CREATE_NEW_PROCESS_GROUP
  • CREATE_NO_WINDOW
  • CREATE_PROTECTED_PROCESS
  • CREATE_PRESERVE_CODE_AUTHZ_LEVEL
  • CREATE_SEPARATE_WOW_VDM
  • CREATE_SHARED_WOW_VDM
  • CREATE_SUSPENDED
  • CREATE_SYSTEM_MAXIMUM_PRIORITY
  • CREATE_UNICODE_ENVIRONMENT
  • DEBUG_ONLY_THIS_PROCESS
  • DEBUG_PROCESS
  • DETACHED_PROCESS
  • EXTENDED_STARTUPINFO_PRESENT
  • INHERIT_CALLER_PRIORITY
lpEnvironment

A pointer to a null-terminated list of strings, which specifies the environment variables for the new process. If NULL, the new process inherits the environment of the calling process.

lpCurrentDirectory

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

lpStartupInfo

A pointer to a STARTUPINFO structure that specifies how the new process should be launched.

lpProcessInformation

A pointer to a PROCESS_INFORMATION structure that receives identification information for the new process.

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

General Usage

The CreateProcess function creates a new process that runs in the security context of the calling process. The new process can be created with a new console or inherit the console of the parent. It can also inherit handles, environment variables, and the current directory from the parent.

Security Considerations

When creating a new process, be mindful of the security implications, especially regarding handle inheritance and security attributes. It's recommended to use explicit security attributes rather than relying on default inheritance when possible.

Error Handling

Always check the return value of CreateProcess and call GetLastError to diagnose any failures. Common errors include invalid paths, insufficient permissions, or incorrect parameter values.

Note: The CreateProcess function can be complex. Thoroughly understand all parameters and their implications before use.
Important: Ensure that the application being launched is trusted and that the necessary permissions are in place.

See Also