Share via


Hiding a console window in c++?

Question

Tuesday, September 29, 2009 1:30 AM

Hi guys, i am trying to hide a console window in c++ but the problem is that it still shows up for a quick millisecond before it disperses.

#include <windows.h>
#include <iostream>

int main()
{
    for(;;){
HWND hide;
AllocConsole();
hide = FindWindowA("ConsoleWindowClass",NULL);
ShowWindow(hide,0);
    }
return(0);
    
}




any way to fix it so that it dosent show up at all?

All replies (5)

Tuesday, September 29, 2009 3:22 AM âś…Answered

Just don't ask for a console.  Project + Properties, Linker, System, change SubSystem to Windows.  Replace your main() function definition with this:

int __stdcall WinMain(void*, void*, char* cmdLine, int)
{
    // etc...

    return 0;
}

Hans Passant.


Tuesday, September 29, 2009 2:52 AM

Ummm, hang on a minute. You're running a console application. It already has a console open
when it starts. Therefore, AllocConsole() will likely fail since you can only have one console
per process. Test the return to see if AllocConsole() fails.

  • Wayne

Tuesday, September 29, 2009 7:53 AM | 1 vote

The lazy approach: ShowWindow( GetConsoleWindow(), SW_HIDE);

Regards.

Dev s r'us


Tuesday, September 29, 2009 3:38 PM

Hi, when i try to use

ShowWindow( GetConsoleWindow(), SW_HIDE);

the widnows still shows up. Maybe i am putting the code in the wrong spot?

#include <windows.h>
#include <iostream>
using namespace std;
int main()
{

//hides consoel windows =D  
ShowWindow( GetConsoleWindow(), SW_HIDE);

    for(;;){
HWND hide;
AllocConsole();
hide = FindWindowA("ConsoleWindowClass",NULL);
ShowWindow(hide,0);
    }
    
return(0);
    
}

Tuesday, September 29, 2009 4:58 PM

This is bound to happen since you are running a console application.
With a console application, Windows gives the application a console long before you get to main. So in this time it is going to be drawn before you hide it.
There is no way around this if you are planning on using a console application.

If you want to use a console window then you are going to have to put up with that flash.

Also as stated earlier, your call to AllocConsole is unnecessary and will fail.

As taken from the AllocConsole remarks section "A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console."

If you don't want a console to show then the only way is as nobugz said, change the project to a GUI application then the console will not even be allocated to the process and so you won't even see it for a millisecond.Visit my (not very good) blog at http://c2kblog.blogspot.com/