most of the time the following works, but sometimes it doesn't. It is as if Windows XP has a limit on the number of hooks or something like that. Incidentally, when this doesn't work, the Windows Start Menu starts to behave strangely (can't navigate it normally using the keyboard or mouse any more), so the system seems to really have reached a limit. But what LIMIT???
HookHandle=SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) KeyboardProc,
HInstance and threadid are always correct. It's just that sometimes Windows is able to install the hook and sometimes it isn't.
If it fails, GetLastError is 126 (Module not found). The module that isn't found could either be the caller (a DLL), or the host application which is being hooked (an EXE). I don't know which one this error refers to. Obviously both modules exist, and all module-related handles and IDs are correct.