Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute child console programs without showing the console window from the Win32 GUI program?

(I've searched SO answers and found no clear solution to this problem.)

I'm working on a MFC GUI program. This program runs various child programs including console program and shell command script(.cmd).

Initially it displayed one GUI window and one console window (created with AllocConsole) because there are many console output from the child processes. But many users complained about the console window so we decided to hide the console window.

Firstly tried like below:

if (AllocConsole())
{
    ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}

Okay, no console window but there are visible flicker at the console creation time. I've tried several CreateProcess options for child process creation to prevent showing of console window altogether but failed at short and I think it is practically impossible.

It is not a big deal. We can ignore temporary window flicker at the startup.

But is it really impossible to hide child console window completely?

like image 992
9dan Avatar asked Jan 20 '11 04:01

9dan


1 Answers

Setup the STARTUPINFO like this for the CreateProcess call:

    STARTUPINFO si = { 0 };
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    si.hStdOutput =  GetStdHandle(STD_OUTPUT_HANDLE);
    si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
    si.wShowWindow = SW_HIDE;
like image 144
John Avatar answered Oct 26 '22 00:10

John