tags:

views:

76

answers:

2

Hello

I want to set global hook that tracks which application is active.

In my main program I am doing the foloowing :

HMODULE mod=::GetModuleHandle(L"HookProcDll");
HHOOK rslt=(WH_CALLWNDPROCRET,MyCallWndRetProc,mod,0);

The hook procedure which is called MyCallWndRetProc exists in separate dll called HookProcDll.dll. The hook procedure is watching for WM_ACTIVATE message.

The thing is that the code stucks in the line where I am setting the hook, i.e in the line where I am calling ::SetWindowsHookEx. And then Windows gets unresponsive, my task bar disappears and I am left with empty desktop. Then I must reset the computer.

What am doing wrong, why Windows get unresponsive ? and Do I need to inject HookProcDll.dll in every process in order to set the global hook, and how can I do that ?

+1  A: 

This almost definitely sounds like a crashing bug in MyCallWndRetProc. You specify your hook DLL to get loaded in every process with a window and it crashes when MyCallWndRetProc gets called after a window message. Since it gets called after every window message in every process, it'll eventually take down every process displaying UI in the user session. You can't even launch new apps since your hook proc will be immediately loaded into them.

Including the code to MyCallWndRetProc, and perhaps your DllMain as well, should give us some insight into what is happening.

Michael
A: 

This is the code for my hook procedure, and it is stored in HookProcDll.dll :

#include "HookProcDll.h"
LRESULT CALLBACK MyCallWndRetProc(
  __in  int nCode,
  __in  WPARAM wParam, /* whether the msg is sent by the current process */
  __in  LPARAM lParam  /* pointer to CWPRETSTRUCT , which specifies details about the message */
)
{
    if(nCode >=0)
    {
        CWPRETSTRUCT* retStruct=(CWPRETSTRUCT*)lParam;
        if(retStruct->message == WM_ACTIVATE)
        {

        }
    }
    return ::CallNextHookEx(0,nCode,wParam,lParam);
}

My HookProcDll.dll has no explicit DllMain function .The HookProcDll.dll is made with visual studio dll project so I guess it includes standrad implementation for DllMain.