How to set global hook for WH_CALLWNDPROCRET?

I want to set a global hook that keeps track of which application is active.

In my main program, I do the following:

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

The hook procedure, which is called MyCallWndRetProc, exists in a separate dll called HookProcDll.dll. The hook routine keeps track of the message WM_ACTIVATE.

The fact is that the code is held in the line where I set the hook, i.e. in the line where I'm calling ::SetWindowsHookEx. And then Windows becomes unresponsive, my taskbar disappears, and I leave the desktop blank. Then I have to reset the computer.

What is doing wrong, why is Windows not responding? and also Do I need to enter HookProcDll.dll in every process to set the global hook, and how can I do this?

+3
source share
2 answers

It almost certainly sounds like a bug in MyCallWndRetProc. You specify your DLL load to load in each process using a window, and it crashes when MyCallWndRetProc is called after a window message. Since it is called after each message box in each process, it will ultimately kill every process that displays the user interface in a user session. You cannot even launch new applications, since your hook process will be immediately loaded into them.

Including the code in MyCallWndRetProc and perhaps your DllMain should also give us some idea of ​​what is going on.

+1
source

hook, 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 DllMain. HookProcDll.dll visual studio dll, , standrad DllMain.

0

Source: https://habr.com/ru/post/1747445/


All Articles