最近学习SetWindowsHookEx 在DLL中全局HOOK键盘鼠标已经成功,现在想完成一个功能
就是在Windows关机或重启时先得到广播的WM_QUERYENDSESSION 消息,处理好自己的
工作后再让机子正常关机或重启。
但我把键盘HOOK的功能改成 WH_GETMESSAGE 但去HOOK不到WM_QUERYENDSESSION
源码如下DLL里的,请高手帮助指点下,谢谢了!!
#include "windows.h"
HHOOK g_hMsgProc;
HWND g_hWnd;
LRESULT CALLBACK GetMsgProc(
int code, // hook code
WPARAM wParam, // removal option
LPARAM lParam // message
)
{
MSG *lpMsg;
lpMsg = (MSG*)lParam;
if(lpMsg->message==WM_QUERYENDSESSION)
{
MessageBox(NULL,"Exit!","!!!!!!!!",0);
SendMessage(g_hWnd,WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hMsgProc);
return CallNextHookEx(g_hMsgProc,code,wParam,lParam);
}
return CallNextHookEx(g_hMsgProc,code,wParam,lParam);
}
/*LRESULT CALLBACK KeyboardProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
if(VK_F2==wParam) //只有按F2可以退出
{
SendMessage(g_hWnd,WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hMouse);
UnhookWindowsHookEx(g_hKeyboard);
}
return 1;//危险!!
}
LRESULT CALLBACK MouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
return 1;
}*/
void SetHook(HWND hWnd)
{
g_hWnd=hWnd;
//g_hMouse=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("Hook"),0);
//g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("Hook"),0);
g_hMsgProc=SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,GetModuleHandle("Hook"),0);
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课