Solved

System-wide hook does not work with Explorer?

Posted on 2003-11-15
7
772 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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.

707 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

16 Experts available now in Live!

Get 1:1 Help Now