C++ WINAPI 充电

huangapple go评论148阅读模式
英文:

C++ WINAPI Charging

问题

我需要在我的笔记本电脑充电达到95%时停止充电,使用WinAPI C++,我该如何做到这一点?

尝试浏览WinAPI文档,但未找到任何信息。

英文:

I need to stop charging my laptop when it reaches 95% using WINAPI C++ how can I do that?

Tried digging through the winapi documentation but didn't find anything

答案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);
}
英文:

Did you try looking at the power management API?

Here is the code snippet:

#include &lt;Windows.h&gt;

GUID currentPowerScheme;
    PowerGetActiveScheme(NULL, &amp;currentPowerScheme);
    SYSTEM_POWER_STATUS powerStatus;
    GetSystemPowerStatus(&amp;powerStatus);
    ULONG batteryCapacity = powerStatus.BatteryLifePercent;
    DWORD batteryThreshold = 95;

if (batteryCapacity &gt;= 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 = &amp;batteryStopThreshold;

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

huangapple
  • 本文由 发表于 2023年3月8日 19:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定