C++ 如何在启动时隐藏控制台窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18260508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ How do I hide a console window on startup?
提问by mads232
I want to know how to hide a console window when it starts.
我想知道如何在启动时隐藏控制台窗口。
It's for a keylogger program, butit'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")
)
我看到窗口出现并在启动时立即消失。之后它似乎立即打开了一个新的控制台,它只是空白。(空白我的意思是“按任意键继续..”我想知道它是否与system("PAUSE")
)
So I want to know why it opens a new console, instead of only creating and hiding the first one.
所以我想知道为什么它会打开一个新的控制台,而不是只创建和隐藏第一个控制台。
Thanks. :)
谢谢。:)
回答by nikau6
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. GetConsoleWindowretrieves the window handle used by the console. IsWindowVisiblecan be used to checked if a window (in that case the console) is visible or not.
要根据需要隐藏/显示控制台窗口,您可以使用以下功能: 可以使用ShowWindow隐藏/显示控制台。GetConsoleWindow检索控制台使用的窗口句柄。 IsWindowVisible可用于检查窗口(在这种情况下是控制台)是否可见。
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return ::IsWindowVisible(::GetConsoleWindow()) != FALSE;
}
回答by user3932876
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++.
在您的代码中隐藏启动时的控制台窗口是不可能的,因为可执行文件是由具有特定设置的操作系统运行的。这就是为什么当您使用时控制台窗口在启动时会显示很短的时间,例如FreeConsole();
要在启动时真正隐藏窗口,您必须向编译器添加一个特殊选项。如果您在 Windows (MinGW) 上使用 gcc,您只需-mwindows
在 makefile 中添加作为编译器选项,绝对不会有窗口或“flash”。我不知道 VisualStudio 或你现在使用的任何东西,但改变你的 IDE 编译你代码的方式是要走的路,而不是在 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.
在我看来,这种方法比使用更好,WinMain
因为它工作可靠,并且您不会使您的 C++ 代码平台依赖。
回答by udit043
#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;
}
回答by Turch
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.
控制台应用程序实际上并不创建控制台本身,它只是在一个控制台中运行。如果您从资源管理器中运行可执行文件,Windows 会创建一个控制台供其运行。当您调用 时FreeConsole
,它不会关闭新控制台,只是将您的进程与其分离。
As WhozCraig noted in the comments, create a regular Windows application and don't create a window.
正如 WhozCraig 在评论中指出的那样,创建一个常规的 Windows 应用程序而不是创建一个窗口。
回答by segfault
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
您正在编写控制台程序,因为入口点是main()
. 对于基于图形的 Windows 应用程序,入口点应该是WinMain
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559(v=vs.85).aspx
回答by Charlie
#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.
这将返回一个窗口句柄(HWND),ShowWindow()
然后将其隐藏。此解决方案仅适用于 Windows 系统。
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.
编辑:一个可能的解决方案/hack 可能是(在 Visual Studio 中)将链接器->系统->子系统设置为“Windows(/SUBSYSTEM:WINDOWS)”而不是“控制台(/SUBSYSTEM:CONSOLE)”。然而,这可能不是最佳的。
回答by qehgt
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.
只需将您的应用程序类型从“控制台应用程序”更改为“Windows 应用程序”(并将您的更改main
为WinMain
)。在这种情况下,您的应用程序将在没有控制台窗口的情况下启动。
回答by user1520846
It is simple. FreeConsole()api will do that magic for you
很简单。 FreeConsole()api 会为你做这个魔术
BOOL WINAPI FreeConsole(VOID);
回答by Alex Sha
Just do that on startup
只需在启动时执行此操作
myConsole = GetConsoleWindow();
ShowWindow(myConsole,0);