关于Getmessage hook的一个问题
目前在做一个关于hook的demo程序,目的很简单,要探测用户点击Dialog上特定的button的动作。首先装个全局的CBT hook,找到需要hook的窗体句柄hwnd,然后装个线程的Getmessage hook。
case HCBT_ACTIVATE :
if(bTargetWindow(hwnd))//判断当前激活的窗体是否是需要监听的窗体
{
DWORD threadID=GetWindowThreadProcessId (hWnd,NULL);
if(g_hGetMessageHook==NULL) g_hGetMessageHook=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)GetMsgProc,g_hdll,threadID);
}
//===============================================================
以下内容来自msdn
When the user clicks a button, its state changes, and the button sends notification messages to its parent window. For example, a push button control sends the BN_CLICKED notification message whenever the user chooses the button.
The BN_CLICKED notification code is sent when the user clicks a button.
The parent window of the button receives the BN_CLICKED notification code through the WM_COMMAND message
Button Default Message Processing
Message Default action
BM_CLICK :Sends the button a WM_LBUTTONDOWN and a WM_LBUTTONUP message, and sends the parent window a BN_CLICKED notification message.
基本意思是:
当用户点击了button时,button发送BN_CLICKED消息来通知父窗体,而父窗体通过WM_COMMAND 消息来接收来接收BN_CLICKED该消息。
所以我是在GetMsgProc回调函数里面判断WM_COMMAND 消息。当收到的消息是WM_COMMAND 时,判断lParam 值是否为button的句柄。
目前的问题是用在回调函数里面一直截获不到WM_COMMAND消息的出现,但我用spy++来观察父窗体的消息,点击按钮后,可以看到WM_COMMAND消息出现。
我有以下疑点:
是否是装载的hook类型不对,通过getmessage hook 根本就侦测不到WM_COMMAND消息?是否
WM_COMMAND 消息并不是父窗体通过 GetMessage or PeekMessage 来取得的?
我可以在GetMsgProc回调函数里截获到WM_LBUTTONDOWN 和WM_LBUTTONup消息,并且该消息是button发送给父窗体的。为什么会截获到button的消息?
由于对hook研究不深,望各位指点迷津