-1

recently I have attempted to try my hand at programming services starting with a simple key logger. The original code is very crude however it works granted the current window is focused. The problem started when I attempted to alleviate this issue by adding the logger to a service. I want to log keys without using hooks however. Here is the worker thread:

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

All the other code just starts and handles the service.

ACpp8165b
  • 11
  • 3
  • 3
    `kbhit` and `getch` read things that users type into **your program's console window**. As a service, your program doesn't have a console window. – user253751 May 04 '16 at 22:54
  • 1
    `WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0` is a bit heavyweight for something that could just be a test of a thread-safe boolean. `while (not terminated)` – user4581301 May 04 '16 at 23:22

1 Answers1

0

I want to log keys without using hooks however.

Sorry, but in a service, you have to use a keyboard hook, otherwise it cannot detect user input since it is a non-visual process running in the background. There is nothing for the user to type into that is directly associated with the service.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770