CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2004
    Posts
    21

    Can't set windows hook - what wrong with my code ?

    Hi All,

    I need to catch all keyboard event and some of the keyboard keys i need to intercept.
    So i using windows hook to do it.

    But in some reason when i do "SetWindowsHookEx" i getting wrong return code and my hook does not working.

    This is my code ( just the relevant part ):


    LRESULT CALLBACK KeyboardProc(int code, // hook code
    WPARAM wParam, // virtual-key code
    LPARAM lParam // keystroke-message information
    )
    {
    LRESULT lRetCode = 0;

    return lRetCode;
    }

    LRESULT SetHook()
    {

    HINSTANCE hinstance = NULL;
    HHOOK hookHandle = NULL;
    LRESULT lRetCode = 0;

    hookHandle = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hinstance, 0);

    return lRetCode;
    }



    What i did wronge ?

    Thanks for any help.

  2. #2
    Join Date
    Apr 1999
    Posts
    65

    Re: Can't set windows hook - what wrong with my code ?

    void main(void)
    {
    HMODULE hModule= LoadLibrar("yourhook.dll");
    fKeyProc fp = (fKeyProc) GetProcAddress(hModule, "KeyProc");
    SetWindowsHookEx(WH_KEYBOARD, fp, (HINSTANCE)hModule, 0);
    }

    Hope this helps.

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Can't set windows hook - what wrong with my code ?

    Is your code in a DLL?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Apr 1999
    Posts
    65

    Re: Can't set windows hook - what wrong with my code ?

    Yes, the procedure should be in a dll. Then only, can OS map it to all the processes

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Can't set windows hook - what wrong with my code ?

    Quote Originally Posted by YANSHOF
    This is my code ( just the relevant part ):
    You missed most relevant part: hook callback.

    Global hook in a DLL needs shared data segment. It is most likely that you did not use it.

    If you are going to use your hook only in NT family you can use low level keyboard hook that is always global and can be set with callback in n exe module.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Can't set windows hook - what wrong with my code ?

    You should make the HOOK handle globaly shared in DLL's shared segment,


    How To Share Data Between Different Mappings of a DLL
    Regards,
    Ramkrishna Pawar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured