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. Mouse hook

Mouse hook

Scheduled Pinned Locked Moved C / C++ / MFC
beta-testingquestioncode-reviewworkspace
3 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.
  • 0 Offline
    0 Offline
    0v3rloader
    wrote on last edited by
    #1

    Hello, I have an app installing a mouse hook: The hook setup function...

    BOOL CXCapture::Setup()
    {
    DFUNC_DEF(CXCapture::Setup);
    BOOL bRetVal;
    //!CODE_START--->
    m_hMouse = ::SetWindowsHookEx(WH_MOUSE, &MouseProc, ::AfxGetInstanceHandle(), NULL);
    bRetVal = m_hMouse != NULL;
    DFUNC_RET(bRetVal != FALSE, DSTR("Could not initialise mouse_hook (Err#%d)", GetLastError()));
    return bRetVal;
    }

    The callback function...

    LRESULT CALLBACK CXCapture::MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    DFUNC_DEF(CXCapture::MouseProc);
    //!CODE_START--->
    DTRACE(DSTR("Hook: %d::%d, %d", nCode, wParam, lParam));
    // now lets allow the message to be processed
    return ::CallNextHookEx(m_hMouse, nCode, wParam, lParam);
    }

    It happens that while the mouse is inside my application's (only) dialog box, the callback function (MouseProc) is called flawlessly, but when the mouse focus is out of the window, the OS simply unhooks it. Therefore, whenever the mouse leaves the app's window focus, I am left out with no mouse hook whatsoever!! My question is why this is happening and what should (or can) I do in order to solve this. All feedback is greatly appreciated. David Nimrod

    A 1 Reply Last reply
    0
    • 0 0v3rloader

      Hello, I have an app installing a mouse hook: The hook setup function...

      BOOL CXCapture::Setup()
      {
      DFUNC_DEF(CXCapture::Setup);
      BOOL bRetVal;
      //!CODE_START--->
      m_hMouse = ::SetWindowsHookEx(WH_MOUSE, &MouseProc, ::AfxGetInstanceHandle(), NULL);
      bRetVal = m_hMouse != NULL;
      DFUNC_RET(bRetVal != FALSE, DSTR("Could not initialise mouse_hook (Err#%d)", GetLastError()));
      return bRetVal;
      }

      The callback function...

      LRESULT CALLBACK CXCapture::MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
      {
      DFUNC_DEF(CXCapture::MouseProc);
      //!CODE_START--->
      DTRACE(DSTR("Hook: %d::%d, %d", nCode, wParam, lParam));
      // now lets allow the message to be processed
      return ::CallNextHookEx(m_hMouse, nCode, wParam, lParam);
      }

      It happens that while the mouse is inside my application's (only) dialog box, the callback function (MouseProc) is called flawlessly, but when the mouse focus is out of the window, the OS simply unhooks it. Therefore, whenever the mouse leaves the app's window focus, I am left out with no mouse hook whatsoever!! My question is why this is happening and what should (or can) I do in order to solve this. All feedback is greatly appreciated. David Nimrod

      A Offline
      A Offline
      AssemblySoft
      wrote on last edited by
      #2

      The Callback function needs to reside in a DLL. the each process can load the dll into it's own address space. Carl:suss:

      0 1 Reply Last reply
      0
      • A AssemblySoft

        The Callback function needs to reside in a DLL. the each process can load the dll into it's own address space. Carl:suss:

        0 Offline
        0 Offline
        0v3rloader
        wrote on last edited by
        #3

        Yeah, I've read about that just an hour ago or so and am modifying the code. Thanks for the reply. David

        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