Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
 Creating Processes

  Switch on low bandwidth view
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: 7/2/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
How can I capture the *.exe output without waiting?      LabBoy ... Thomas Lee   |   Edit   |   Show History

Hello Everybody,

I have a Third Party Tools (*.exe), for do something. Its processing takes a lot of time (3-5 minutes)
While it is running, it print out many information to the DOS screen continuously.

I wanted to capture the DOS output (DOS screen), and show its output on the GUI application at the same time (without waiting).
I mean that, I want to see the execution progress of the *.exe (DOS output) on the GUI.

I used the API functions (CreatePipe(), CreateProcess(), PeekNamedPipe() and ReadFile()) it look like some MSDN examples.

I found out the problem, it is

while the *.exe is still running, the PeekNamedPipe () could tell that no a message in the Pipe. And, if use the ReadFile(), the programe will still wait hear until the *.exe finished or theminated.

Could sombody please tell me that "How to capture the DOS output while it is running?"

Thanks in advance,
LB



[tfl - 14 03 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or
the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a
quicker response using the forums than through the Community Content.

For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&

how to invoke multiple command prompts using C program?      Adarsh Panpalia ... dunion   |   Edit   |   Show History
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 What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker