WH_GETMESSAGE HOOK 调用回调函数失败
刚学Hook,很多地方不懂……
我想用WH_GETMESSAGE HOOK 得到global的消息。
我在win32 dll project 中创建 dll如下:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
//============================================================================
__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
// add code
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
同时在dll project中 添加 exports.def文件:
EXPORTS
GetMsgProc
在win32 project中写如下代码:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef LRESULT(CALLBACK *LPGetMsgProc)(int nCode, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
// Load DLL and get address of GetMsgProc() function
HMODULE hDll = LoadLibrary("TestDLL.dll");
if (!hDll)
return -1;
LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "GetMsgProc");
if (!pfnProc)
{
FreeLibrary(hDll);
return -1;
}
// Setup global hook
HHOOK hHook = SetWindowsHookEx(WH_GETMESSAGE, pfnProc, hDll, 0);
if (hHook != NULL)
{
// Wait a bit
Sleep(10000);
// Cleanup hook
UnhookWindowsHookEx(hHook);
}
// Cleanup DLL
FreeLibrary(hDll);
return hHook != NULL;
}
hook 可以安装成功。
但是__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) 并不会被调用, 所以问题是在什么地方呢? 什么时候 CALLBACK 才会被调用?
实在没积分了,提前谢谢大家!