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

后端 未结 9 592
悲哀的现实
悲哀的现实 2021-02-01 05:43

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 littl

相关标签:
9条回答
  • 2021-02-01 05:43
    #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.

    0 讨论(0)
  • 2021-02-01 05:49
    #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;
    }
    
    0 讨论(0)
  • 2021-02-01 05:49

    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.

    0 讨论(0)
  • 2021-02-01 05:51

    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.

    0 讨论(0)
  • 2021-02-01 05:54

    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.

    0 讨论(0)
  • 2021-02-01 05:54

    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

    0 讨论(0)
提交回复
热议问题