• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Close cmd prompt window opened with CreateProcess

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 );
0
galneweinhaw
Asked:
galneweinhaw
1 Solution
 
AxterCommented:
See http://support.microsoft.com/support/kb/articles/Q178/8/93.ASP ('HOWTO: Terminate an Application "Cleanly" in Win32')
0
 
jkrCommented:
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;
0
 
jkrCommented:
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...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
stuartdehaanCommented:
Try this:
TerminateProcess(pi.hProcess,0);

Stuart
0
 
galneweinhawAuthor Commented:
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.
0
 
galneweinhawAuthor Commented:
changing  CREATE_NEW_CONSOLE to NULL does nothing also
0
 
jkrCommented:
>>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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now