SetWindowsHookEx and dwThreadId

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
andrewyuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fasterCommented:
which application is not active?
0
andrewyuAuthor 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
0
fasterCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

andrewyuAuthor 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
0
fasterCommented:
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.
0
andrewyuAuthor 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)
0
fasterCommented:
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?
0
andrewyuAuthor 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
0
fasterCommented:
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.
0
andrewyuAuthor Commented:
Actually, if the MouseProc or KeyboardProc called, it would updated a counter, so, I know it is not called !!!!!!
0
fasterCommented:
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.
0
andrewyuAuthor Commented:
I see !!!!!!

Thank you very much indeed !!!!!!
0
andrewyuAuthor 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
0
fasterCommented:
there are must be something wrong in your code, post all your code.
0
andrewyuAuthor 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
0
fasterCommented:
show me the code in your hook procedure
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.