Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 619
mads232 Avatar asked Aug 15 '13 19:08

mads232


People also ask

How do you hide a console window?

"SW_HIDE" hides the window, while "SW_SHOW" shows the window.

How do I open console programs without a console window?

If you do not know what I am talking about: press Win+R, type “help” and press ENTER. A black console window will open, execute the HELP command and close again. Often, this is not desired. Instead, the command should execute without any visible window.


1 Answers

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; } 
like image 159
nikau6 Avatar answered Sep 19 '22 08:09

nikau6