Solved

System-wide hook does not work with Explorer?

Posted on 2003-11-15
7
783 Views
Last Modified: 2013-12-03
Hi,

In my program, I have one WH_MOUSE hook which monitors the mouse throughout the system.
As an example, I put the following code into hook procedure

      if (wParam == WM_NCLBUTTONDOWN)
      {
            if (pMst->wHitTestCode == HTCAPTION)
            {
                  TCHAR szTemp[256];
                  GetModuleFileName(NULL, szTemp, 256);
                  SetWindowText(pMst->hwnd, szTemp);
            }
      }

I check if wParam is WM_NCLBUTTONDOWN, and if so whether the mouse cursor is on title bar or not.
If it is, then i get the module file name and change the window title to be its module file name.

Everything works fine for most programs like notepad, messenger, internet explorer.
Notepad will be replaced by C:\Windows\notepad.exe
So i think there is nothing wrong with the code.
However, explorer does not work (like My Computer)
It looks like the hook procedure does not catch the message for explorer.

So i tried the following code
      
      HWND hProgman = FindWindow(TEXT("Progman"), TEXT("Program Manager"));
      DWORD dwThreadID;
      dwThreadID = GetWindowThreadProcessId(hProgman, NULL);

      if (dwThreadID != NULL)
            hProgmanHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)ProgmanProc, g_hInstanceDll, dwThreadID);

Instead of putting a system-wide hook, but put the hook right into Progman (Which i think is explorer.exe)
However, the code still does not work.

Is there anyway to get message from explorer?
Thanks.
0
Comment
Question by:CoolTsubasa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9755234
>>However, explorer does not work (like My Computer)

This is most likely to be the case because these are 'special' windows that might ignore the attempt to change their caption. Try

              TCHAR szTemp[256];
              GetModuleFileName(NULL, szTemp, 256);
              _tcscat ( szTemp, _T("\n"));
             OutputDebugString ( szTemp);

and watch the output using a viewer like http://www.sysinternals.com/ntw2k/freeware/debugview.shtml - you will notice that hooks DO work in explorer.exe also :o)

0
 

Author Comment

by:CoolTsubasa
ID: 9755242
Yeah i have tried that too
No message ouput

Actually the code will do the following
Get base name, output it to debugger by OutputDebugString, and set it to the window
I can see all other file names except explorer.exe
0
 
LVL 86

Expert Comment

by:jkr
ID: 9760786
Have you tried outputting that info for all messages but only the one you mentioned above?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:CoolTsubasa
ID: 9761071
I tried couple messages, some of them output the string, and some of them did not.
(the message came from ProgmanHook, but not WH_GETMESSAGE hook that i used above.)

I changed WH_MOUSE to WH_CALLWNDPROC, and added the following code
if (nCode == HC_ACTION)
{
    OutputDebugString("ABC");
}
So basically it produces the message regardless of what message is.

but the number of message output i got was really small (only about 4,5 messages)
When I start My Computer, it fired about 5 messages, and that's it.
It does not do anything when i move the window or click the window
0
 

Author Comment

by:CoolTsubasa
ID: 9762039
I have just re-written the entire code, and the code works for explorer now.

The problem was the following part
      if (nCode == HC_ACTION)
      {
            if (wParam == PM_REMOVE)
            {
                  // Check if the user has clicked the titlebar
                  if (pMsg->message == WM_NCLBUTTONDOWN)

if i take out wParam == PM_REMOVE, it works fine.
I do not know why this line prevents from the program to run correctly though.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10021561
PAQed, with points refunded (300)

Computer101
E-E Admin
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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