gpt4 book ai didi

c - WH_GETMESSAGE 全局钩子(Hook)不工作

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:42 38 4
gpt4 key购买 nike

我正在尝试在所有线程上设置一个全局 GetMessage Hook 。这是我的 DLL:

#include <windows.h>

__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MessageBeep(0);

return CallNextHookEx(NULL, nCode, wParam, lParam);
}

如您所见,并不多。我只希望它在被调用时调用 MessageBeep。

#include <windows.h>

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 加载库,获取过程并设置 Hook 。但是,永远不会调用 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

关于c - WH_GETMESSAGE 全局钩子(Hook)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3209475/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com