I am using a keyboard-hook with code in a DLL like this:
#pragma comment(linker, "/SECTION:.shared,RWS")
HHOOK hHook = NULL;
BOOL WINAPI DllMain (HINSTANCE hInst, DWORD dwReason, PVOID pvReserved)
if (dwReason == DLL_PROCESS_ATTACH)
hInstance = hInst;
LRESULT CALLBACK KeyboardProc(int iCode, WPARAM wParam, LPARAM lParam)
if (iCode >= 0 )
if ((iCode == HC_ACTION) && (! (lParam & 0x80000000) ))
MessageBox(NULL, "MsgBox", "A key was pressed", MB_ICONINFORMATION);
__declspec (dllexport) BOOL KeylogOff()
hHook = NULL;
__declspec (dllexport) BOOL KeylogOn()
hHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) KeyboardProc, (HINSTANCE) hInstance, 0);
MessageBox(NULL,"Cannot set Keyboard hook","Error",MB_OK);
This code works, but if I run another program, that uses a keyboard-hook (even another instance of my program, runned from other directory), he's in front of the hook chain, and I'm not getting messages. Is there a function, that informs me, what is my current position in hook chain? Is my KeyboardProc function passing messages to next hook right?
Another problem is in MS-DOS. If I run MS-DOS prompt in Windows, my program doesn't get keyboard messages from that window. I've tried WH_GETMESSAGE filter, but I don't know, what message I have to capture.
Thanks in advance.