0

I'm trying to hook a button click in a mfc window.And I used WM_COMMAND and WM_NOTIFY.For test, if the button is clicked, the hook must create a messagebox. The problem is that it doesn't react to button clicks. Here's the code for reacting to WM_COMMAND and WM_NOTIFY:

LPMSG msg = (LPMSG)lParam;

    switch( msg->message )
    {
    case WM_COMMAND:
        MessageBox( NULL,"HOOK","YOOOO",MB_ICONEXCLAMATION );
        break;
    case WM_NOTIFY:
        MessageBox( NULL,"HOOK","YOOOOO",MB_ICONEXCLAMATION );
        break;
    }

And here's the code to hole dll:

#include <Windows.h>
#include "FindingWindow.h"
#pragma comment( linker,"/SECTION:.SHARED,RWS" )
#pragma data_seg( ".SHARED" )
CaptureTextWindow* ctw;
HHOOK hook = 0;
HMODULE hInstance = 0;
HWND hWndServer = NULL;
#pragma data_seg()

static LRESULT CALLBACK msghook(int nCode, WPARAM wParam, LPARAM lParam);
__declspec(dllexport) BOOL clearMyHook(HWND hWnd);

BOOL APIENTRY DllMain( HINSTANCE hInst, DWORD ul_reason_for_call, LPVOID lpReserved )
{
    switch( ul_reason_for_call )
    {
    case DLL_PROCESS_ATTACH:
        hInstance =  hInst;
        return TRUE;
    case DLL_PROCESS_DETACH:
        if(hWndServer != NULL)
          clearMyHook(hWndServer);
        return TRUE;
    }   
    return TRUE;
}



__declspec(dllexport) BOOL WINAPI setMyHook(HWND hWnd)
  {
   if(hWndServer != NULL)
      return FALSE;
   hook = SetWindowsHookEx(
                           WH_CALLWNDPROC,
                           (HOOKPROC)msghook,
                           hInstance,
                           0);
   if(hook != NULL)
   { /* success */
      hWndServer = hWnd;
      return TRUE;
   } /* success */
   return FALSE;
}
__declspec(dllexport) BOOL clearMyHook(HWND hWnd)
{
    if(hWnd != hWndServer)
       return FALSE;
    BOOL unhooked = UnhookWindowsHookEx(hook);
    if(unhooked)
       hWndServer = NULL;
    return unhooked;
}

static LRESULT CALLBACK msghook( int nCode,        // hook code
                                   WPARAM wParam ,  // message identifier
                                   LPARAM lParam )  
{
    if( nCode < 0 )
    {
        CallNextHookEx( hook, nCode, wParam, lParam );
        return 0;
    }
    LPMSG msg = (LPMSG)lParam;

    switch( msg->message )
    {
    case WM_COMMAND:
        MessageBox( NULL,"HOOK","YOOOO",MB_ICONEXCLAMATION );
        break;
    case WM_NOTIFY:
        MessageBox( NULL,"HOOK","YOOOOO",MB_ICONEXCLAMATION );
        break;
    }

    return CallNextHookEx( hook, nCode, wParam, lParam );
}

I used not only WM_COMMAND cause I thought mb it will work, but it didn't.Thanks for answer.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Oleksandr Verhun
  • 814
  • 1
  • 8
  • 23
  • The Dialog Box displayed by the MessageBox API will receive a WM_COMMAND message if you click on Ok. I don't know how the hooking system will handle that, but you may end in a very bad reentrancy dead lock. – manuell Dec 24 '13 at 13:09

1 Answers1

2

The lParam for a WH_CALLWNDPROC hook is of type CWPSTRUCT. Your CallWndProc should look like this:

// ...
const CWPSTRUCT& cwps = *(CWPSTRUCT*)lParam;
switch ( cwps.message ) {
case WM_COMMAND:
    MessageBox( ... );
    break;
// ...
IInspectable
  • 46,945
  • 8
  • 85
  • 181