谁能解释下面的代码,这是用来隐藏的控制台窗口,而在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);
}
发布于 2018-09-16 18:10:04
让我们把它分成更小的部分:
void hide();
int main()
{
hide();
MSG Msg;
//IO::MKDir(IO::GetOurPath(true));
//InstallHook();
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
上面的循环是所谓的。由于windows GUI程序是事件驱动的,因此可以使用such a loop is the pattern为windows进程处理传入的windows消息。由于您的程序将接收WM_QUIT
消息,因此GetMessage()
将返回FALSE
,并且循环将退出。TranslateMessage()
只是将虚拟按键消息转换为字符消息,以便在需要时进一步向下处理。DispatchMessage()
的作用是确保发送到特定窗口的消息能够找到到达该窗口的WindowProc callback的途径。
//MailTimer.Stop();
return 0;
}
void hide()
{
HWND stealth;
AllocConsole();
上面的最后一行是allocating a new console for the process.。如果您的进程已经有了一个控制台,则此调用将失败,返回零表示。
stealth = FindWindowA("ConsoleWindowClass", NULL);
FindWindowA()
所做的是向具有指定类名和窗口名的窗口返回一个窗口句柄(HWND
)。在这里,它是在省略名称(第二个参数)并仅指定类的情况下使用的,对于这个类,在控制台窗口的情况下只能存在一个窗口,其类是"ConsoleWindowClass"
。
ShowWindow(stealth, 0);
}
All this line does隐藏了由传递的句柄标识的窗口,这是我们的控制台窗口,我们在代码片段中已经知道了这一点。本例中的第二个参数0
是SW_HIDE
的枚举器值,因此在该行之后将隐藏控制台窗口。
https://stackoverflow.com/questions/52352526
复制相似问题