我正在尝试捕捉屏幕保护程序事件。这是我的代码:
#include "stdafx.h"
#include <iostream>
#include "stdio.h"
#include <Windows.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()
{
if (!(_hook = SetWindowsHookEx(WH_GETMESSAGE, HookCallback,NULL , 0)))
{
std::cout<<"Failed to install hook!\n";
}
}
void ReleaseHook()
{
UnhookWindowsHookEx(_hook);
}
int _tmain(int argc, _TCHAR* argv[])
{
SetHook();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
}
ReleaseHook();
return 0;
}
功能SetWindowsHookEx(WH_GETMESSAGE, HookCallback,NULL , 0)
失败。它返回代码:
错误 1428:无法在没有模块句柄的情况下设置非本地挂钩
。我究竟做错了什么?谢谢!