C++ WINAPI 充电

问题描述 投票:0回答:1

使用 WINAPI C++,当我的笔记本电脑电量达到 95% 时,我需要停止充电,我该怎么做?

尝试挖掘 winapi 文档但没有找到任何东西

c++ winapi
1个回答
0
投票

您是否尝试查看电源管理 API? 这是代码片段

#include <Windows.h>
GUID currentPowerScheme;
    PowerGetActiveScheme(NULL, &currentPowerScheme);
    SYSTEM_POWER_STATUS powerStatus;
    GetSystemPowerStatus(&powerStatus);
    ULONG batteryCapacity = powerStatus.BatteryLifePercent;
    DWORD batteryThreshold = 95;
if (batteryCapacity >= batteryThreshold) {
        GUID subGroup = GUID_ACDC_POWER_SOURCE;
        GUID powerSetting = GUID_BATTERY_DISCHARGE_THRESHOLD;
        DWORD batteryStopThreshold = batteryThreshold * 100;
        POWER_SETTING_VALUE value;
        value.ValueLength = sizeof(DWORD);
        value.Data = &batteryStopThreshold;

        PowerWriteACValueIndex(NULL, &currentPowerScheme, &subGroup, &powerSetting, batteryStopThreshold);
   }

© www.soinside.com 2019 - 2024. All rights reserved.