Solved

System-wide hook does not work with Explorer?

Posted on 2003-11-15
7
780 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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