本文介绍了WH_GETMESSAGE全局钩子不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个全局钩子的GetMessage上的所有线程。这是我的DLL:

I am trying to set a global GetMessage hook on all threads. This is my DLL:

#include <windows.h>

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

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

正如你所看到的,它的并不多。我只是想它来调用的MessageBeep时,它被称为

As you can see, it's not much. I just want it to call MessageBeep whenever it's called.

#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函数加载库,获取程序,并设置了钩。然而,的MessageBeep永远不会被调用。是有什么我做错了?

My WinMain loads the library, gets the procedure and sets the hook. However, MessageBeep is never being called. Is there something I'm doing wrong here?

此外,另一件事情一直困扰着我。在此呼吁:

Also, one other thing has been bothering me. In this call:

if(!(LPGetMsgProc pfnProc = (LPGetMsgProc)GetProcAddress(hDll, "GetMsgProc@12")))

我被迫使用GetMsgProc @ 12,因为我无法得到它的权利任何其他方式。有人能告诉我,我应该如何使用DEF文件或别的东西,所以我可以只为GetMsgProc?虽然MSDN指出,由于我在声明中有__declspec(dllexport)的,我不会需要它...

I was forced to use "GetMsgProc@12" because I couldn't get it right any other way. Can somebody please tell me how I'm supposed to use a .def file or something else so I can just have it as "GetMsgProc"? Though MSDN stated that since I have __declspec(dllexport) in my declaration I wouldn't need it...

我的IDE是code ::使用MinGW块。先谢谢了。

My IDE is Code::Blocks with MinGW. Thanks in advance.

推荐答案

第三个参数...

HHOOK hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, pfnProc, hInstance, 0);

...是传递到WinMain函数的句柄。但它需要引用到回调函数所在的DLL - 你的情况,这会是 hDLL

这篇关于WH_GETMESSAGE全局钩子不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:32