31

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

3
  • 1
    system spawns a new shell.. Commented Aug 15, 2013 at 19:44
  • 9
    Don't write it as a console program. Just code a WinMain() with no window. Use a messagebox or modal dialog for your screen-spam. Commented Aug 15, 2013 at 19:45
  • You have to specify the subsystem in the linker options. Commented Aug 15, 2013 at 19:52

10 Answers 10

44

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; } 
Sign up to request clarification or add additional context in comments.

2 Comments

SW_MINIMIZE instead of SW_HIDE to have the console hidden but still visible in the taskbar.
Hello nikau6, it is great to have your reply. Now I gain even more from reading your posting, too. I wanted, however, was the console completely hidden. The code in your original posting did the magic. It worked as I intended. I used it to bring up a GUI therefore I wanted the console to be completely hidden not even on the task bar. Great posting. Thank you again.
22

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.

1 Comment

The compiler flag is the way to go. For visual studio, you have this answer
15
#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.

1 Comment

Thank you. This works awesomely. Specifically, the ShowWindow(GetConsoleWindow(), SW_HIDE) solution. Also, I admire your boldness: "This is the correct answer..." And you were right, lol. Thanks again. Note to all: I had to #define _WIN32_WINNT as 0x0500 (or higher) prior to #include <windows.h> to get a compile. Apparently GetConsoleWindow() wasn't available until Windows 2000.
7
#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; } 

Comments

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.

Comments

5

You are writing a console program as the entry point is main(). For graphical based Windows applications, entry point should be WinMain http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559(v=vs.85).aspx

Comments

5

It is simple. FreeConsole() api will do that magic for you

BOOL WINAPI FreeConsole(VOID); 

Comments

4

Just do that on startup

myConsole = GetConsoleWindow(); ShowWindow(myConsole,0); 

2 Comments

auto myConsole = GetConsoleWindow(); ShowWindow(myConsole, 0); did the trick :D
Dynamic typing is strong with this one.
3

Just change the type of your application from "Console application" to "Windows appplication" (and change your main to WinMain). In this case, your application will be started without console window at all.

Comments

-1

8 years later lol

BUT

a simple solution for your project would be to simply use a .vbs

Dim WShell Set WShell = CreateObject("WScript.Shell") WShell.Run "xr.exe", 0 Set WShell = Nothing 

that will when ran start xr.exe hidden however windows defender thinks its a virus

1 Comment

Question was for C++.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.