【发布时间】:2016-05-04 22:49:49
【问题描述】:
最近我尝试从一个简单的键盘记录器开始尝试编程服务。原始代码非常粗糙,但是如果当前窗口处于焦点状态,它就可以工作。当我试图通过将记录器添加到服务来缓解此问题时,问题就开始了。但是,我想在不使用钩子的情况下记录密钥。这是工作线程:
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam){
std::ofstream File("Info.txt");
char C = 0;
// Periodically check if the service has been requested to stop
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0){
if(kbhit()){
C = getch();
File.open("Info.txt", std::fstream::out | std::fstream::app);
File << C;
File.close();
Sleep(100);
//The thread needs to sleep so that it won't eat CPU
//Additionally people cannot type as fast as a computer can process data
}
}
return ERROR_SUCCESS;
}
所有其他代码只是启动并处理服务。
【问题讨论】:
-
kbhit和getch读取用户在程序的控制台窗口中键入的内容。作为一项服务,您的程序没有控制台窗口。 -
WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0对于可能只是测试线程安全布尔值的东西来说有点重量级。while (not terminated)
标签: c++ multithreading logging service keylogger