-
-
[求助]我的消息钩子为什么拦截不了消息
-
发表于:
2018-4-18 15:07
3718
-
我在一个dll里面写了如下代码,再用另外一个进程LoadLibrary,但是却拦截不了键盘消息,下断点发现set钩子函数没问题,但是跳不到要执行的函数那里(在xp环境下做的)
插入代码
```#include <windows.h>
#include <stdio.h>
HHOOK g_hHook = NULL; //全局保存键盘钩子句柄,取消钩子的时候用
void OutputDbgInfo(char *format,int param)
{
char *str;
sprintf(str,format,param);
OutputDebugString(str);
}
LRESULT CALLBACK HookKeyboardMsg(int code,WPARAM wParam,LPARAM lParam)
{
LRESULT lResult = 1;
__try
{
if(wParam == VK_F1)
{
MessageBox(NULL,"加速已开启","tip",MB_OK);
return lResult;
}
lResult = CallNextHookEx(g_hHook,code,wParam,lParam);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
OutputDbgInfo("[-] HookKeyboardMsg Exception,code:%d\n",GetLastError());
return -1;
}
return lResult;
}
BOOL HookKeyboard(DWORD dwThread)
{
BOOL bSuccess = TRUE;
HHOOK hhook;
__try
{
hhook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)HookKeyboardMsg,NULL,dwThread);
if(hhook == NULL)
{
OutputDbgInfo("[-] SetWindowsHookEx error,code:%d\n",GetLastError());
bSuccess = FALSE;
}
g_hHook = hhook;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
OutputDbgInfo("[-] SetWindowsHookEx Exception,code:%d\n",GetLastError());
return FALSE;
}
return bSuccess;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
HookKeyboard(::GetCurrentThreadId());
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课