钩子安装失败,SetWindowsHookEx返回值0
我基本是按照vc++深入详解写的,在实验室机器上没错误,可回家就错
哪位高人能指点下,我实在是搞不明白到底哪里错了?
截取了部分代码如下:
HMODULE h=GetModuleHandle("dll_pocket");
PROC p=GetProcAddress(h,"GetMsgProc");
HHOOK g_hhook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,h,dwThreadId);
DWORD error=GetLastError();
调试时:
h值为0x10000000(GetProcAddress调用成功是不是说明h值正确)
p值为0x10001005(说明GetMsgProc也正确)
dwThreadId值为2948(我在任务管理器核对过,是正确的)
g_hhook值为0x00000000(所有参数都是正确的,为什么会返回0)
error值为87
整个dll如下所示:(dll工程名为dll_pocket)
#define DLLPOCKET_EXPORT extern "C" _declspec(dllexport)
#include "dllpocket.h"
#pragma data_seg("Shared")
HHOOK g_hhook = NULL;
#pragma data_seg()
#pragma comment(linker, "/Section:Shared,rws")
LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam) {
MSG *msg=(MSG*)wParam;
if(msg.message==WM_LBUTTONDOWN)
::PostMessage(FindWindow(NULL,"pocket"),WM_USER+1,msg->wParam,msg->lParam);
return(CallNextHookEx(g_hhook, code, wParam, lParam));
}
DLLPOCKET_EXPORT BOOL WINAPI HookApp(BOOL fInstall, DWORD dwThreadId) {
BOOL fOk;
if (fInstall) {
if(g_hhook!=NULL)
return FALSE;
// Install the Windows' hook
HMODULE h=GetModuleHandle("dll_pocket");
PROC p=GetProcAddress(h,"GetMsgProc");
g_hhook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,h,dwThreadId);
DWORD error=GetLastError();
fOk = (g_hhook != NULL);
}
else
{
if(g_hhook==NULL)
return FALSE;
fOk = UnhookWindowsHookEx(g_hhook);
g_hhook = NULL;
}
return(fOk);
}