DLL 的CPP文件
#include "stdafx.h" #include "MSNHook.h" #include <stdio.h>
// 下面几句的含义是告诉编译器将各变量放入它自己的数据共享节中
#pragma data_seg("Shared") HHOOK g_hhook = NULL; DWORD g_dwThreadIdMsn = 0; POINT MouseLoc={0,0}; char text[256]={0}; HWND g_Hwnd = NULL; #pragma data_seg()
//告诉编译器设置共享节的访问方式为:读,写,共享
#pragma comment(linker,"/section:Shared,rws")
HINSTANCE g_hinstDll = NULL;
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: g_hinstDll = (HINSTANCE)hModule; break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
LRESULT WINAPI GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam);
BOOL WINAPI SetMsnHook(DWORD dwThreadId) { OutputDebugString("SetMsnHook"); BOOL fOK = FALSE; if(dwThreadId != 0) { OutputDebugString("SetMsnHook dwThreadId != 0"); g_dwThreadIdMsn = GetCurrentThreadId();
//安装WM_MOUSE钩子和处理函数GetMsgProc g_hhook = SetWindowsHookEx(WH_MOUSE,GetMsgProc,g_hinstDll,dwThreadId); fOK = (g_hhook != NULL); if(fOK) { fOK = PostThreadMessage(dwThreadId,WM_NULL,0,0); } else { fOK = UnhookWindowsHookEx(g_hhook); g_hhook = NULL; } } return(fOK); }
LRESULT WINAPI GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam) {
char temp[20]; sprintf(temp,"%d\n",nCode); OutputDebugString("temp"); if (nCode==HC_ACTION) { MOUSEHOOKSTRUCT *l=(MOUSEHOOKSTRUCT *)lParam; MouseLoc=l->pt; //送鼠标位置
//char text[256] = ""; HWND hWnd = WindowFromPoint(l->pt); if(hWnd) { //GetWindowText(hWnd,text,256); SendMessage(hWnd,WM_GETTEXT,256,(LPARAM)(LPCTSTR)text); // strcpy(text,"123455555"); SendMessage(hWnd,WM_SETTEXT,256,(LPARAM)(LPCTSTR)text); g_Hwnd = hWnd; } //SendMessage(WindowFromPoint(l->pt),WM_GETTEXT,256,(LPARAM)(LPCTSTR)psw); }
return(CallNextHookEx(g_hhook,nCode,wParam,lParam)); }
void WINAPI GetText(int &x,int &y,char ** ptext) { x = MouseLoc.x; y = MouseLoc.y; *ptext = text; }
HWND WINAPI GetMyHwnd() { return g_Hwnd; } 上面是处理钩子的DLL代码,下面我们要让这个DLL起作用还需要一个启动部分,通过这个启动部分我们才能让我们的钩子函数真正的注入到系统其他函数中。我们这里使用个对话框的程序,程序非常简单:一个按钮用来启动钩子,一个用来停止,一个TIMER用来刷新显示,还有一个EDITBOX用来接受信息。
程序如下:
|