CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Mar 2005
    Posts
    9

    Question I have a real problem with hooks

    Hi all,
    i'm trying to monitor the system for hotkeys like powerstrip it monitors for hotkeys to increase or decrease gamma or whatever while any thing is active the first thing i thought about was hooks so i wrote my hook procedure in a dll setup the hook like this

    ///code in exe
    gkbhookdll = LoadLibrary("kbhook.dll");//load dll
    gkeyboardhook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)GetProcAddress(gkbhookdll,"KeyProc"),gkbhookdll,0);

    i specified 0 for last parameter to be system wide every thing was ok

    ///code in dll
    LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    MessageBox(NULL,"called hook","called hook",MB_OK);
    return CallNextHookEx(0,nCode,wParam,lParam);
    }

    now strange problem is that when i activate another window then the hook doesn't work even if i reactivated my window, any one knows what's happening.
    does CallNextHookEx returns something that Unhooks my hook?
    Thanks for advance and if any one knows a better way for monitoring system for specific hotkeys please tell me, thanks every body again.
    and if some body knows how to format parameters returned from the hook with CHotKeyCtrl.GetHotKey is welcome because of the parameter holding wModifiers.
    The hook can only give you the code of one button and tell you if alt is pressed. Thanks for the third time.
    Last edited by snakekain; March 4th, 2005 at 07:43 AM.

  2. #2
    Join Date
    Feb 2005
    Posts
    24

    Arrow Re: I have a real problem with hooks

    Hello,

    How are you? I don't know the reason of your problem. Sorry! I'm a beginner of hook systems!

    I work under Windows CE, and I want to ask you, if you know sites web, where I could find examples about hook functions. Ask everybody!

    Thanks,

    javitobcn

  3. #3
    Join Date
    Mar 2005
    Posts
    9

    Re: I have a real problem with hooks


  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: I have a real problem with hooks

    Quote Originally Posted by snakekain
    now strange problem is that when i activate another window then the hook doesn't work even if i reactivated my window, any one knows what's happening.
    does CallNextHookEx returns something that Unhooks my hook?
    Thanks for advance and if any one knows a better way for monitoring system for specific hotkeys please tell me, thanks every body again.
    and if some body knows how to format parameters returned from the hook with CHotKeyCtrl.GetHotKey is welcome because of the parameter holding wModifiers.
    The hook can only give you the code of one button and tell you if alt is pressed. Thanks for the third time.
    A better way? Well ... I would use RegisterHotkey.

    Quote Originally Posted by MSDN
    Calling CallNextHookEx is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.
    Well maybe there is a program that doesn't want you to receive hook's.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: I have a real problem with hooks

    Maybe you need to replace this:
    LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)

    with this:
    __declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)

    But this would be if its not working at all. But you say that the hook works until you switch to another app? You do see the 'called hook' message box at first? If thats so then the __declspec probably isnt the solution.

  6. #6
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: I have a real problem with hooks

    Quote Originally Posted by Martin O
    Maybe you need to replace this:
    LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)

    with this:
    __declspec(dllexport) LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)

    But this would be if its not working at all. But you say that the hook works until you switch to another app? You do see the 'called hook' message box at first? If thats so then the __declspec probably isnt the solution.
    No, if he uses a definition file (.def extension) he doesn't need the __declspec() export method:

    Code:
    LIBRARY MyHookDll
    
    EXPORTS
    
         KeyProc @1
    This will do the same. And I recommend this method if you use LoadLibrary() and GetProcAddress().
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  7. #7
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: I have a real problem with hooks

    Sorry I was just comparing with my hook code & that was the only difference I saw but now that I think about it that shouldnt matter (your not exporting the hook procedure so you dont need it).

    Anyway heres my KeyboardHook dll project. It's pretty simple, use

    StarHook(GetCurrentProcessId(), hWindowHandle);
    // Tell the hook procedure to send keyboard messages from different processes to this process' hWindowHandle

    then call SetHook(true);

    and call EndHook() at the end of the program.
    Attached Files Attached Files
    Last edited by Martin O; March 4th, 2005 at 10:13 AM.

  8. #8
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: I have a real problem with hooks

    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  9. #9
    Join Date
    Mar 2005
    Posts
    9

    Smile Re: I have a real problem with hooks

    Thank you guys for all those replys, of course i exported the function using the def file, when i read the tutorial from http://www.codeguru.com/Cpp/W-P/syst...cle.php/c5699/ it setup the hook from the dll but i did it from the exe. I looked at RegisterHotKey (thanks to god then to NoHero) which is a more convenient way it worked very well but a small problem exist however that if i set the hot key to Q for example switching to another task pressing Q doesn't work for the current window as if i'm not pressing it but the message box appears at my window that means all apps will not be aple to work properly any help will be appreciated, thanks again for advance.

  10. #10
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: I have a real problem with hooks

    Sorry, I do not understand your problem. Would you mind to explain it again, more in detail?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  11. #11
    Join Date
    Mar 2005
    Posts
    9

    Exclamation Re: I have a real problem with hooks

    Thanks nohero here's the problem i use
    RegisterHotKey(m_hWnd,1,0,(int)'Q'); to setup the hot key at the start up of the program when i start the program the wm_hotkey message key is sent with the right parameters, if i leave my program open and activate notepad to write something in it if i pressed the hot key 'Q' in this example then nothing happen the notedpad doesn't write any thing may be that means it didn't recieve wm_keydown message that happens for every other window, thanks guys again.
    Last edited by snakekain; March 6th, 2005 at 11:27 AM.

  12. #12
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: I have a real problem with hooks

    Quote Originally Posted by snakekain
    Thanks nohero here's the problem i use
    RegisterHotKey(m_hWnd,1,0,(int)'Q'); to setup the hot key at the start up of the program when i start the program the wm_hotkey message key is sent with the right parameters, if i leave my program open and activate notepad to write something in it if i pressed the hot key 'Q' in this example then nothing happen the notedpad doesn't write any thing may be that means it didn't recieve wm_keydown message that happens for every other window, thanks guys again.
    Of course. This is normal behaviour of RegisterHotkey. Just read MSDN:

    The RegisterHotKey function defines a system-wide hot key.
    If you want the hotkey to only work in your application then you should use Accelerators. This works as followed:

    • Create a new accelerator resource in the resource editor.
    • Assign a hotkey to an ID of a button (for example)
    • Use LoadAccelerators before the main loop and ...
    • Use the returned handle in the main loop with TranslateAccelerator .


    This should result in the following code:

    [ prototype code ]

    Code:
    MSDN msg      = {0};
    int Ret       = 0;
    HACCEL hAccel = NULL;
    
    // Load resource
    hAccel = LoadAccelerators(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MYACCELTABLE));
    
    // Assuming that hwnd is the handle to your window
    while ( (bRet = GetMessage(&msg, hwnd, 0, 0)) != 0 )
    {
       if ( bRet == -1 )
       {
          // error
       }
       else
       {
          if ( !TranslateAccelerator(hwnd, hAccel, &msg) )
          {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
          }
       }
    }
    If you have any further problems with it, you should post your entire project, so I can help you out directly at the kernel of the problem. If you have any further questions on this, feel free to ask.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  13. #13
    Join Date
    Mar 2005
    Posts
    9

    Smile Re: I have a real problem with hooks

    Thank you for your reply, sorry for taking a lot of your time, i need a system-wide hotkey but i don't want it to prevent every other application from recieving the key because that will annoy the user, i want my app in the system tray recieving the hot key messages, while the user works with other apps uninterrupted.

    In the example i gave the user will be bothered a lot finding that pressing Q will not write Q in the note pad or any other app while my app is working.

    If that'll not work i guess working with system-wide hooks will do because it doesn't prevent other apps from recieving the messages except if i returned 1, and that means i'll have to find a work around to work with the hotkey format returned from CHotKeyCtrl. Thanks again alot, you really helped me out.

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

    Re: I have a real problem with hooks

    Quote Originally Posted by snakekain
    gkbhookdll = LoadLibrary("kbhook.dll");//load dll
    gkeyboardhook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)GetProcAddress(gkbhookdll,"KeyProc"),gkbhookdll,0);
    Where is your KeyProc?

    Your hook works. Providing you start your hook using StartHook.
    You are blocking all keystrokes for all windows but one: the one with handle you send when calling StartHook.

    If you want to monitor keyboard it is easier by using WH_KEYBOARD_LL. This is always global hook even if procedure is in an exe module.

    AS for CHotKeyCtrl you can pass its window handle and use it to translate keyboard input into a code and key value.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  15. #15
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: I have a real problem with hooks

    Hello.

    To create a global hotkey that does not interfere with other applications, you can use DirectInput (it is not so complicated to use, and very powerful).

    Or you can call at short-intervals, GetAsyncKeyState (under Windows 98, GetAsyncKeyState retrives asynchronously system-wide key strokes, but i don't know if it works under all windows because GetKeyState does not retrives system-wide key strokes).

Page 1 of 2 12 LastLast

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