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. High CPU with Mouse Global Hook

High CPU with Mouse Global Hook

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++question
1 Posts 1 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.
  • R Offline
    R Offline
    redfish34
    wrote on last edited by
    #1

    From Code Project articles i have coded a C++ DLL to be used as a global hook for C# projects. I have been successful in getting a global keyboard hook working. But when i try the same with a mouse hook, computer CPU goes to 100% and C# app gets a million messages that say the same thing. I am not the greatest C++ programmer. Am i missing something here? I think i have all the code parts needed? Below is the code i am using (40+ lines): #include "stdafx.h" #include "hookdll.h" #pragma data_seg("SHARED") HHOOK m_mouseHook = NULL; HWND m_hHookClient = NULL; #pragma data_seg() #pragma comment(linker, "/SECTION:SHARED,RWS") // linker directive HINSTANCE m_hHookDLL = NULL; const int GH_MOUSEOTHER = WM_USER + 4099; BOOL APIENTRY DllMain( HANDLE hInstance, DWORD callReason, LPVOID reserved) { switch (callReason) { case DLL_PROCESS_ATTACH: m_hHookDLL = (HINSTANCE)hInstance; break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern "C" __declspec(dllexport) void InstallHook(HWND hWnd) { m_mouseHook = NULL; m_hHookClient = hWnd; // set mouse hook m_mouseHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseHookProc, m_hHookDLL, NULL); } extern "C" __declspec(dllexport) void RemoveHook() { UnhookWindowsHookEx(m_mouseHook); } LRESULT CALLBACK MouseHookProc( int nCode, WPARAM wParam, LPARAM lParam) { if (nCode >= 0) { PostMessage(m_hHookClient, GH_MOUSEOTHER, wParam, lParam); } // pass control to next hook in the hook chain return CallNextHookEx(NULL, nCode, wParam, lParam); } -- modified at 6:15 Wednesday 22nd March, 2006

    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