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. Please help me to solve a problem about global mouse hook

Please help me to solve a problem about global mouse hook

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
2 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
    capint
    wrote on last edited by
    #1

    I created a dll file like this :

    HHOOK MyHook;
    HINSTANCE MyInstance;
    LRESULT CALLBACK MyMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
    extern "C" __declspec(dllexport) void Hook();
    extern "C" __declspec(dllexport) void Unhook();

    //I initialized MyInstance
    BOOL CMouseHook1App::InitInstance()
    {
    CWinApp::InitInstance();
    MyHook = NULL;
    MyInstance = this->m_hInstance;
    return TRUE;
    }

    void Hook()
    {
    if(MyHook == NULL && MyInstance != NULL )
    {
    MyHook = SetWindowsHookEx(WH_MOUSE,MyMouseHookProc,MyInstance, 0);
    if(MyHook!= NULL)
    MessageBox(NULL,_T(" HOOKED "), _T(""),0);
    }
    else
    MessageBox(NULL,L" Not Hooked", L"",0);

    }

    LRESULT CALLBACK MyMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    if(nCode <0)
    {
    CallNextHookEx(MyHook,nCode,wParam,lParam);
    return 0;
    }

    if(wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
    {
    MessageBox(NULL,_T("OK"),_T("Test"),MB_OK);
    PostMessage(hWndApp,WM_LBUTTONDOWN,0,0);
    }

    return CallNextHookEx(MyHook,nCode,wParam,lParam);

    }

    But in my app, when I use the function Hook(), it doesn't work at all. Did I do anything wrong? Someone plz help me

    _ 1 Reply Last reply
    0
    • C capint

      I created a dll file like this :

      HHOOK MyHook;
      HINSTANCE MyInstance;
      LRESULT CALLBACK MyMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
      extern "C" __declspec(dllexport) void Hook();
      extern "C" __declspec(dllexport) void Unhook();

      //I initialized MyInstance
      BOOL CMouseHook1App::InitInstance()
      {
      CWinApp::InitInstance();
      MyHook = NULL;
      MyInstance = this->m_hInstance;
      return TRUE;
      }

      void Hook()
      {
      if(MyHook == NULL && MyInstance != NULL )
      {
      MyHook = SetWindowsHookEx(WH_MOUSE,MyMouseHookProc,MyInstance, 0);
      if(MyHook!= NULL)
      MessageBox(NULL,_T(" HOOKED "), _T(""),0);
      }
      else
      MessageBox(NULL,L" Not Hooked", L"",0);

      }

      LRESULT CALLBACK MyMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
      {
      if(nCode <0)
      {
      CallNextHookEx(MyHook,nCode,wParam,lParam);
      return 0;
      }

      if(wParam == WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
      {
      MessageBox(NULL,_T("OK"),_T("Test"),MB_OK);
      PostMessage(hWndApp,WM_LBUTTONDOWN,0,0);
      }

      return CallNextHookEx(MyHook,nCode,wParam,lParam);

      }

      But in my app, when I use the function Hook(), it doesn't work at all. Did I do anything wrong? Someone plz help me

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      You need to declare the HHOOK MyHook; in a shared data section. So you need to first create a data section, declare the HHOOK in that data section and then make that section shared. Look at my article at http://www.codeproject.com/KB/DLL/ParkMouse.aspx[^]

      « Superman »

      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