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: 11/19/2009

Tags :


Community Content

dunion
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.
Tags :

Page view tracker