Close cmd prompt window opened with CreateProcess

Posted on 2005-04-18
Last Modified: 2013-11-20
I'm running a program via command prompt from within my program.  But when everything is done the cmd prompt window is still open, what can I do to close the window?

My Code:

if( !CreateProcess( NULL, // No module name (use command line).
                                    commandLine.GetBuffer(commandLine.GetLength()), // Command line.
                                    NULL,             // Process handle not inheritable.
                                    NULL,             // Thread handle not inheritable
                                    FALSE,            // Set handle inheritance to FALSE.
                                    CREATE_NEW_CONSOLE,                // No creation flags.
                                    NULL,             // Use parent's environment block.
                                    workingDirectory,             // Use parent's starting directory.
                                    &si,              // Pointer to STARTUPINFO structure.
                                    &pi ))             // Pointer to PROCESS_INFORMATION structure.
       AfxMessageBox( "REPORTA.EXE failed to run" );

      commandLine.ReleaseBuffer ( );
      CloseHandle( pi.hProcess );
      CloseHandle( pi.hThread );
Question by:galneweinhaw
    LVL 30

    Expert Comment

    See ('HOWTO: Terminate an Application "Cleanly" in Win32')
    LVL 86

    Expert Comment

    If you are running Win9x, there's a system setting for the command window and you cannot do much about that. On W2k and above, I'd try to

    - remove 'CREATE_NEW_CONSOLE'

    - run the app hidden using

       ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

       si.cb           =   sizeof  (   STARTUPINFO);
       si.dwFlags      =   STARTF_USESHOWWINDOW;
       si.wShowWindow  =   SW_HIDE;
    LVL 86

    Expert Comment

    Oh, BTW, if the program you launched already has exited, terminating it is not very useful and might be risky if you chose to close all comsoles...
    LVL 1

    Expert Comment

    Try this:


    Author Comment

    I'm running this same code in another program and I'm not having this problem.

    TerminateProcess doesn't do anything.

    and I'm using Win98... doh! though it will be used on other Win OS too.

    Author Comment

    changing  CREATE_NEW_CONSOLE to NULL does nothing also
    LVL 86

    Accepted Solution

    >>TerminateProcess doesn't do anything.

    Since the process isn't running any more, that won't help anyway (as I already mentioned)

    Click on the console's system menu and choose properties (or find the appropriate applet in the control panel). There you'll find an option that controls this behaviour, set that to not wait until the user presses a key.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now