CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Can anyone tell me what the problem is about this global mouse hook?

    I have installed a global mouse hook. Then when I finish doing something, I call UnhookWindowsHookEx to cancel this hook. But when I want to install the hook again, the function SetWindowsHookEx returns NULL. If I close my program, which installed and canceled the hook just now, and restart it, it will work the first time I install the hook, but fail again at the second time.

    Anybody knows what the problem is?
    And how to fix it?

    Thanks a million.
    Stone

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

    Re: Can anyone tell me what the problem is about this global mouse hook?

    What kind of global hook?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  3. #3
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Re: Can anyone tell me what the problem is about this global mouse hook?

    WH_MOUSE

    I find that WH_KEYBOARD encounters the same problem.
    Stone

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

    Re: Can anyone tell me what the problem is about this global mouse hook?

    I have just tested this type of hook and I can kill it and restart it at will.
    Could you post your project?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    May 2004
    Posts
    45

    Re: Can anyone tell me what the problem is about this global mouse hook?

    Quote Originally Posted by stoneyrh
    If I close my program, which installed and canceled the hook just now, and restart it, it will work the first time I install the hook, but fail again at the second time.
    Windows has a protection against ill-behaved hooks. If an exception occurs inside such a hook, the hook is never called again until you exit the program that installed the hook - even if you reinstall the hook inside your program. (It checks the address of the hook; if the hook had generated an exception, Windows does not install the hook again.)

    So check if there is something wrong happening inside your hook.
    And remember that it's unadvisable to do any elaborate processing inside a hook - ideally they must only set a flag or put (or queue) a value in some shared memory, maybe PostMessage (not even SendMessage) to somewhere; the actual processing must be done outside the hook.
    If you call the innocent function "fprintf", for instance, it will use a lot of things of the standard C library, and it will cause problems. It's a simple example of things that can go wrong.
    Debug your program using the APIs "OutputDebugString" and "wsprintf" and the "dbgview" program that you can download at http://www.sysinternals.com .

  6. #6
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Re: Can anyone tell me what the problem is about this global mouse hook?

    What I want to do is to lock a computer. That is to say, to prevent the system from sending all the mouse and keyboard inputs to all applications when the hooks(mouse hook and keyboard hook) is installed. Who wants to unlock the system must input a password which is set before locking the system. So in my mouse hook, I simply return TRUE. But in my keyboard hook, I do something to check the password. And if it is right, it will send a message to the locking process. Then the main process will unlock the system. Maybe the problem is as Thingol said. I think I should Post a message but Send a message.
    Yes?
    Stone

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

    Re: Can anyone tell me what the problem is about this global mouse hook?

    For that type of think you do not WH_MOUSE. You can use WH_MOUSE_LL. The only limitation is that you cannot do that with earlier version of windows.

    See this thread and download BlockKeyboard.zip.

    I have also attached small app with WH_MOUSE global hook and ability to set and us-set hook.
    Attached Files Attached Files
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Re: Can anyone tell me what the problem is about this global mouse hook?

    Oh, thank you, JohnCz. I will study the examples carefully.

    By the way, I don't know exactly what use of the first parameter-nCode-of hook procedures. MSDN says that if it is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx. But when on earth will it be less than zero? At that time, what on earth kind of action does the system conduct?
    Stone

  9. #9
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Re: Can anyone tell me what the problem is about this global mouse hook?

    I want to use WH_MOUSE_LL, but the compiler can't recognize it.
    Maybe I should upgrade it to VC7.0.
    Stone

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

    Re: Can anyone tell me what the problem is about this global mouse hook?

    Not necessarily, you should upgrade to the latest version of SDK.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  11. #11
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Re: Can anyone tell me what the problem is about this global mouse hook?

    OK, I found the new SDK and downloaded it.
    Thanks a million.
    Stone

  12. #12
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Can anyone tell me what the problem is about this global mouse hook?

    Quote Originally Posted by JohnCz
    ...I have also attached small app with WH_MOUSE global hook and ability to set and us-set hook.
    John,

    I have not made a dll-involved project before, and I can't get this one to link and/or execute. I have VS6.0. Here's what I tried.

    I set the active configuration to MouseHookDlg - Win32 Debug and tried to build the project. The linker complained that "LINK : fatal error LNK1104: cannot open file "HookDll.lib". So, at the top of the MouseHookDlgDlg.cpp file, which seemed to call all of the dll functions, I added the following:
    Code:
    #ifdef _DEBUG
    #pragma comment( lib, "HookDLL/Debug/HookDll.lib" )
    #else
    #pragma comment( lib, "HookDLL/Release/HookDll.lib" )
    #endif
    Rebuilding, I got the same linker error, but the above seemed correct and I really didn't think I should have. So I switched active configs to release (i.e., to MouseHookDlg - Win32 Release) and tried building again. This time it linked with no errors.

    But then it (the release version) wouldn't execute:
    Code:
    MouseHookDlg.exe - Unable To Locate Component
    ---------------------------
    This application has failed to start because HookDll.dll was not found. Re-installing the application may fix this problem. 
    ---------------------------
    OK   
    ---------------------------
    Sorry for the long post, but I'm really confused on what to do or try next. Can you help please?

    Regards,
    Mike

  13. #13
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Can anyone tell me what the problem is about this global mouse hook?

    OK, I didn't try hard enough in the project settings. It all works now. Here's what I did:

    1. I inserted the #pragma's indicated above

    2. In Project->Settings, I made the following changes:
    a. In HookDll release setting, on the Link page, I changed the Output File to ../Release/HookDll.dll (before the change, it was missing the ../Release part)
    b. In the MouseHookDlg Debug settings, also on the Link page, I deleted an entry for "HookDll.lib" from the Object/Library Modules box

    And everything worked just fine.

    Sorry again for not trying harder before my post.

    Regards,
    Mike

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