业务代码

监听windows是否处于休眠唤醒状态 #

#include <windows.h>
#include <iostream>
#include <powrprof.h>

#pragma comment(lib, "Powrprof.lib")

using namespace std;

ULONG CALLBACK DeviceCallback(PVOID Context, ULONG Type, PVOID Setting)
{
    if (Type == PBT_APMSUSPEND)
    {
        cout << "close" << endl;
    }
    if (Type == PBT_APMRESUMESUSPEND)
    {
        cout << "open" << endl;
    }
    return ERROR_SUCCESS;
}

int main()
{
    HPOWERNOTIFY g_power_notify_handle = NULL;
    DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS params;
    params.Callback = DeviceCallback;
    params.Context = 0;
    PowerRegisterSuspendResumeNotification(DEVICE_NOTIFY_CALLBACK, &params, &g_power_notify_handle);
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    PowerUnregisterSuspendResumeNotification(g_power_notify_handle);
    return 0;
}