C++ set power plan (energy saving, balanced, high performance), get battery information

This article introduces: How to use C++ to get battery information, get power plan and set power plan.

  • There are 4 power plans in Windows system

  •  program definition enumeration
    enum EmBatteryStrategy
    {
        IV_BATTERY_UNKNOWN          = 0,
        IV_BATTERY_POWER_SAVER      = 1,    // 节能
        IV_BATTERY_BALANCED         = 2,    // 平衡
        IV_BATTERY_HIGH_PERFORMANCE = 3,    // 高性能
        IV_BATTERY_SUPERIOR         = 4,    // 卓越性能
    };
  • Not much nonsense, let's go directly to the code
  • First define the header file, in which we define the state of the battery, the state of the power plan, and 3 interfaces for obtaining battery information:
   enum EmDevmgrBatteryType
    {
        IV_DEVMGR_UNKNOWN = 0,
        IV_DEVMGR_NO_BATTERY = 1,           // 没有电池
        IV_DEVMGR_BATTERY_CHARGING = 2,     // 正在充电
        IV_DEVMGR_BATTERY_USING = 3,
    };

    enum EmBatteryStrategy
    {
        IV_BATTERY_UNKNOWN          = 0,
        IV_BATTERY_POWER_SAVER      = 1,    // 节能
        IV_BATTERY_BALANCED         = 2,    // 平衡
        IV_BATTERY_HIGH_PERFORMANCE = 3,    // 高性能
        IV_BATTERY_SUPERIOR         = 4,    // 卓越性能
    };

    class Battery 
    {
    public:
        // 获取电源信息(电源状态,剩余电源,可用时长)
        static BOOL GetBatteryData(EmDevmgrBatteryType& nBatteryState, int& nBatteryPct, unsigned __int64& ullAvailTime);
        // 获取当前电源模式(高性能模式,平衡模式,节能模式)
        static EmBatteryStrategy GetBatteryStrategy();
        // 设置电源模式
        static BOOL SetBatteryStrategy(EmBatteryStrategy nStrategy);
    };
  • The following is the implementation part. The 4 GUIDs defined above are the GUID values ​​of each power plan, and this value is fixed.
    namespace
    {
        const GUID PowerSchemeSaver = { 0xA1841308, 0x3541, 0x4FAB, { 188, 129, 247, 21, 86, 242, 11, 74 } };       // 节能
        const GUID PowerSchemeBalance = { 0x381B4222, 0xF694, 0x41F0, { 150, 133, 255, 91, 178, 96, 223, 46 } };    // 平衡
        const GUID PowerSchemeHigh = { 0x8C5E7FDA, 0xE8BF, 0x4A96, { 154, 133, 166, 226, 58, 140, 99, 92 } };       // 高性能
        const GUID PowerSchemeSuperior = { 0x91D47585, 0x1A6B, 0x402B, { 128, 81, 103, 214, 59, 243, 194, 254 } };  // 卓越性能
    }

    BOOL Battery::GetBatteryData(EmDevmgrBatteryType& nBatteryState, int& nBatteryPct, unsigned __int64& ullAvailTime)
    {
        SYSTEM_POWER_STATUS sps;
        ::GetSystemPowerStatus(&sps);

        if (sps.BatteryFlag == BATTERY_FLAG_NO_BATTERY)
        {
            nBatteryState = IV_DEVMGR_NO_BATTERY;
        }
        else if (sps.ACLineStatus == AC_LINE_ONLINE)
        {
            nBatteryState = IV_DEVMGR_BATTERY_CHARGING;
        }
        else
        {
            nBatteryState = IV_DEVMGR_BATTERY_USING;
        }

        nBatteryPct = static_cast<int>(sps.BatteryLifePercent);
        ullAvailTime = static_cast<unsigned __int64>(sps.BatteryLifeTime);
        return TRUE;
    }

    EmBatteryStrategy Battery::GetBatteryStrategy()
    {
        EmBatteryStrategy nStrategy = IV_BATTERY_UNKNOWN;

        GUID* CurrentScheme;
        if (ERROR_SUCCESS != PowerGetActiveScheme(NULL, &CurrentScheme))
        {
            return nStrategy;
        }

        if (*CurrentScheme == PowerSchemeSuperior)
        {
            nStrategy = EmBatteryStrategy::IV_BATTERY_SUPERIOR;
        }
        else if (*CurrentScheme == PowerSchemeHigh)
        {
            nStrategy = EmBatteryStrategy::IV_BATTERY_HIGH_PERFORMANCE;
        }
        else if (*CurrentScheme == PowerSchemeBalance)
        {
            nStrategy = EmBatteryStrategy::IV_BATTERY_BALANCED;
        }
        else if (*CurrentScheme == PowerSchemeSaver)
        {
            nStrategy = EmBatteryStrategy::IV_BATTERY_POWER_SAVER;
        }

        LocalFree(CurrentScheme);
        return nStrategy;
    }

    BOOL Battery::SetBatteryStrategy(EmBatteryStrategy nStrategy)
    {
        if (nStrategy == EmBatteryStrategy::IV_BATTERY_HIGH_PERFORMANCE)
        {
            return (ERROR_SUCCESS == PowerSetActiveScheme(NULL, &PowerSchemeHigh));
        }
        if (nStrategy == EmBatteryStrategy::IV_BATTERY_BALANCED)
        {
            return (ERROR_SUCCESS == PowerSetActiveScheme(NULL, &PowerSchemeBalance));
        }
        if (nStrategy == EmBatteryStrategy::IV_BATTERY_POWER_SAVER)
        {
            return (ERROR_SUCCESS == PowerSetActiveScheme(NULL, &PowerSchemeSaver));
        }

        return FALSE;
    }

 How to get other battery information

SYSTEM_POWER_STATUS sps;
::GetSystemPowerStatus(&sps)

In fact, we can also obtain more detailed battery information through the GetSystemPowerStatus interface. The structure obtained by the GetSystemPowerStatus method is defined as follows:

typedef struct _SYSTEM_POWER_STATUS {
  BYTE  ACLineStatus;
  BYTE  BatteryFlag;
  BYTE  BatteryLifePercent;
  BYTE  SystemStatusFlag;
  DWORD BatteryLifeTime;
  DWORD BatteryFullLifeTime;
} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;

We can look at the value of each variable:

ACLineStatuspower status

value significance

0

offline

1

on-line

255

unknown status

BatteryFlag  battery metering status

value significance

1

High - The battery capacity exceeds 66%

2

Low - Battery capacity is less than 33%.

4

Major — less than 5% battery capacity

8

Charge

128

No system battery

255

Unknown status - Unable to read battery flag information

BatteryLifePercent  The percentage of remaining battery gauge. This member can be a value in the range 0 to 100, or 255 if the status is unknown.

SystemStatusFlag Status of power saving mode

value significance

0

Power saving mode is off.

1

Power saving mode is on. Save energy where possible.

BatteryLifeTime  The remaining battery life in seconds, or –1 if the remaining seconds are unknown or the device is connected to AC power

BatteryFullLifeTime The number of seconds of battery life in a fully charged state, or –1 if the full battery lifetime is unknown, or if the device is connected to AC power

Guess you like

Origin blog.csdn.net/dm569263708/article/details/129241855
Recommended