WH_GETMESSAGE全局钩子无法正常工作
我试图在所有线程上设置一个全局GetMessage挂钩。 这是我的DLL:
#include __declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) { MessageBeep(0); return CallNextHookEx(NULL, nCode, wParam, lParam); }
如你所见,它并不多。 我只是希望它在调用时调用MessageBeep。
#include typedef LRESULT (CALLBACK *LPGetMsgProc)(int nCode, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow) { if(!(HMODULE hDll = LoadLibrary("library.dll"))) return 1; if(!(LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "GetMsgProc@12"))) return 2; HHOOK hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, pfnProc, hInstance, 0); MSG msg; while(GetMessage(&msg, NULL, 0, 0) > 0) {} UnhookWindowsHookEx(hMsgHook); return 0; }
我的WinMain加载库,获取过程并设置钩子。 但是,永远不会调用MessageBeep。 我在这里做错了吗?
另外,还有一件事困扰着我。 在这个电话中:
if(!(LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "GetMsgProc@12")))
我被迫使用“GetMsgProc @ 12”因为我无法以任何其他方式正确使用它。 有人可以告诉我,我应该如何使用.def文件或其他东西,所以我可以把它作为“GetMsgProc”? 虽然MSDN声明由于我在声明中有__declspec(dllexport),我不需要它…
我的IDE是Code :: Blocks with MinGW。 提前致谢。
第三个参数……
HHOOK hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, pfnProc, hInstance, 0);
…是传递给WinMain函数的句柄。 但是它需要引用回调函数所在的DLL – 在你的情况下,那就是hDLL
。