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?
Re: Can anyone tell me what the problem is about this global mouse hook?
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 .
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?
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?
Re: Can anyone tell me what the problem is about this global mouse hook?
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:
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?
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
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.