Windows电源管理
+ -

使用RegisterPowerSettingNotification判断笔记本的供电类型或供电改变消息回调

2023-07-31 120 0
Windows电源管理相关编程

判断电源的供电类型,或当电原供电类型发生变化时,通知应用程序。

#include <Windows.h>
#include <stdio.h>
#include <winuser.h>
#include <poclass.h>

// 电源设置更改回调函数
LRESULT CALLBACK PowerSettingCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_POWERBROADCAST && wParam == PBT_POWERSETTINGCHANGE)
    {
        POWERBROADCAST_SETTING* powerSetting = (POWERBROADCAST_SETTING*)lParam;
        if (powerSetting->PowerSetting == GUID_ACDC_POWER_SOURCE) 
        {
            if (powerSetting->Data[0] == PoAc)
            {
                printf("AC power source changed! \n");
            }
            else if (powerSetting->Data[0] == PoDc)
            {
                printf("DC power source changed! \n");
            }  
            else if (powerSetting->Data[0] == PoHot)
            {
                printf("PoHot power source changed! \n");
            }
            else
            {
                printf("unknow power source changed! \n");
            }

        }
    }

    return DefWindowProc(hWnd, message, wParam, lParam);
}

int main()
{
    // 注册窗口类
    WNDCLASS wndClass = { 0 };
    wndClass.lpfnWndProc = PowerSettingCallback;
    wndClass.hInstance = GetModuleHandle(NULL);
    wndClass.lpszClassName = L"PowerSettingWindowClass";
    if (!RegisterClass(&wndClass))
    {
        printf("Failed to register window class!\n");
        return 1;
    }

    // 创建窗口
    HWND hWnd = CreateWindow(wndClass.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
    if (!hWnd)
    {
        printf("Failed to create window!\n");
        return 1;
    }

    // 注册电源设置更改通知
    HPOWERNOTIFY powerNotify = RegisterPowerSettingNotification(hWnd, &GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE);
    if (!powerNotify) 
    {
        printf("Failed to register power setting notification!\n");
        return 1;
    }

    printf("Power setting notification registered!\n");

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // 取消注册电源设置更改通知
    if (!UnregisterPowerSettingNotification(powerNotify)) 
    {
        printf("Failed to unregister power setting notification!\n");
        return 1;
    }

    printf("Power setting notification unregistered!\n");

    return 0;
}

本人笔记本电脑默认外接电源,通过拨插电源可以收到消息回调。

Power setting notification registered!
AC power source changed!
DC power source changed!
AC power source changed!

ps:微软的电源管理另一篇文章说可以通过PowerSettingRegisterNotification来实现,不过本人的示例代码会出现崩溃问题:


//#define GUID_STRING_LENGTH 39

void CALLBACK PowerSettingCallback(
    PVOID contex
   // LPCWSTR powerSettingGuid
    //PVOID settingData,
    //DWORD settingDataLength
)
{
    //printf("Power setting changed: %ls\n", powerSettingGuid);
}

int main()
{
    HPOWERNOTIFY powerNotify;

    // 注册电源设置通知
    if (!PowerSettingRegisterNotification(
        &GUID_POWERSCHEME_PERSONALITY,
        DEVICE_NOTIFY_CALLBACK,
        PowerSettingCallback,
        &powerNotify
    ))
    {
        printf("Failed to register power setting notification.\n");
        return 1;
    }

    printf("Power setting notification registered. Press any key to exit.\n");
    getchar();

    // 取消注册电源设置通知
    if (!PowerSettingUnregisterNotification(powerNotify))
    {
        printf("Failed to unregister power setting notification.\n");
        return 1;
    }

    printf("Power setting notification unregistered.\n");

    return 0;
}

0 篇笔记 写笔记

使用RegisterPowerSettingNotification判断笔记本的供电类型或供电改变消息回调
判断电源的供电类型,或当电原供电类型发生变化时,通知应用程序。#include #include #include #include // 电源设置更改回调......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!