Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use global hook with WH_GETMESSAGE

How to use global hook with WH_GETMESSAGE

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cedricvictor
    wrote on last edited by
    #1

    Dear all: I try to use global hook with WH_GETMESSAGE, but still failed. It work only on my application, not global. My code show as below:

    #pragma data_seg (".SHARED")
    HHOOK g_hPreviousMsgHook = 0;
    HINSTANCE g_hInstance = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.SHARED,RWS")

    BOOL CSoundDllApp::InitInstance()
    {
    CWinApp::InitInstance();
    g_hInstance = AfxGetInstanceHandle();
    return TRUE;
    }

    BOOL InstallWinHook()
    {
    BOOL bReturn = TRUE;
    g_hPreviousMsgHook = SetWindowsHookEx(WH_GETMESSAGE,
    &MsgHookProcedure, g_hInstance, 0);

    if(NULL == g\_hPreviousMsgHook)
    {
        bReturn = FALSE;
    }
    return bReturn;
    

    }

    LRESULT CALLBACK MsgHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(0 > nCode)
    return CallNextHookEx(g_hPreviousMsgHook, nCode,
    wParam, lParam);
    PMSG data = (PMSG)lParam;

    switch(data->message)
    {
    case WM\_POINTERDOWN:
       Beep(0x7fff, 1000);
    break;
    }
    return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam,   
                                                 lParam);
    

    }

    I set 0 into last parameter of setWindowsHookEx api, but it seem like not work. Could someone tell me where's wrong, please? Thanks for your help, Victor

    L 1 Reply Last reply
    0
    • C cedricvictor

      Dear all: I try to use global hook with WH_GETMESSAGE, but still failed. It work only on my application, not global. My code show as below:

      #pragma data_seg (".SHARED")
      HHOOK g_hPreviousMsgHook = 0;
      HINSTANCE g_hInstance = 0;
      #pragma data_seg()
      #pragma comment(linker, "/SECTION:.SHARED,RWS")

      BOOL CSoundDllApp::InitInstance()
      {
      CWinApp::InitInstance();
      g_hInstance = AfxGetInstanceHandle();
      return TRUE;
      }

      BOOL InstallWinHook()
      {
      BOOL bReturn = TRUE;
      g_hPreviousMsgHook = SetWindowsHookEx(WH_GETMESSAGE,
      &MsgHookProcedure, g_hInstance, 0);

      if(NULL == g\_hPreviousMsgHook)
      {
          bReturn = FALSE;
      }
      return bReturn;
      

      }

      LRESULT CALLBACK MsgHookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
      {
      if(0 > nCode)
      return CallNextHookEx(g_hPreviousMsgHook, nCode,
      wParam, lParam);
      PMSG data = (PMSG)lParam;

      switch(data->message)
      {
      case WM\_POINTERDOWN:
         Beep(0x7fff, 1000);
      break;
      }
      return CallNextHookEx(g\_hPreviousMouseHook, nCode, wParam,   
                                                   lParam);
      

      }

      I set 0 into last parameter of setWindowsHookEx api, but it seem like not work. Could someone tell me where's wrong, please? Thanks for your help, Victor

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This message is directly tied to your message queue as described at http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959(v=vs.85).aspx#whgetmessagehook[^].

      C 1 Reply Last reply
      0
      • L Lost User

        This message is directly tied to your message queue as described at http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959(v=vs.85).aspx#whgetmessagehook[^].

        C Offline
        C Offline
        cedricvictor
        wrote on last edited by
        #3

        Dear Richard: It mean I just hook message only from application message queue, so I can not get message from anthor place? When I touch on some applications, it works. When I touch on desktop, it does not work. How? Does it mean the touch message on desktop not exist in application message queue? Thanks for your help, Victor

        L 1 Reply Last reply
        0
        • C cedricvictor

          Dear Richard: It mean I just hook message only from application message queue, so I can not get message from anthor place? When I touch on some applications, it works. When I touch on desktop, it does not work. How? Does it mean the touch message on desktop not exist in application message queue? Thanks for your help, Victor

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sorry, my original message was incorrect (the MSDN description lacks clarity). This should capture all messages, not just those directed to your application.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups