title: How properly hook WM_COMMAND
body: <p>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:</p>
  
  <pre><code>LPMSG msg = (LPMSG)lParam;
  
      switch( msg-&gt;message )
      {
      case WM_COMMAND:
          MessageBox( NULL,"HOOK","YOOOO",MB_ICONEXCLAMATION );
          break;
      case WM_NOTIFY:
          MessageBox( NULL,"HOOK","YOOOOO",MB_ICONEXCLAMATION );
          break;
      }
  </code></pre>
  
  <p>And here's the code to hole dll:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #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 &lt; 0 )
      {
          CallNextHookEx( hook, nCode, wParam, lParam );
          return 0;
      }
      LPMSG msg = (LPMSG)lParam;
  
      switch( msg-&gt;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 );
  }
  </code></pre>
  
  <p>I used not only WM_COMMAND cause I thought mb it will work, but it didn't.Thanks for answer.</p>