Link to home
Start Free TrialLog in
Avatar of CoolTsubasa
CoolTsubasa

asked on

System-wide hook does not work with Explorer?

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.
Avatar of jkr
jkr
Flag of Germany image

>>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)

Avatar of CoolTsubasa
CoolTsubasa

ASKER

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
Have you tried outputting that info for all messages but only the one you mentioned above?
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial