我正在尝试捕捉屏幕保护程序事件。这是我的代码:
#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include <Windows.h>
#include <strsafe.h>
HHOOK _hook;
LRESULT CALLBACK HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
LPMSG msg = (LPMSG)lParam;
if(msg->message == WM_SYSCOMMAND)
{
if (msg->wParam == SC_SCREENSAVE)
{
std::cout<<"SC_SCREENSAVE\n";
}
}
}
return CallNextHookEx(_hook, nCode, wParam, lParam);
}
void SetHook()
{
HINSTANCE hinstDLL = LoadLibrary(L"user32.dll");
if (!(_hook = SetWindowsHookEx(WH_GETMESSAGE,HookCallback,hinstDLL,0)))
{
std::cout<<"Failed to install hook!\n";
}
}
void ReleaseHook()
{
UnhookWindowsHookEx(_hook);
}
int _tmain(int argc, _TCHAR* argv[])
{
SetHook();
// Don't mind this, it is a meaningless loop to keep a console application running.
// I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
}
ReleaseHook();
return 0;
}
注册成功。但我没有赶上这个事件。甚至没有进入回调函数。我究竟做错了什么?