标签:
1 // MouseHook.h 2 3 #ifdef MOUSEHOOK_EXPORTS 4 #define MOUSEHOOK_API __declspec(dllexport) 5 #else 6 #define MOUSEHOOK_API __declspec(dllimport) 7 #endif 8 9 extern "C" MOUSEHOOK_API void CALLBACK InstallMouseHook(void); 10 extern "C" MOUSEHOOK_API void CALLBACK UnMouseHook(void); 11 12 extern "C" MOUSEHOOK_API LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
1 // MouseHook.cpp 2 // MouseHook.cpp : Defines the entry point for the DLL application. 3 // 4 5 #include "stdafx.h" 6 #include "MouseHook.h" 7 #include <Pwinuser.h> 8 9 // 告诉编译器将变量放入它自己的数据共享节中 10 #pragma data_seg("MouseHookData") 11 12 HINSTANCE ghInst = NULL; 13 HHOOK ghMouseHook = NULL; 14 15 #pragma data_seg() 16 17 // 告诉编译器设置共享节的访问方式为:读,写,共享 18 #pragma comment(linker, "/SECTION:MouseHookData,RWS") 19 20 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) 21 { 22 switch(ul_reason_for_call) 23 { 24 case DLL_PROCESS_ATTACH: 25 ghInst = (HINSTANCE)hModule; 26 break; 27 case DLL_THREAD_ATTACH: 28 case DLL_THREAD_DETACH: 29 case DLL_PROCESS_DETACH: 30 break; 31 } 32 return TRUE; 33 } 34 35 extern "C" MOUSEHOOK_API void InstallMouseHook(void) 36 { 37 RETAILMSG(1,(L"[Mouse HOOK]Install HOOK: 0x%x\r\n",ghInst)); 38 if(ghInst) 39 { 40 ghMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, ghInst, 0); 41 if(NULL == ghMouseHook) 42 { 43 RETAILMSG(1,(L"[Mouse HOOK]HOOK create failed: %d\r\n",GetLastError())); 44 } 45 } 46 } 47 48 extern "C" MOUSEHOOK_API void UnMouseHook(void) 49 { 50 RETAILMSG(1,(L"[Mouse HOOK]Uninstall HOOK:0x%x\r\n",ghMouseHook)); 51 if(ghMouseHook) 52 { 53 UnhookWindowsHookEx(ghMouseHook); 54 ghMouseHook = NULL; 55 } 56 57 ghInst = NULL; 58 } 59 60 /* 61 nCode 62 [输入参数] 说明 HOOK 过程如何处理此消息。如果 nCode 小于零, HOOK 过程必须将此消息直接传递给下一个 CallNextHookEx, 并从 CallNextHookEx 返回. 63 本参数可以取以下的值:HC_ACTION 64 wParam 和 lParam 参数中包含鼠标消息的信息。 65 wParam 66 [输入参数] 鼠标消息标识. 本参数取如下值之一: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_MOUSEHWHEEL, WM_RBUTTONDOWN, 或 WM_RBUTTONUP. 67 lParam 68 [输入参数] 指定 MSLLHOOKSTRUCT 结构体. 其中包含坐标等信息。 69 */ 70 // CE6 的鼠标 HOOK 的响应速度很慢, 原因不明! 71 extern "C" MOUSEHOOK_API LRESULT MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) 72 { 73 static POINT ptPre; 74 75 #if _DEBUG 76 RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,nCode is: %d\r\n",nCode)); 77 RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,wParam is: %d\r\n",wParam)); 78 #endif 79 PMSLLHOOKSTRUCT pMouseHookStruct = (PMSLLHOOKSTRUCT)lParam; 80 #if _DEBUG 81 RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,mouseData is: %d\r\n",pMouseHookStruct->mouseData)); 82 RETAILMSG(1,(L"[Mouse HOOK]Mouse Proc,dwExtraInfo is: %d\r\n",pMouseHookStruct->dwExtraInfo)); 83 #endif 84 85 if(HC_ACTION != nCode) 86 { 87 return CallNextHookEx(ghMouseHook, nCode, wParam, lParam); 88 } 89 90 if(ptPre.x != pMouseHookStruct->pt.x || ptPre.y != pMouseHookStruct->pt.y) 91 { 92 #if _DEBUG 93 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)); 94 #endif 95 ptPre = pMouseHookStruct->pt; 96 97 // 若注释掉此句, 则不再响应鼠标操作 98 return CallNextHookEx(ghMouseHook, nCode, wParam, lParam); // 继续传递消息 99 } 100 101 return TRUE; 102 } 103 104 // 调用示例 105 106 // mouse hook test 107 typedef void (*pInstallMouseHook)(void); 108 typedef void (*pUnMouseHook)(void); 109 HINSTANCE ghMouseModule = NULL; 110 pInstallMouseHook InMouseHook = NULL; 111 pUnMouseHook UnMouseHook = NULL; 112 113 114 ghMouseModule = LoadLibrary(L"\\AppStore\\SystemInfo\\MouseHook.dll"); 115 if(ghMouseModule) 116 { 117 InMouseHook = (pInstallMouseHook)GetProcAddress(ghMouseModule,L"InstallMouseHook"); 118 UnMouseHook = (pUnMouseHook)GetProcAddress(ghMouseModule,L"UnMouseHook"); 119 } 120 if(NULL != InMouseHook) 121 { 122 InMouseHook(); 123 }
标签:
原文地址:http://www.cnblogs.com/91program/p/5206454.html