Click here to Skip to main content
12,550,766 members (54,305 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked

Enhanced version of the CreateProcess function

, 26 Apr 2004
Rate this:
Please Sign up or sign in to vote.
Waits for the child process to exit, minimizes the main window while the child process runs, and automatically restores the window when it exits.


In one of my programs, I needed to wait until an external application finished processing some data, and wanted to minimize the main window during that time to prevent the user from doing anything else before the results were available. That's why I wrote CreateProcessEx.

The CreateProcessEx function takes seven parameters but only the first two are required:

  • lpAppName:Pointer to a null-terminated string that specifies the application to execute. The string can specify the full path and file name of the application to execute, or it can specify a partial name.

    If lpAppName is NULL, the application name must be the first white space-delimited token in the lpCmdLine string.

  • lpCmdLine: Pointer to a null-terminated string that specifies the command line to execute. This parameter can be NULL.
  • bAppInCmdLine: Indicates if the lpAppName string must be used as the first white space-delimited token in the lpCmdLine string.
  • bCompletePath: Indicates if the lpAppName string specifies a partial name that must be completed using the path of the calling process.
  • bWaitForProcess: Indicates if the calling process must wait until child process exits.
  • bMinimizeOnWait: Indicates if the main window will be minimized while the child process runs and automatically restored when it exits.
  • hMainWnd: Handle of the main window to be minimized. If hMainWnd is NULL, then AfxGetMainWnd() is used.
DWORD CreateProcessEx ( LPCSTR lpAppPath, LPCSTR lpCmdLine, 
    BOOL bAppInCmdLine, BOOL bCompletePath,
    BOOL bWaitForProcess, BOOL bMinimizeOnWait, HWND hMainWnd ) {

    STARTUPINFO startupInfo;
    PROCESS_INFORMATION    processInformation;    

    char szAppPath    [ _MAX_PATH  ];
    char szCmdLine    [ _MAX_PATH  ];
    char drive        [ _MAX_DRIVE ];
    char dir        [ _MAX_DIR   ];
    char name       [ _MAX_FNAME ];
    char ext        [ _MAX_EXT ];

    szAppPath[ 0 ] = '\0';
    szCmdLine[ 0 ] = '\0';
    ZeroMemory( &startupInfo, sizeof( STARTUPINFO ));

    startupInfo.cb = sizeof( STARTUPINFO );

    ZeroMemory( &processInformation, sizeof( PROCESS_INFORMATION ));

    if ( bCompletePath ) {

        GetModuleFileName( GetModuleHandle( NULL ), szAppPath, _MAX_PATH );

        _splitpath( szAppPath, drive, dir, NULL, NULL );
        _splitpath( lpAppPath, NULL, NULL, name, ext );

        _makepath ( szAppPath, drive, dir, name, strlen( ext ) ? ext : ".exe" );
    else strcpy( szAppPath, lpAppPath );

    if ( bAppInCmdLine ) {

        strcpy( szCmdLine, "\"" );
        strcat( szCmdLine, szAppPath );
        strcat( szCmdLine, "\"" );

    if ( lpCmdLine ) {

        // We must use quotation marks around the command line (if any)...

        if ( bAppInCmdLine ) strcat( szCmdLine, " \"" );
                        else strcpy( szCmdLine, "\"" );

        strcat( szCmdLine, lpCmdLine );
        strcat( szCmdLine, "\"" );

    DWORD dwExitCode = -1;
    if ( CreateProcess(    bAppInCmdLine ? NULL: szAppPath,    // lpszImageName
                        szCmdLine,                            // lpszCommandLine
                        0,                                    // lpsaProcess
                        0,                                    // lpsaThread
                        FALSE,                                // fInheritHandles
                        DETACHED_PROCESS,                    // fdwCreate
                        0,                                    // lpvEnvironment
                        0,                                    // lpszCurDir
                        &startupInfo,                        // lpsiStartupInfo
                        &processInformation                    // lppiProcInfo
                    )) {

        if ( bWaitForProcess ) {

            if ( bMinimizeOnWait )
                if ( IsWindow( hMainWnd )) ShowWindow( hMainWnd, SW_MINIMIZE );
                #ifdef __AFX_H__
                else AfxGetMainWnd()->ShowWindow( SW_MINIMIZE );

            WaitForSingleObject( processInformation.hProcess, INFINITE );

            if ( bMinimizeOnWait )

                if ( IsWindow( hMainWnd )) ShowWindow( hMainWnd, SW_RESTORE );
                #ifdef __AFX_H__
                else AfxGetMainWnd()->ShowWindow( SW_RESTORE );

            GetExitCodeProcess( processInformation.hProcess, &dwExitCode );
        else {

            CloseHandle( processInformation.hThread );
            CloseHandle( processInformation.hProcess );

            dwExitCode = 0;

    return dwExitCode;

To use the function, simply include the CreateProcessEx.h and CreateProcessEx.cpp files in your project. That's it!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Spain Spain
I love releasing useful and user friendly applications. I've been programming for more than 12 years. Started with Basic and Pascal but quickly moved to C and C++.

Currently I work for GemiScorp Software Solutions (

I have developed some popular products like: SafeLogon, LaunchIt NOW! Plus, SafeSystem and SafeCryptor.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
bygreencn16-Apr-15 19:13
memberbygreencn16-Apr-15 19:13 
Generalchild of child process Pin
NehaMishra286843-Mar-11 0:35
memberNehaMishra286843-Mar-11 0:35 
GeneralThanks! Pin
cybernaut_ev9-Sep-09 9:36
membercybernaut_ev9-Sep-09 9:36 
GeneralNot working in unicode Pin
DijaS26-Nov-08 17:51
memberDijaS26-Nov-08 17:51 
QuestionCan I handle the text output of the running process? and receive a Message when the process is done? Pin
mimosa31-Mar-08 7:54
membermimosa31-Mar-08 7:54 
GeneralTCHAR-enabled version Pin
Xandyboy13-Aug-07 12:20
memberXandyboy13-Aug-07 12:20 
General.:::. Nice One .:::. Pin
Programm3r16-Oct-06 1:20
memberProgramm3r16-Oct-06 1:20 
GeneralGreat Solution - - Thanks!!! Pin
JeffYoung6628-Sep-06 12:29
memberJeffYoung6628-Sep-06 12:29 
QuestionWhat is the best way to terminate the *.exe file called by CreateProcess() function ?. Pin
zjonlee2-Jun-06 21:17
memberzjonlee2-Jun-06 21:17 
GeneralAbout the CreateProcess() on 4.2 Pin
slyzhang23-Mar-06 15:34
memberslyzhang23-Mar-06 15:34 
QuestionWhy is there no closeHandle in 'if ( bWaitForProcess ) { ... }' clause...? Pin
Jin, Lee11-Mar-06 22:07
memberJin, Lee11-Mar-06 22:07 
GeneralRestriction on command line length in CreateProcess Pin
AbhijeetJ11-Apr-05 2:42
memberAbhijeetJ11-Apr-05 2:42 
GeneralRe: Restriction on command line length in CreateProcess Pin
GregDude6-Feb-07 15:36
memberGregDude6-Feb-07 15:36 
QuestionHow i can startup iexplore Pin
buzzu18-Mar-05 6:41
sussbuzzu18-Mar-05 6:41 
GeneralSetForgroundWindow or BringWindowToTop Pin
Nawal Kishore3-Feb-05 0:08
memberNawal Kishore3-Feb-05 0:08 
GeneralThanks - and question Pin
rouzbie7-Dec-04 17:06
memberrouzbie7-Dec-04 17:06 
QuestionHow to get the Window Handle Pin
wilbertku@hotmail.com25-Jul-04 4:08
memberwilbertku@hotmail.com25-Jul-04 4:08 
QuestionCould you explain? Pin
WREY27-Apr-04 23:32
memberWREY27-Apr-04 23:32 
AnswerRe: Could you explain? Pin
Boherna28-Apr-04 0:24
memberBoherna28-Apr-04 0:24 
GeneralRe: Could you explain? Pin
WREY28-Apr-04 11:26
memberWREY28-Apr-04 11:26 
GeneralRe: Could you explain? Pin
Boherna28-Apr-04 13:19
memberBoherna28-Apr-04 13:19 
GeneralRe: Could you explain? Pin
_umberto2120-Jul-04 14:58
suss_umberto2120-Jul-04 14:58 
GeneralRe: Could you explain? Pin
anonymous20-Jul-04 14:58
sussanonymous20-Jul-04 14:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 27 Apr 2004
Article Copyright 2004 by BillyNorwak
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid