10

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?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Alan20210202
  • 117
  • 1
  • 3
  • have you looked through: https://msdn.microsoft.com/de-de/library/windows/desktop/aa373163(v=vs.85).aspx ? – robin.koch Aug 07 '17 at 11:15
  • I can't find anything in the (English version) that article that would allow switching 'battery saver' to 'on'. FWIW, looks like you are not alone in this question: https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/8b1dbe33-d8fb-4be8-af53-0de13d288136/enabling-battery-saver-energy-saver-in-windows-10-via-win32-api?forum=powermanagement – Local Needs Aug 18 '17 at 16:00
  • It does take PowerSetActiveScheme(). But sure, you have no idea which particular scheme is the "good one". Tinkering with the power schemes is one of the few ways that a computer manufacturer can make his machine "special", even though everybody uses the exact same components and exact same OS. There is a better project out there. – Hans Passant Oct 31 '17 at 23:07

3 Answers3

4

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.

hidefromkgb
  • 5,834
  • 1
  • 13
  • 44
  • 1
    That would work on Windows, if the battery saver feature is enabled. I think the OP wants to know how to enable/disable the feature. – jwdonahue Nov 02 '17 at 19:02
  • I wonder if setting the thresholds to 0 (using powercfg) is effectively the same as disabling the batterySaver feature – oarevalo Apr 04 '19 at 22:51
1

@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.

oarevalo
  • 3,298
  • 1
  • 22
  • 19
0

You seem to be out of luck. MSDN docs show no API through which the battery saver could be controlled. Examining SettingsHandlers_OneCore_BatterySaver shows that only GetSetting is exposed. Even SetPowerState in WMI Win32_Battery is not implemented -- I know this is not exactly what you need, but it shows that Microsoft has not gotten around to exposing the battery-related functionality. At this point, instead of reverse-engineering the button click, your best bet is probably to emulate it with something like AutoHotKey, however beware of the pitfalls with that.

mnistic
  • 10,866
  • 2
  • 19
  • 33
  • I spent several hours looking at this the other day. There's more than one way to set thresholds and control power levels, but no published means to enable/disable the feature. – jwdonahue Nov 06 '17 at 00:08