-4

Can anyone explain the following code, which was used to hide the console windows while doing Keylogger project in c++

void hide();
int main()
{
    hide();
    MSG Msg; 

    //IO::MKDir(IO::GetOurPath(true));

    //InstallHook();

    while (GetMessage(&Msg, NULL, 0, 0)) 
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    //MailTimer.Stop();
    return 0;
}

void hide()
{
    HWND stealth; 
    AllocConsole();
    stealth = FindWindowA("ConsoleWindowClass", NULL);
    ShowWindow(stealth, 0);
}
  • at first for what `AllocConsole();` ? for what need console at all ? at second in system can be any count of console windows. as result code wrong and incorrect at all – RbMm Sep 16 '18 at 08:56
  • Please clarify whether your final goal to understand this code or to actually hide the window. If it's the latter then the best way is not to create a window at all. – r3mus n0x Sep 16 '18 at 09:05
  • to understand the code –  Sep 16 '18 at 09:23
  • @madhurachanna well it uses `AllocConsole` to create a console window, it gets a handle to that window using `FindWindow`, taking advantage of the fact that the console window class is `"ConsoleWindowClass"`, then it hides the console window using `ShowWindow`. – john Sep 16 '18 at 09:34
  • The fact that `main` is used and not `WinMain` means the app is a console app and so already has a console window, so `AllocConsole()` is redundant. And [`GetConsoleWindow()`](https://learn.microsoft.com/en-us/windows/console/getconsolewindow) should be used instead of `FindWindow()`. If there are multiple console windows onscreen at a time, `FindWindow()` is not guaranteed to find the right window. – Remy Lebeau Sep 16 '18 at 17:42

1 Answers1

2

Let's break it into smaller pieces:

void hide();
int main()
{
    hide();
    MSG Msg; 

    //IO::MKDir(IO::GetOurPath(true));

    //InstallHook();

   while (GetMessage(&Msg, NULL, 0, 0))
   {
       TranslateMessage(&Msg);
       DispatchMessage(&Msg);
   }

The above loop is what's called a message pump. As windows GUI programs are event-driven, such a loop is the pattern to handle incoming windows messages for your windows process. As your program will be receiving the WM_QUIT message, GetMessage() will return FALSE and the loop will exit. TranslateMessage() is just there to translates virtual-key messages to character messages, for further handling down the pipeline in case you need that. DispatchMessage() is there to make sure messages being sent to specific windows will find their way to that window's WindowProc callback.

    //MailTimer.Stop();
    return 0;
}

void hide()
{
    HWND stealth; 
    AllocConsole();

The last line above is allocating a new console for the process.. In case your process already has a console this call fails, indicated by returning zero.

    stealth = FindWindowA("ConsoleWindowClass", NULL);

What FindWindowA() does is return a window handle (HWND) to the window that has the specified class name and window name. Here it is used while omitting the name (second argument) and specifying only the class, for which only a single window could exist in this case of a console window, the class of which is "ConsoleWindowClass".

    ShowWindow(stealth, 0);
}

All this line does is hiding the window identified by the handle being passed, which is our console window, as we already know by this point in the snippet. The second argument 0 in this case is the enumerator value of SW_HIDE, hence after this line the console window is being hidden.

Geezer
  • 5,600
  • 18
  • 31
  • Was the question what simple or the framing of the question was bad? –  Sep 16 '18 at 10:35
  • @madhurachanna Well people shouldn't really be downvoting due to the question being plain *simple*. That said, some people inevitably do. However, this amount of votes is probably tied to the fact you didn't specify enough what exactly you're asking about. If this answer solved it for you then perhaps you can see which exact parts did the trick and then edit your question to make it more focused in accordance with that. – Geezer Sep 16 '18 at 10:43