?
Solved

System-wide hook does not work with Explorer?

Posted on 2003-11-15
7
Medium Priority
?
784 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

741 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