C ++ How to hide the console window at startup?

I want to know how to hide the console window at startup.

I want to be honest and tell you about the keylogger program, but it's not my intention to hack someone. This is for a small school project that I want to do to show the dangers for hackers. (I thought it would be great to show something like this at school.)

I hope you help me with this.

So. Here is the code I wrote looking at it on google. (Ignore all comments about intentions.)

#include <cstdlib> #include <iostream> #include <Windows.h> using namespace std; int main() { /* Note. This program is only created to show the risk of being unaware of hackers. * This program should never be used to actually hack someone. * Therefore this program will never be avaiable to anyone, except me. */ 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; } 

As you can see, I turned on Windows.h and wrote FreeConsole (); mostly.

Yes, I see that the window appears and immediately disappears. But immediately after that a new console opens, which is simply empty. (With a space, I mean: "Press any key to continue .." I think about whether this has anything to do with the "system (" PAUSE ")"

So, I want to know why it opens a new console instead of just creating and hiding the first one.

Thanks.:)

+6
source share
9 answers

To literally hide / show the console window on demand, you can use the following functions: You can hide / show the console using ShowWindow . GetConsoleWindow retrieves the window handle used by the console. IsWindowVisible can be used to check if a window is visible (in this case, the console) or not.

 #include <Windows.h> void HideConsole() { ::ShowWindow(::GetConsoleWindow(), SW_HIDE); } void ShowConsole() { ::ShowWindow(::GetConsoleWindow(), SW_SHOW); } bool IsConsoleVisible() { return (::IsWindowVisible(::GetConsoleWindow()) != FALSE); } 
+4
source

So, I want to know why it opens a new console instead of just creating and hiding the first one.

The console application does not actually create a console, it just launches in one. If you run the executable from Explorer, Windows creates a console to run it. When you call FreeConsole , it does not close the new console, it just separates your process from it.

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

+3
source

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

+3
source

Hiding the console window at startup is actually impossible in the code, because the executable is launched by the operating system with certain settings. That is 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 need to add a special compiler. If you use gcc on Windows (MinGW), you can simply add -mwindows as a compiler option to your make file and there will be absolutely no window or "flash". I don’t know about VisualStudio or what you are currently using, but changing the way you compile your IDE that you code is a way to go instead of coding workarounds in C ++.

In my opinion, this approach is better than using WinMain , because it works reliably, and you do not make your C ++ Code platform dependent.

+2
source

Just change the application type from “Console Application” to “Windows Application” (and change your main to WinMain ). In this case, your application will be launched without a console window.

+1
source
 #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; } 
+1
source

It's simple. FreeConsole () api will do the magic for you

 BOOL WINAPI FreeConsole(VOID); 
+1
source

Just do it at startup

 myConsole = GetConsoleWindow(); ShowWindow(myConsole,0); 
+1
source
 #include <windows.h> ShowWindow(GetConsoleWindow(), SW_HIDE); //SW_RESTORE to bring back 

This will return the window handle (HWND) to myConsole , and ShowWindow will hide it. This solution, of course, will only work on Windows systems.

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

edit: A possible solution would be to install (in visual studio) Linker-> System-> SubSystem on "Windows (/ SUBSYSTEM: WINDOWS)" instead of "Console (/ SUBSYSTEM: CONSOLE)". This causes the console to not appear, which avoids flickering.

0
source

Source: https://habr.com/ru/post/951838/


All Articles