C++ How do I hide a console window on startup?

c++, console-application, windows

Solution

To literally hide/show the console window on demand, you could use the following functions: It's possible to hide/show the console by using ShowWindow. GetConsoleWindow retrieves the window handle used by the console. IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.

#include <Windows.h>

void HideConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}

void ShowConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}

bool IsConsoleVisible()
{
    return ::IsWindowVisible(::GetConsoleWindow()) != FALSE;
}

Problem

I want to know how to hide a console window when it starts. It's for a keylogger program, but it's not my intention to hack someone. It's for a little school project that I want to make to show the dangers about hackers. Here's my code so far: ``` #include <cstdlib> #include <iostream> #include <Windows.h> using namespace std; int main() { cout << "Note. This program is only created to show the risk of being unaware of hackers." << endl; cout << "This program should never be used to actually hack someone." << endl; cout << "Therefore this program will never be avaiable to anyone, except me." << endl; FreeConsole(); system("PAUSE"); return 0; } ``` I see the window appear and immediately disappear at startup. It seems to open a new console right after that, which is just blank. (By blank I mean "Press any key to continue.." I'm wondering if it has anything to do with `system("PAUSE")`) So I want to know why it opens a new console, instead of only creating and hiding the first one. Thanks. :)

Original source

Related problems