在挂钩过滤器功能中过滤Windows消息

在挂钩过滤器功能中过滤Windows消息

本文介绍了在挂钩过滤器功能中过滤Windows消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Windows钩子检索另一个应用程序的消息.我已经用SetWindowsHookEx设置了WH_GETMESSAGE钩子.这是通过DLL完成的.在我的GetMsgProc函数中(目标应用程序收到消息时应调用该函数),我想根据消息的类型采取措施.但是我在使用if语句时遇到了麻烦.

I am trying to retrieve messages for another application with a Windows hook. I have setup a WH_GETMESSAGE hook with SetWindowsHookEx. This is done via a DLL. In my GetMsgProc function (that should be called whenever the target application receives a message) I want to take action based on the type of message. However I am having trouble with this if statement.

LRESULT CALLBACK MessageHookProcedure(int code, WPARAM wParam, LPARAM lParam){
    if(((MSG*)lParam)->message == WM_COMMAND){
        MessageBox(NULL,L"The hook procedure was called",L"Test Window",MB_OK);
    }

    return CallNextHookEx(g_MessageHook,code,wParam,lParam);
}

由于某些原因,从未创建过MessageBox.我知道应用程序正在从Spy ++接收WM_COMMAND消息.如果我删除IF语句,则会一遍又一遍地创建MessageBox,因为它会收到各种消息.

For some reason the MessageBox is never created. I know the application is receiving WM_COMMAND messages from Spy++. If I take out the IF statement the MessageBox is created over and over as it receives a variety of messages.

推荐答案

您确定要分别钩住正确的窗口或正确的消息吗?在某些情况下,将生成WM_SYSCOMMANDWM_MENUCOMMAND而不是WM_COMMAND.

Are you sure that you're hooking the correct window or the correct message, respectively? Under some circumstances WM_SYSCOMMAND or WM_MENUCOMMAND is generated instead of WM_COMMAND.

您的代码看起来不错,您是否也尝试过将传入的消息转储到控制台中?

Your code looks fine, have you also tried dumping the incoming messages into console?

这篇关于在挂钩过滤器功能中过滤Windows消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:40