• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1657
  • Last Modified:

"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.
1 Solution
I don't see what's wrong at the moment, but have you compared your code to this Microsoft example:
Bengal_TigerAuthor Commented:
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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Bengal_TigerAuthor Commented:

Thanks for the response.  I am using Windows XP, and I am logged in as Admin.
Try WH_KEYBOARD_LL hook. It is much better to use. Specifically, it is possible to install global keyboard keyboard hook without using hook Dll - hook function can be placed inside of exe file.
Bengal_TigerAuthor Commented:

The WH_KEYBOARD_LL hook worked.

Thanks for all the help!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now