Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use CreateProcess?

Execute (spawn) a program

How do I use CreateProcess?

by  2ffat  Posted    (Edited  )
Declaration
Code:
BOOL CreateProcess(
    LPCTSTR lpApplicationName,
    LPTSTR lpCommandLine,
    LPSECURITY_ATTRIBUTES lpProcessAttributes,
    LPSECURITY_ATTRIBUTES lpThreadAttributes,
    BOOL bInheritHandles,
    DWORD dwCreationFlags,
    LPVOID lpEnvironment,
    LPCTSTR lpCurrentDirectory,
    LPSTARTUPINFO lpStartupInfo,
    LPPROCESS_INFORMATION lpProcessInformation);

lpApplicationName is a null terminated string that specifies the program to execute. It can be the full path and filename or a partial name. It can be NULL but if it is then lpCommandLine must be the program to execute. [color red]See Gotcha below.[/color]

lpCommandLine specifies the command line to execute. It can be NULL.

lpProcessAttributes is a pointer to the SECURITY_ATTRIBUTES structure. See the Windows SDK for more info on this structure. If NULL, the handle cannot be inherited.

lpThreadAttributes is the same as lpProcessAttibutes.

bInheritHandles tells whether the new process inherits handles from the calling process.

dwCreationFlags specifies additional flags for priority class and creation of the process. The following creation flags can be specified in any combination, except as noted:
[tab][color blue]CREATE_DEFAULT_ERROR_MODE[/color] -- The new process does not inherit the error mode of the calling process. Instead, CreateProcess gives the new process the current default error mode. An application sets the current default error mode by calling SetErrorMode.This flag is particularly useful for multi-threaded shell applications that run with hard errors disabled. The default behavior for CreateProcess is for the new process to inherit the error mode of the caller. Setting this flag changes that default behavior.
[tab][color blue]CREATE_NEW_CONSOLE[/color] -- The new process has a new console, instead of inheriting the parent's console. This flag cannot be used with the DETACHED_PROCESS flag.
[tab][color blue]CREATE_NEW_PROCESS_GROUP[/color] -- The new process is the root process of a new process group. The process group includes all processes that are descendants of this root process. The process identifier of the new process group is the same as the process identifier, which is returned in the lpProcessInformation parameter. Process groups are used by the GenerateConsoleCtrlEvent function to enable sending a CTRL+C or CTRL+BREAK signal to a group of console processes.
[tab][color blue]CREATE_SEPARATE_WOW_VDM[/color] -- Windows NT only: This flag is valid only when starting a 16-bit Windows-based application. If set, the new process is run in a private Virtual DOS Machine (VDM). By default, all 16-bit Windows-based applications are run as threads in a single, shared VDM. The advantage of running separately is that a crash only kills the single VDM; any other programs running in distinct VDMs continue to function normally. Also, 16-bit Windows-based applications that are run in separate VDMs have separate input queues. That means that if one application hangs momentarily, applications in separate VDMs continue to receive input.
[tab][color blue]CREATE_SHARED_WOW_VDM[/color] -- Windows NT only: The flag is valid only when starting a 16-bit Windows-based application. If the DefaultSeparateVDM switch in the Windows section of WIN.INI is TRUE, this flag causes the CreateProcess function to override the switch and run the new process in the shared Virtual DOS Machine.
[tab][color blue]CREATE_SUSPENDED[/color] -- The primary thread of the new process is created in a suspended state, and does not run until the ResumeThread function is called.
[tab][color blue]CREATE_UNICODE_ENVIRONMENT[/color] -- If set, the environment block pointed to by lpEnvironment uses Unicode characters. If clear, the environment block uses ANSI characters.
[tab][color blue]DEBUG_PROCESS[/color] -- If this flag is set, the calling process is treated as a debugger, and the new process is a process being debugged. The system notifies the debugger of all debug events that occur in the process being debugged.If you create a process with this flag set, only the calling thread (the thread that called CreateProcess) can call the WaitForDebugEvent function.
[tab][color blue]DEBUG_ONLY_THIS_PROCESS[/color] -- If not set and the calling process is being debugged, the new process becomes another process being debugged by the calling process's debugger. If the calling process is not a process being debugged, no debugging-related actions occur.
[tab][color blue]DETACHED_PROCESS[/color] -- For console processes, the new process does not have access to the console of the parent process. The new process can call the AllocConsole function at a later time to create a new console. This flag cannot be used with the CREATE_NEW_CONSOLE flag.

lpEnvironment points to an environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process.

lpCurrentDirectory is a null-terminated string that specifies the current drive and path for the child process. If NULL, the process is created in the place as the calling program.

lpStartupInfo points to the STARTINFO structure. See the Windows SDK for more info on this structure.

lpProcessInformation points to the PRCOESS_INFORMATION structure. See the Windows SDK for more info on this structure.

[tab]If successful, CreateProcess will return a non-zero number. If it fails, use GetLastError to get error.

Pros
[tab]This function can call 16-bit programs. It is the "prefered" way to spawn other processes. It is very flexiable and powerful.

Cons
[tab]This is a very complex function that calls several structures.

Examples
[tab]The following examples came from March 1999 C++ Builders Developer's Journal (http://bcbjournal.org/)
[tab]The following starts Notepad in a normal window.
Code:
STARTUPINFO StartInfo; // name structure
PROCESS_INFORMATION ProcInfo; // name structure
memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
StartInfo.cb = sizeof(StartInfo); // Set structure size
int res = CreateProcess("C:\\Windows\\notepad.exe",NULL, NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo); // starts notepad

[tab]The following starts a program hidden from the user.
Code:
STARTUPINFO StartInfo; // name structure
PROCESS_INFORMATION ProcInfo; // name structure
memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
StartInfo.cb = sizeof(StartInfo); // Set structure size
StartInfo.dwFlags = STARTF_USESHOWNWINDOW; // Necessary for wShowWindow to work
StartInfo.wShowWindow = SW_HIDE; // Hide window
int res = CreateProcess(NULL, "MyApp.exe", NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo); // starts MyApp

[tab]The following example starts a program then waits for the process to end.
Code:
STARTUPINFO StartInfo; // name structure
PROCESS_INFORMATION ProcInfo; // name structure
memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
StartInfo.cb = sizeof(StartInfo); // Set structure size
int res = CreateProcess(NULL, "MyApp.exe", NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo); // starts MyApp
if (res)
{
    WaitForSingleObject(ProcInfo.hThread, INFINITE); // wait forever for process to finish
    SetFocus(); // Bring back focus
}

[red]Gotcha[/red]
[tab]If the program is a 16-bit program, do not use lpApplicationName to run program, use lpCommandLine. The reason is that not all Window versions will work if lpApplicationName calls a 16-bit program. If in doubt, use lpCommandLine to call the program since it will work in both 16-bit and 32-bit programs.

Code:
int res = CreateProcess("MyApp.exe", NULL, NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo);
[tab]If MyApp is a 16-bit program, this call will only owrk with NT.

Code:
int res = CreateProcess(NULL, "MyApp.exe", NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo);
[tab]This will work for all Windows and 16- & 32-bit programs.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top