简体   繁体   中英

Catching the screen saver event

I'm trying to catch the screen saver event. This is my code:

#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;
}

The function SetWindowsHookEx(WH_GETMESSAGE, HookCallback,NULL , 0) fails. It returns the code:

error 1428: Cannot set nonlocal hook without a module handle

.What am I doing wrong? Thanks!

System-Wide hooks require the application to have elevated access. You need to run your code As Administrator.

Maybe you will be interested in this question/answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM