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