hkBattousai 0 Light Poster

I want to create a low level keyboard hook.
I tried the code below :

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0) return CallNextHookEx((HHOOK) WH_KEYBOARD_LL, (int) nCode, (WPARAM) wParam, (LPARAM) lParam);
    DebugText("KB Event!");
    return (LRESULT) NULL;
}

HHOOK hHook;
hHook = (HHOOK) SetWindowsHookEx(    (int)        WH_KEYBOARD_LL,
                                    (HOOKPROC)    LowLevelKeyboardProc,
                                    (HINSTANCE)    NULL,
                                    (DWORD)        GetCurrentThreadId());
CheckError();
UnhookWindowsHookEx((HHOOK) hHook);
CheckError();

There is no run time or compiler error, but GetLastError() doesn't return zero.
SetWindowsHookEx() returns this error :
Error code : 1429
Error constant : ERROR_GLOBAL_ONLY_HOOK
Error description : This hook procedure can only be set globally.
What is wrong in my code?

By the way, CheckError() is defined like this :

void DebugText(char * szText, int nText)
{
    char szNum[64];
    char szMsg[128];
    itoa(nText, szNum, 10);
    strcpy(szMsg, szText);
    strcat(szMsg, szNum);
    MessageBox(NULL, szMsg, "DebugText()", MB_ICONINFORMATION | MB_OK);
}

void CheckError(void)
{
    DWORD dwError = GetLastError();
    if (dwError != 0) DebugText("Error : ", dwError);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.