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

Posted on 2007-08-06
Last Modified: 2013-12-03
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.
Question by:Bengal_Tiger
    LVL 8

    Expert Comment

    I don't see what's wrong at the moment, but have you compared your code to this Microsoft example:

    Author Comment

    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.
    LVL 22

    Expert Comment

    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.


    Author Comment


    Thanks for the response.  I am using Windows XP, and I am logged in as Admin.
    LVL 48

    Accepted Solution

    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.

    Author Comment


    The WH_KEYBOARD_LL hook worked.

    Thanks for all the help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now