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

时光总嘲笑我的痴心妄想 提交于 2020-06-24 05:06:07

问题


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


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



回答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.




回答3:


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



回答4:


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.




回答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




回答6:


#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.




回答7:


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.




回答8:


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

BOOL WINAPI FreeConsole(VOID);



回答9:


Just do that on startup

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


来源:https://stackoverflow.com/questions/18260508/c-how-do-i-hide-a-console-window-on-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!