SetWindowsHookEx and dwThreadId

andrewyu
andrewyu used Ask the Experts™
on
How can I monitor all the message (MOUSE and KEYBOARD) of the Windows even though the application is not active?

Can I use SetWindowsHookEx and use 0 as the dwThreadId ? It seems that I was fail if I use it ?

Andrew
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
which application is not active?

Author

Commented:
If application A call a dll B which have a callback function for WH_MOUSE and WH_KEYBOARD.

How can I monitor all the message (MOUSE and KEYBOARD) of the Windows even though the application A is not active?

Can I use SetWindowsHookEx and use 0 as the dwThreadId ? It seems that I was fail if I use it ?

thanks,
Andrew
Commented:
Of course you need A running.  You can use 0 as thread id, if it does not work, your code is wrong, post your code if you need help.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
I wrote:

hdll=LoadLibrary("MOTION32".DLL");
MouseProc=GetProcAddress(hdll, "MouseProc");
SetWindows(WH_MOUSE, MouseProc, hdll, 0);
KeyboardProc=GetProcAddress(hdll, "KeyboardProc");
SetWindows(WH_KEYBOARD, KeyboardProc, hdll, 0);

but fail !

For the mouse hook, it can only work if the mouse in over the application A even the application A is active !
For keyboard hook, it can only work if the application A is active !

Why ?

Andew

Commented:
What do you mean by "A is active"?  Is A running?

The code you show is correct, if A has not been terminated and it does work, it is your hook proc that has problem.

Why only 5 points, this question is not the easiest I have ever seen.

Author

Commented:
Yes, A is ruuning and it is active ! Active means that it is the only active window !
Actually, the MouseProc can only work when the mouse is move over the window or click over the window even the application A is active as the KeyboardProc can only work when the application is active !

Andrew
(that's all the point I have)

Commented:
I am quite sure that it is working, it is only that you think it is not.  Please remember that a hook DLL is running in the context of the application being hooked.

Currently how do you know it is working or not working?

Author

Commented:
I know it doesn't work because the MouseProc and the KeyBoard is also written by me as there is no response sometime !

Actually, I want to know will the MouseProc called when the mouse is moving outside the application A ?
And I also want to know will the KeyboardProc called when I press a key on another application ?

thanks,
Andrew

Commented:
Yes, the hook procedure is called no matter which is the active application.

I know the hook proc is written by you, but how do you know it is not called?  Probably it is called, but you think it is not.  Lots of programmers make the same mistake in hooks and they all think that it is not being called.

Author

Commented:
Actually, if the MouseProc or KeyboardProc called, it would updated a counter, so, I know it is not called !!!!!!

Commented:
OK, my guess is right, you make the common mistake.

Try to use a counter is OK, but the problem is whether you put the counter (a global variable, right?) in the right place.  As I have said, the dll is running in the context of the process being hooked (there are a lot of such processes at any given time), so let's there is another process B, then both A and B will have its OWN counter!  Therefore when B is active, the hook procedure only updates the variable for B, so in A you see it as not updated.

To fully understand this, you must have a good knowledge of win32 processes and dlls.

To solve this problem, you must put the counter in a shared memory section, this way all the processes will share the same variable.

In the .c file of your dll:

#pragma data_seg(".sdata")
int nCounter = 0;
#pragma data_seg()

In the .def file, add:

SECTIONS
   .sdata   READ WRITE SHARED

Refer to win32 document for shared memory.

I am sure this time it will work.

Author

Commented:
I see !!!!!!

Thank you very much indeed !!!!!!

Author

Commented:
Sorry for bother you again !

I've tried this already, but, the result is still the same !

And I also want to know whether this method can check the mouse action on the desktop ?

So ...... Do I need to do more on the program instead of the DLL ?

Andrew
PS:Thank you very much as I can't give you so much points

Commented:
there are must be something wrong in your code, post all your code.

Author

Commented:
Sorry for reply you so late as I am very sick and busy recently !

I have already show you the code above !
But, I have only add the following to the code !

In the .c file of your dll:

     #pragma data_seg("hookdata")
     int nCounter = 0;
     #pragma data_seg()

     In the .def file, add:

     SECTIONS
        hoookdata   READ WRITE SHARED

Commented:
show me the code in your hook procedure

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial