Creating Processes

Expand
43 out of 65 rated this helpful - Rate this topic

Creating Processes

The CreateProcess function creates a new process, which runs independently of the creating process. However, for simplicity, the relationship is referred to as a parent-child relationship.

The following code demonstrates how to create a process.


#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

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

    if( argc != 2 )
    {
        printf("Usage: %s [cmdline]\n", argv[0]);
        return;
    }

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        argv[1],        // 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
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

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

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


If CreateProcess succeeds, it returns a PROCESS_INFORMATION structure containing handles and identifiers for the new process and its primary thread. The thread and process handles are created with full access rights, although access can be restricted if you specify security descriptors. When you no longer need these handles, close them by using the CloseHandle function.

You can also create a process using the CreateProcessAsUser or CreateProcessWithLogonW function. This allows you to specify the security context of the user account in which the process will execute.

 

 

Send comments about this topic to Microsoft

Build date: 5/5/2012

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD
Create process and IE

Answering Moonprince's remark about the handle to IE: the following article describes how Internet Explorer starts up and explains why the initial process exits so quickly:

Internet Explorer in Protected Mode – How the Low Integrity Environment Gets Created ( http://helgeklein.com/blog/2009/01/internet-explorer-in-protected-mode-how-the-low-integrity-environment-gets-created/)

4/28/2012
Handle count not reduced
Hi, In our company we are restarting a service automatically if it is stopped. While doing that we are creating process using 'CreateProcess' method. Because of this the handle count increases. Even though we close the handles using 'CloseHandle' method before restarting the process the handle count is not getting reduced. As per the documentation the handles should be close when 'CloseHandle' is used and the handle count should go down but this is not happening. Can any one help me what can I do to reduce the handle count. Thanks, Sathwick
10/24/2011
CreateProcess will not return the right handle when launching IE7 and over
If you execute the code in this example and try to launch IE7 you will see that the WaitForSingleObject exits right away... without waiting for the closure of the newly created IE browser window.  This is new since IE7... and probably new browsers will have the same behavior (Google Chrome!?!).  The PID and Process handle seem to be the one of the new tab created inside of the IE browser (even if this is a new browser).  Probably there is a way of getting the PID and handle of the browser from there...
9/28/2010
how to invoke multiple command prompts using C program?
Hi *,
I tried invoking multiple command prompts running several batch files simultaneously (I want to have several command prompts simultaneously) using C/C++ program.
But I am not geting how to do it.
Please help me out.

Thanks in advance.

Regards
Adarsh

Create some sort of thread manager with storage for the output of the programs. Plan to dequeue and clean up this in a separate thread regularly enough so that it doesn't get too big.

For each third party app you want to 'host' create a thread. In each thread create a process. Create named pipes for each of the threads to capture the STDIN and STDOUT for each of the processes (in your case you may only need the STDOut). CreateProcess allows you to specify the specify the STARTUPINFO. This startupInfo allows you to set hStdError, hStdOutput, and hStdInput, which you can point to the named pipes, which should be pointers on a structure in your object that manages all the threads. In the threads, you (in a threadsafe way) update this data.

Then in your UI you can read or display this data independent / asynchronous to the functioning of the actual third party programs, whenver you display it you can clear the data off the 'data collector' and move it to the UI manager since as far as the command line program is concerned it's gone anyway.

Dave.
5/28/2009