大家好,又见面了,我是你们的朋友全栈君。
全局消息钩子的钩子函数一定要再dll中,然后启动安装钩子不能在dll中,要是我想在程序开始时安装钩子怎么办。很简单利用两个钩子就行了,只要安装钩子和钩子函数不在同一个dll就行了。
下面请看案例(编译
编译环境: vs2010 多字节 已在window7 32位测试通过
)
//mouseHook.cpp 钩子函数所在dll #include <windows.h> // a sample exported function #define DEF_PROCESS_NAME "winmine.exe" #define WIDTH 32 #define HEIGHT 24 HINSTANCE g_hInstance=NULL; HHOOK g_hHook=NULL; //窗口坐标转化为雷区坐标 VOID windowPosChangeToMinePos(POINT * pt){ int y = (pt->x-10)/16+1; int x = (pt->y-55)/16+1; pt->x = x; pt->y = y; } BOOL first = TRUE; BYTE rects[HEIGHT][WIDTH]; HWND window = NULL; // a sample exported function //LRESULT是一个数据类型,指的是从窗口程序或者回调函数返回的32位值 //lParam wParam 是宏定义,一般在消息函数中带着两个类型的参数,通常用来存储窗口消息的参数。wParam用来存储小段消息,如标志。lParam 通常用于存储消息所需的对象。 LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam) { char szPath[MAX_PATH]={0,}; char *p=NULL; HWND hWnd; int x; int y; char xs[MAX_PATH] = {0,}; char ys[MAX_PATH] = {0,}; int i,j; byte * temp; int tempInt; char byteTemp[2] = {0,}; if (nCode>=0) { //0=key press,1=key release if (wParam == WM_MOUSEMOVE )//释放键盘按键时 { GetModuleFileNameA(NULL,szPath,MAX_PATH); p=strrchr(szPath,'\\');//查找字符在指定字符串中从左面开始的最后一次出现的位置 //比较当前进程名称,若为notepad.exe,则消息不会传递给应用程序(或下一个“钩子”) if (!_stricmp(p+1,DEF_PROCESS_NAME)){ //SetWindowTextA(hWnd,"saolei"); LPMOUSEHOOKSTRUCT lpMouse=(MOUSEHOOKSTRUCT FAR*)lParam; if (first==TRUE) { window = lpMouse->hwnd; first=FALSE; } ScreenToClient(lpMouse->hwnd,&lpMouse->pt); windowPosChangeToMinePos(&lpMouse->pt); x = lpMouse->pt.x; y =lpMouse->pt.y; if(x>0&&y>0&&window==lpMouse->hwnd){ temp= (byte *)0x01005361; for (i=0;i<HEIGHT;i++) { for (j=0;j<WIDTH;j++) { rects[i][j] = *(temp+i*WIDTH+j); } } tempInt = rects[x-1][y-1]; //itoa(tempInt,xs,16); /* itoa(x,xs,16); itoa(y,ys,10); strcat(xs,","); strcat(xs,ys);*/ /* strcat(xs,","); itoa(x,ys,10); strcat(xs,ys); strcat(xs,","); itoa(y,ys,10); strcat(xs,ys);*/ /* itoa((int)lpMouse->hwnd,xs,16); SetWindowTextA(lpMouse->hwnd,xs);*/ if (tempInt==0x8f) { SetWindowTextA(lpMouse->hwnd,"扫雷"); } else SetWindowTextA(lpMouse->hwnd,"扫蕾"); } } } } //若非notepad.exe,则调用CallNextHookEx()函数,将消息传递给应用程序(或下一个“钩子”)。 return CallNextHookEx(g_hHook,nCode,wParam,lParam); } LPDWORD lpdwProcessId; BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: g_hInstance = hinstDLL; break; case DLL_PROCESS_DETACH: break; case DLL_THREAD_ATTACH: // attach to thread break; case DLL_THREAD_DETACH: // detach from thread break; } return TRUE; // succesful } #ifdef __cplusplus extern "C" { #endif __declspec(dllexport) void HookStart() { //钩子类型、回调函数地址、实例句柄、线程ID g_hHook=SetWindowsHookEx(WH_MOUSE,MouseProc,g_hInstance,0); } __declspec(dllexport) void HookStop() { if(g_hHook) { UnhookWindowsHookEx(g_hHook); g_hHook=NULL; } } #ifdef __cplusplus } #endif
//middleObject.cpp 生成dll 在dll被加载启动安装钩子 #include <windows.h> #include <stdio.h> #include <conio.h> #define DEF_DLL_NAME "mouseHook.dll" #define DEF_HOOKSTART "HookStart" #define DEF_HOOKSTOP "HookStop" typedef void (*PFN_HOOKSTART)(); typedef void (*PFN_HOOKSTOP)(); HMODULE hDll=NULL; PFN_HOOKSTART HookStart=NULL; PFN_HOOKSTOP HookStop=NULL; BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: //加载KeyHook.dll hDll=LoadLibraryA(DEF_DLL_NAME); //获取导出函数地址 HookStart=(PFN_HOOKSTART)GetProcAddress(hDll,DEF_HOOKSTART); //开始钩取 HookStart(); break; case DLL_PROCESS_DETACH: HookStop=(PFN_HOOKSTART)GetProcAddress(hDll,DEF_HOOKSTOP); HookStop(); // detach from process break; case DLL_THREAD_ATTACH: // attach to thread break; case DLL_THREAD_DETACH: // detach from thread break; } return TRUE; // succesful } #ifdef __cplusplus extern "C" { #endif __declspec(dllexport) void dummy() { } #ifdef __cplusplus } #endif
只要把middleObject.dll加载就可以安装消息钩子了
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155408.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...