title: SetWindowsHookEx, identifying the windows dialog where the mouse is pointing
body: <p>This is another question I have related to <code>setWindowsHookEx</code>, I have written small Application to hook <code>dll</code> into windows <code>calculator applicatio</code>. Basically, <code>exe</code> application inject the <code>dll</code> into <code>calculator</code> and it monitors all the mouse down events happening at the calculator application. And currently , I am able to disable all the mouse down events happening at the calculator. however, I want to get it done based on the location the mouse is pointing. Specifically,  when the mouse button is clicking on specific menu bar item. I tried to get this done by using GetWindowText to retrieve the text where button is pointing, but most of the time it return the window text instead of the window item ( <code>menu item</code>) it is pointing. Following is the code that I have written for this. Any idea on how to disable a particular window element on menu bar.</p>
  
  <p>EXE code which call SetWindowsHookEx</p>
  
  <pre><code>// program.exe.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  #include &lt;Windows.h&gt;
  #include &lt;string&gt;
  #include  &lt;io.h&gt;
  
  using namespace std;
  
  void Usage()
  {
      printf("Usage: InjectDLL pid path-to-dll [-privilege]");
  }
  
  
  int _tmain(int argc, char* argv[])
  {
  
      /*
      * Load library in which we'll be hooking our functions.
      */
      HMODULE dll = LoadLibrary(L"C:\\drivers\\dllinject.dll");
      if (dll == NULL) {
          printf("The DLL could not be found.\n");
          getchar();
          return -1;
      }
  
      /*
      * Get the address of the function inside the DLL.
      * _hookmethod@12 is used to hook into 32bit applications
      */
      //HOOKPROC addr = (HOOKPROC)GetProcAddress(dll, "_hookmethod@12");
      HOOKPROC addr = (HOOKPROC)GetProcAddress(dll, "hookmethod");
      if (addr == NULL) {
          printf("The function was not found.\n");
          getchar();
          return -1;
      }
  
      /*
      * Hook the function.
      */
      DWORD procID=0;
  
      HWND targetWnd = FindWindowA("CalcFrame", "Calculator");
  
  
      wchar_t msgBuf[1024] = L"";
      wchar_t msgBuf2[1024] = L"";
      wsprintf(msgBuf, L"the thread Id is :  %d , process id is  : %d ", threadID, procID);
  
      OutputDebugString(msgBuf);
  
      //
      //WH_KEYBOARD_LL 
      HHOOK handle = SetWindowsHookEx(WH_GETMESSAGE, addr, dll, threadID);
      //HHOOK handle = SetWindowsHookEx(WH_CALLWNDPROC, addr, dll, threadID);
      //HHOOK handle = SetWindowsHookEx(WH_MSGFILTER, addr, dll, threadID);
      DWORD x = GetLastError();
      wsprintf(msgBuf2, L"the last error is %d", x);
      OutputDebugString(msgBuf2);
      if (handle == NULL) {
          printf("The KEYBOARD could not be hooked.\n");
      }
      else{
          printf("Program successfully hooked.\nPress enter to unhook the function and stop the program.\n");
      }
  
      /*
      * Unhook the function.
      */
  
      getchar();
      UnhookWindowsHookEx(handle);
  
      return 0;
  }
  </code></pre>
  
  <p>dll which is used to block the clicking of menu bar item.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  
  static HWND s_hWndButton;
  
  INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) {
  
      switch (Reason) {
      case DLL_PROCESS_ATTACH:
          OutputDebugString(L"DLL attach function called11.\n");
          break;
      case DLL_PROCESS_DETACH:
          OutputDebugString(L"DLL detach function called.\n");
          break;
      case DLL_THREAD_ATTACH:
          OutputDebugString(L"DLL thread attach function called.\n");
          break;
      case DLL_THREAD_DETACH:
          OutputDebugString(L"DLL thread detach function called.\n");
          break;
      }
  
      return TRUE;
  }
  
  extern "C" __declspec(dllexport) LRESULT  __stdcall hookmethod(int code, WPARAM wParam, LPARAM lParam) {
  
      TCHAR   msgBuftext[65536];
      TCHAR   msgBufhandle[65536];
      TCHAR   msgBufhandletext[65536];
      TCHAR   msgBufwintext[2048];
      if (code &gt;= 0) {
          MSG* cp = (MSG*)lParam;
          int txtlen = GetWindowTextLengthW(cp-&gt;hwnd);
          GetWindowText(cp-&gt;hwnd, msgBufhandletext, txtlen);
  
          wsprintf(msgBufhandle, L"Handle %X", cp-&gt;hwnd);
          wsprintf(msgBufwintext, L"Window Text %s", msgBufhandletext);
  
          LPMSG msg = (LPMSG)lParam;
  
          if (msg-&gt;message == WM_LBUTTONDOWN) {
              OutputDebugString(msgBufwintext);
  
              msg-&gt;message = WM_NULL;
  
          }
  
      }
  
      return(CallNextHookEx(NULL, code, wParam, lParam));
  }
  </code></pre>