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

75,364

Solution 1

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;
}

Solution 2

Hiding a console window at startup is not really possible in your code because the executable is run by the operating system with specific settings. That's why the console window is displayed for a very short time at startup when you use for example FreeConsole(); To really hide the window at startup, you have to add a special option to you compiler. If you use gcc on Windows (MinGW) you can just add -mwindows as compiler option in your makefile and there will be absolutely no window or "flash". I don't know about VisualStudio or whatever you use at the moment, but changing the way your IDE compiles you code is the way to go instead of coding workarounds in C++.

In my view, this approach is better than using WinMain because it works reliably and you don't make your C++ Code platform dependent.

Solution 3

#include <windows.h>
ShowWindow(GetConsoleWindow(), SW_HIDE); //SW_RESTORE to bring back

This will return a windows handle (HWND) to ShowWindow() which will in turn hide it. This solution is for windows systems only.

This is the correct answer to the question, even if its not marked as it.

edit: A possible solution/hack could be to set (in visual studio) Linker->System->SubSystem to "Windows (/SUBSYSTEM:WINDOWS)" instead of "Console (/SUBSYSTEM:CONSOLE)". This is probably not optimal however.

Solution 4

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

void Stealth()
{
 HWND Stealth;
 AllocConsole();
 Stealth = FindWindowA("ConsoleWindowClass", NULL);
 ShowWindow(Stealth,0);
}

int main()
{
  cout<<"this sentence is visible\n";
  Stealth(); //to hide console window
  cout<<"this sentence is not visible\n";
  system("PAUSE"); //here you can call any process silently like system("start chrome.exe") , so google chrome will open and will surprise user..
  return EXIT_SUCCESS;
}

Solution 5

So i wanna know why it opens a new console, instead of just only create and hide the first one.

A console application doesn't actually create a console itself, it just runs in one. If you run the executable from Explorer, Windows creates a console for it to run in. When you call FreeConsole, it doesn't close the new console, simply detaches your process from it.

As WhozCraig noted in the comments, create a regular Windows application and don't create a window.

Share:
75,364
mads232
Author by

mads232

Hello, my name is Mads. I'm a newbie on programming. I've done c++, c#, HTML, CSS, Javascript, PHP, Pawn, and MySQL. The one I like the most, would be c++.

Updated on July 09, 2022

Comments

  • mads232
    mads232 almost 2 years

    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. :)