Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/disable Windows 10 battery saver in program?

Tags:

c

windows

winapi

I'm writing a small program to save my laptop's battery, and I can now switch between power schemes using PowerSetActiveScheme.

The next step is to control the battery saver in Windows 10. Though I can read the state of it using GetSystemPowerStatus, I can't find a way to enable/disable it programmatically. Are there any functions in Windows API to do this?

like image 433
Alan20210202 Avatar asked Aug 07 '17 09:08

Alan20210202


2 Answers

Most probably you can do it Linux-way, by calling a system app named PowerCfg through ShellExecuteEx():

powercfg /setdcvalueindex SCHEME_CURRENT SUB_ENERGYSAVER ESBATTTHRESHOLD 100
powercfg /setactive scheme_current

This means that the energy saver is activated even when the battery percentage equals 100%. SUB_ENERGYSAVER and its sub-GUID ESBATTTHRESHOLD are described here.

like image 162
hidefromkgb Avatar answered Sep 28 '22 00:09

hidefromkgb


@hidefromkgb's answer is pretty much correct. The only missing part is that to disable Energy Saver and prevent it from turning it on, you need to do:

powercfg /setdcvalueindex SCHEME_CURRENT SUB_ENERGYSAVER ESBATTTHRESHOLD 0
powercfg /setdcvalueindex SCHEME_CURRENT SUB_ENERGYSAVER ESBRIGHTNESS 100 

If you do that, and go back to the control panel Battery Saver section, you will see that the first checkbox is now disabled (although it still shows 20% but it is grayed out so it should be ok). Also the second checkbox (lower screen brightness) will be unchecked.

like image 34
oarevalo Avatar answered Sep 28 '22 01:09

oarevalo