// MouseHook.h #ifdef MOUSEHOOK_EXPORTS #define MOUSEHOOK_API __declspec(dllexport) #else #define MOUSEHOOK_API __declspec(dllimport) #endif extern "C" MOUSEHOOK_API void CALLBACK InstallMouseHook(void); extern "C" MOUSEHOOK_API void CALLBACK UnMouseHook(void); extern "C" MOUSEHOOK_API LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam); // MouseHook.cpp // MouseHook.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "MouseHook.h" #include <Pwinuser.h> // 告诉编译器将变量放入它自己的数据共享节中 #pragma data_seg("MouseHookData") HINSTANCE ghInst = NULL; HHOOK ghMouseHook = NULL; #pragma data_seg() // 告诉编译器设置共享节的访问方式为:读,写,共享 #pragma comment(linker, "/SECTION:MouseHookData,RWS") BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) { switch(ul_reason_for_call) { case DLL_PROCESS_ATTACH: ghInst = (HINSTANCE)hModule; break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern "C" MOUSEHOOK_API void InstallMouseHook(void) { RETAILMSG(1,(L"[Mouse HOOK]Install HOOK: 0x%x\r\n",ghInst)); if(ghInst) { ghMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, ghInst, 0); if(NULL == ghMouseHook) { RETAILMSG(1,(L"[Mouse HOOK]HOOK create failed: %d\r\n",GetLastError())); } } } extern "C" MOUSEHOOK_API void UnMouseHook(void) { RETAILMSG(1,(L"[Mouse HOOK]Uninstall HOOK:0x%x\r\n",ghMouseHook)); if(ghMouseHook) { UnhookWindowsHookEx(ghMouseHook); ghMouseHook = NULL; } ghInst = NULL; } /* nCode [输入参数] 说明 HOOK 过程如何处理此消息。如果 nCode 小于零, HOOK 过程必须将此消息直接传递给下一个 CallNextHookEx, 并从 CallNextHookEx 返回. 本参数可以取以下的值:HC_ACTION wParam 和 lParam 参数中包含鼠标消息的信息。 wParam [输入参数] 鼠标消息标识. 本参数取如下值之一: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_MOUSEHWHEEL, WM_RBUTTONDOWN, 或 WM_RBUTTONUP. lParam [输入参数] 指定 MSLLHOOKSTRUCT 结构体. 其中包含坐标等信息。 */ // CE6 的鼠标 HOOK 的响应速度很慢, 原因不明! extern "C" MOUSEHOOK_API LRESULT MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) { static POINT ptPre; #if _DEBUG RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,nCode is: %d\r\n",nCode)); RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,wParam is: %d\r\n",wParam)); #endif PMSLLHOOKSTRUCT pMouseHookStruct = (PMSLLHOOKSTRUCT)lParam; #if _DEBUG RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,mouseData is: %d\r\n",pMouseHookStruct->mouseData)); RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,dwExtraInfo is: %d\r\n",pMouseHookStruct->dwExtraInfo)); #endif if(HC_ACTION != nCode) { return CallNextHookEx(ghMouseHook, nCode, wParam, lParam); } if(ptPre.x != pMouseHookStruct->pt.x || ptPre.y != pMouseHookStruct->pt.y) { #if _DEBUG RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,pt is: %d,%d(%d,%d)\r\n",pMouseHookStruct->pt.x,pMouseHookStruct->pt.y,ptPre.x,ptPre.y)); #endif ptPre = pMouseHookStruct->pt; // 若注释掉此句, 则不再响应鼠标操作 return CallNextHookEx(ghMouseHook, nCode, wParam, lParam); // 继续传递消息 } return TRUE; } // 调用示例 // mouse hook test typedef void (*pInstallMouseHook)(void); typedef void (*pUnMouseHook)(void); HINSTANCE ghMouseModule = NULL; pInstallMouseHook InMouseHook = NULL; pUnMouseHook UnMouseHook = NULL; ghMouseModule = LoadLibrary(L"\\AppStore\\SystemInfo\\MouseHook.dll"); if(ghMouseModule) { InMouseHook = (pInstallMouseHook)GetProcAddress(ghMouseModule,L"InstallMouseHook"); UnMouseHook = (pUnMouseHook)GetProcAddress(ghMouseModule,L"UnMouseHook"); } if(NULL != InMouseHook) { InMouseHook(); }