基本键记录服务无法在工作线程中记录键

Basic key logging service fails to log keys in worker thread

最近我尝试从一个简单的键盘记录器开始编程服务。原始代码非常粗糙,但它可以正常工作,前提是当前 window 是重点。当我试图通过将记录器添加到服务来缓解此问题时,问题就开始了。但是,我想在不使用挂钩的情况下记录密钥。这是工作线程:

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

所有其他代码只是启动和处理服务。

I want to log keys without using hooks however.

抱歉,但是在服务中,您必须使用键盘挂钩,否则它无法检测到用户输入,因为它是一个非可视化过程运行的背景。用户无需输入与服务直接关联的内容。