http://www.vc180.com/kaifa/anqn/2010/0213/237.html
首先我们要建立一个用来HOOK的DLL。这个DLL的建立和普通的DLL建立没有什么具体的区别,不过我们这里提供的方法有写不同。这里使用隐式导入DLL的方法。代码如下:
头文件
#pragma once
#ifndef MSNHOOK_API
#define MSNHOOK_API __declspec(dllimport)
#endif
MSNHOOK_API BOOL WINAPI SetMsnHook(DWORD dwThreadId);//安装MSN钩子函数
MSNHOOK_API void WINAPI GetText(int &x,int &y,char ** ptext);//安装MSN钩子函数
MSNHOOK_API HWND WINAPI GetMyHwnd();//安装MSN钩子函数
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用来接受信息。
程序如下:
//包含DLL函数导出的头文件
#include "MSNHook.h"
//隐式导入
#pragma comment(lib,"MSNHook.lib")
//声明导入函数
__declspec(dllimport) BOOL WINAPI SetMsnHook(DWORD dwThreadId);
__declspec(dllimport) void WINAPI GetText(int &x,int &y,char ** ptext);
__declspec(dllimport) HWND WINAPI GetMyHwnd();//安装MSN钩子函数
void CTestMSNHookDlg::OnBnClickedOk()
{
//通过SPY++可以看到MSN聊天对话框窗口类是IMWindowClass,通过这个得到该窗口句柄
CWnd *pMsnWin = FindWindow(TEXT("IMWindowClass"),NULL);
if(pMsnWin == NULL) return ;
//通过窗口句柄得到对应的线程的ID
SetMsnHook(GetWindowThreadProcessId(pMsnWin->GetSafeHwnd(),NULL));
MSG msg;
GetMessage(&msg,NULL,0,0);
SetTimer(101,100,NULL);
}
void CTestMSNHookDlg::OnTimer(UINT_PTR nIDEvent)
{
//刷新消息
char * pText = NULL;
int x = 0,y = 0;
GetText(x,y,&pText);
if(x ==0 && y ==0) return ;
m_Edit.Format("%d:%d:%s",x,y,pText);
//m_Edit = pText;
UpdateData(FALSE);
HWND hWnd = GetMyHwnd();
CWnd * pWnd = CWnd::FromHandle(hWnd);
pWnd->GetWindowText(m_Edit);
CDialog::OnTimer(nIDEvent);
}
void CTestMSNHookDlg::OnBnClickedButton1()
{
//关闭
KillTimer(101);
SetMsnHook(0);
OnCancel();
}
好了,基本上就这些了。这里有个问题,我本想得到MSN用户聊天时输入的聊天信息,这里通过WM_GETTEXT消息的不到,如果有知道的朋友告诉一声。