Solved

SetWindowsHookEx and dwThreadId

Posted on 1998-05-14
16
848 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:andrewyu
  • 8
  • 8
16 Comments
 
LVL 7

Expert Comment

by:faster
ID: 1401929
which application is not active?
0
 

Author Comment

by:andrewyu
ID: 1401930
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
 
LVL 7

Accepted Solution

by:
faster earned 50 total points
ID: 1401931
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
 

Author Comment

by:andrewyu
ID: 1401932
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
 
LVL 7

Expert Comment

by:faster
ID: 1401933
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
 

Author Comment

by:andrewyu
ID: 1401934
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
 
LVL 7

Expert Comment

by:faster
ID: 1401935
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
 

Author Comment

by:andrewyu
ID: 1401936
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Expert Comment

by:faster
ID: 1401937
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
 

Author Comment

by:andrewyu
ID: 1401938
Actually, if the MouseProc or KeyboardProc called, it would updated a counter, so, I know it is not called !!!!!!
0
 
LVL 7

Expert Comment

by:faster
ID: 1401939
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
 

Author Comment

by:andrewyu
ID: 1401940
I see !!!!!!

Thank you very much indeed !!!!!!
0
 

Author Comment

by:andrewyu
ID: 1401941
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
 
LVL 7

Expert Comment

by:faster
ID: 1401942
there are must be something wrong in your code, post all your code.
0
 

Author Comment

by:andrewyu
ID: 1401943
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
 
LVL 7

Expert Comment

by:faster
ID: 1401944
show me the code in your hook procedure
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now