Link to home
Start Free TrialLog in
Avatar of Bengal_Tiger
Bengal_Tiger

asked on

"invalid hook procedure" error when I use SetWindowsHookEx() for WH_KEYBOARD, c++

I am trying to set a global Windows hook for WH_KEYBOARD messages.

I am using the following code:
      
      hModule = LoadLibrary("C:\\myDlls\\hookDll.dll");
      lpfn = (HOOKPROC)GetProcAddress( hModule,"GetMsgProc");
      hHook = SetWindowsHookEx(WH_KEYBOARD,lpfn,hModule, 0);

When I try to set a hook for WH_GETMESSAGE, everything works fine.  However, when I try to set the hook for WH_KEYBOARD, I get the "ERROR_INVALID_HANDLE" error.

Thanks in advance for your help.
Avatar of Anthony2000
Anthony2000
Flag of United States of America image

I don't see what's wrong at the moment, but have you compared your code to this Microsoft example:
http://msdn2.microsoft.com/en-us/library/ms644960.aspx
Avatar of Bengal_Tiger
Bengal_Tiger

ASKER

Thats the example I used to write my code.  Likewise, the code works as expected when I set a hook for WH_GETMESSAGE.  The error is only occurring when I try to set a hook for WH_KEYBOARD.
What version of windows are you using?  Is the program running as a regular user or as Admin?

I suspect Vista might restrict the kinds of hooks you can install.

grg99,

Thanks for the response.  I am using Windows XP, and I am logged in as Admin.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alex,

The WH_KEYBOARD_LL hook worked.

Thanks for all the help!