关于Hook拦截不到WM_WM_KILLFOCUS消息。
LRESULT CALLBACK GetMsgProc( int code, // hook code
WPARAM wParam, // current-process flag
LPARAM lParam // message data
)
SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("HookDll"),NULL);
现在钩子可以设置成功,也进入了GetMsgProc函数。,但就是拦截不到指定的消息,
大概就是这些,具体代码如下:
dll def文件:
LIBRARY HookDll
EXPORTS
SetHook
#include <windows.h>
#pragma data_seg("MyHookEx")
HHOOK hhgm=NULL;
#pragma data_seg()
#pragma comment(linker,"/section:MyHookEx,RWS")
LRESULT CALLBACK GetMsgProc( int code, // hook code
WPARAM wParam, // current-process flag
LPARAM lParam // message data
)
{
if(code==HC_ACTION)
{
MSG *msg = (MSG*)lParam;
if(code == HCBT_ACTIVATE)
{
if(msg->message==WM_KILLFOCUS)
{
MessageBox(0,0,0,0);
}
}
return 0;
}else if(code<0)
{
return CallNextHookEx(hhgm,code,wParam,lParam);
}
return 0;
}
void SetHook()
{
hhgm = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("HookDll"),NULL);
if(hhgm)
{
MessageBox(0,"Hook is sucess!",0,0);
}else
{
MessageBox(0,"Hook is NULL!",0,0);
}
}
#include <Windows.h>
_declspec(dllimport) void SetHook();
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HWND hb;
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrveInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbClsExtra=0;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.cbWndExtra=0;
wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wcex.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
wcex.hInstance=hInstance;
wcex.lpfnWndProc=WndProc;
wcex.lpszClassName="ClassName";
wcex.lpszMenuName=NULL;
wcex.style=CS_HREDRAW|CS_VREDRAW;
RegisterClassEx(&wcex);
HWND hwnd=CreateWindow(wcex.lpszClassName,"Title",WS_OVERLAPPEDWINDOW+WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,350,250,NULL,NULL,hInstance,NULL);
hb=CreateWindow("Button","Title",WS_CHILDWINDOW+WS_VISIBLE,0,0,100,50,hwnd,NULL,hInstance,NULL);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
break;
case WM_COMMAND:
if((HWND)lParam==hb)
{
SetHook();
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
break;
}
return 0;
}