如何在启动时隐藏控制台窗口?

时间:2021-02-23 00:21:47

I want to know how to hide a console window when it starts.

我想知道如何在控制台窗口启动时隐藏它。

I want to be honest and tell you 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. (I thought it could be pretty cool to show off something like this in school.)

说实话,我想告诉你,这是一个键盘记录程序,但我并不想去黑别人。这是我想做的一个学校小项目,展示黑客的危险。(我觉得在学校里炫耀这样的东西会很酷。)

I hope you will help me with this.

我希望你能帮助我。

So. Here is the code that i wrote by looking it up on google. (Don't mind all the notes about the 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 included the Windows.h and wrote FreeConsole(); in the main.

如你所见,我包括了窗户。h和写FreeConsole();在主。

Yes, i see the window appearing and immediately disappear. But it seems to open a new console right after that, which is just blank. (With blank, i mean: "Press any key to continue.." I'm thinking about if it has anything to do with the "system("PAUSE")")

是的,我看到窗户出现了,马上就消失了。但在那之后,它似乎打开了一个新的控制台,这只是空白。(空格,我的意思是:“按任意键继续…”我在想,它是否与“系统(暂停)”有关?

So i wanna know why it opens a new console, instead of just only create and hide the first one.

所以我想知道为什么它会打开一个新的控制台,而不仅仅是创建和隐藏第一个控制台。

Thanks. :)

谢谢。:)

9 个解决方案

#1


5  

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.

要按需隐藏/显示控制台窗口,可以使用以下函数:可以使用ShowWindow隐藏/显示控制台。GetConsoleWindow检索控制台使用的窗口句柄。IsWindowVisible可用于检查窗口(在这种情况下是控制台)是否可见。

#include <Windows.h>

void HideConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}

void ShowConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}

bool IsConsoleVisible()
{
    return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}

#2


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.

控制台应用程序实际上并不自己创建控制台,而是在其中运行。如果您从资源管理器运行可执行文件,Windows将为它创建一个控制台来运行。当您调用FreeConsole时,它不会关闭新的控制台,而是将您的进程与它分离。

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

正如WhozCraig在评论中指出的,创建一个常规的Windows应用程序,不要创建一个窗口。

#3


4  

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

#4


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

#5


3  

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上使用gcc (MinGW),你可以在你的makefile中添加-mwindows作为编译器选项,那里绝对没有窗口或“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++代码平台。

#6


2  

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

它是简单的。FreeConsole() api将为您实现这一神奇功能

BOOL WINAPI FreeConsole(VOID);

#7


2  

Just do that on startup

刚开始的时候就这么做

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

#8


2  

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

这将返回一个windows句柄(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.

编辑:一种可能的解决方案是(在visual studio中)将链接器—>系统—>子系统设置为“Windows(/子系统:Windows)”而不是“控制台(/子系统:控制台)”。然而,这可能不是最佳选择。

#9


1  

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 appplication”(并将您的main改为WinMain)。在这种情况下,您的应用程序将在没有控制台窗口的情况下启动。

#1


5  

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.

要按需隐藏/显示控制台窗口,可以使用以下函数:可以使用ShowWindow隐藏/显示控制台。GetConsoleWindow检索控制台使用的窗口句柄。IsWindowVisible可用于检查窗口(在这种情况下是控制台)是否可见。

#include <Windows.h>

void HideConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}

void ShowConsole()
{
    ::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}

bool IsConsoleVisible()
{
    return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}

#2


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.

控制台应用程序实际上并不自己创建控制台,而是在其中运行。如果您从资源管理器运行可执行文件,Windows将为它创建一个控制台来运行。当您调用FreeConsole时,它不会关闭新的控制台,而是将您的进程与它分离。

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

正如WhozCraig在评论中指出的,创建一个常规的Windows应用程序,不要创建一个窗口。

#3


4  

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

#4


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

#5


3  

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上使用gcc (MinGW),你可以在你的makefile中添加-mwindows作为编译器选项,那里绝对没有窗口或“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++代码平台。

#6


2  

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

它是简单的。FreeConsole() api将为您实现这一神奇功能

BOOL WINAPI FreeConsole(VOID);

#7


2  

Just do that on startup

刚开始的时候就这么做

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

#8


2  

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

这将返回一个windows句柄(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.

编辑:一种可能的解决方案是(在visual studio中)将链接器—>系统—>子系统设置为“Windows(/子系统:Windows)”而不是“控制台(/子系统:控制台)”。然而,这可能不是最佳选择。

#9


1  

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 appplication”(并将您的main改为WinMain)。在这种情况下,您的应用程序将在没有控制台窗口的情况下启动。