Click to See Complete Forum and Search --> : Keyboard Hook only works the first time


Yeb
June 30th, 1999, 02:10 AM
I have a system wide Keyboard Hook located in a DLL that is called by another application. The Hook is set and removed by two functions inside the DLL.

The first time I set the hook, it works great. If I remove the hook and then restart it (from the calling application) it only works if the calling application has the input focus or until I close the application and restart it.

I am using Visual C++ 6 on Windows 95.

If anyone has any advice I would appreciate it.

Thanks
Yaakov


Below is a snipit of the code from the DLL.

[ccode]
#pragma data_seg(".THOOKS")
HHOOK hKBHook = NULL;
HWND hHookUpWnd = NULL;
HINSTANCE hInstance = NULL;
#pragma data_seg()

BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpReseved)
{
hInstance = hinstDll;
return (TRUE);
}

__declspec(dllexport) void HookUp(HWND hWnd)
{
if( !hKBHook) {
MessageBeep(-1);
hHookUpWnd = hWnd;
hKBHook = SetWindowsHookEx( WH_KEYBOARD, KeyboardFunc, hInstance, (DWORD) NULL);
assert( hKBHook != NULL);
}
}

__declspec(dllexport) void UnHookUp()
{
if( hKBHook) {
UnhookWindowsHookEx( hKBHook);
hKBHook = NULL;
hHookUpWnd = NULL;
MessageBeep(-1);
}
}


LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam )
{
MessageBeep(-1);
MessageBeep(-1);
MessageBeep(-1);
return 1;

}

[ccode]

July 16th, 1999, 12:48 PM
Hi Yaakov
Your problem is that your hook procedure KeyBoardfunc must return 0 not 1.
If you want your code to be corect you must use next source:

LRESULT CALLBACK KeyboardProc (int nCode, WPARAM wParam, LPARAM lParam )
{
MessageBeep(-1);
MessageBeep(-1);
MessageBeep(-1);

return CallNextHookEx(hKBHook, code, wParam, lParam);
}


Martin
e-mail: martidim@hotmail.com