1

To access the Windows system's current power state (battery level, ac present etc.) from my C++ application, I'm currently using the following WinAPI function:

BOOL WINAPI GetSystemPowerStatus(_Out_ LPSYSTEM_POWER_STATUS lpSystemPowerStatus);

This works well but the results I get with this call are too "old". Maybe this is because Windows or the connected UPS do update these values not that often.

Does anyone know a workaround for this? Or does anyone have a different solution for polling the system's current power state which may deliver more recent results?

Edit: I need this information to inform/warn the user about the power situation (fullscreen app, no Windows shell) and to transfer my application/device into a safe state when the battery level of the UPS gets critical in case of an AC power failure.

jonas
  • 11
  • 2
  • 3
    What is too old? A couple of seconds? minutes? What is your desired rate and what are you getting with this API? – Anders Nov 21 '17 at 18:15
  • In the current situation, I get updates only every 2 or 3 minutes, but I need something in the range of 10 to 20 seconds. The AC state-change is transferred immediately (AC power lost). But when the UPS is on battery, the actual battery level maybe 20% but Windows still reports something around 30% for example. – jonas Nov 21 '17 at 19:43
  • It seems that API wasn't designed for polling but rather to be used in response to a power status change broadcast (PBT_APMPOWERSTATUSCHANGE), which is sent to applications that have registered for those messages. It's possible that the relatively infrequent updates are by design (perhaps at the hardware level) and that there isn't a way to get more up-to-date information. If you care about battery life, then you probably don't want to be polling unnecessarily or frequently. What problem are you trying to solve? – Adrian McCarthy Nov 21 '17 at 21:18
  • Note that the frequency may depend on your battery hardware. There are IOCTL commands for querying about the power source and battery. One of them indicates the granularity of reporting from the hardware. I assume that if your battery circuit provides relatively coarse granularity, there may not be much point in polling more frequently. https://msdn.microsoft.com/en-us/library/windows/desktop/aa372668.aspx – Adrian McCarthy Nov 21 '17 at 21:37
  • @AdrianMcCarthy This may be a hardware issue but maybe there is a possibility to change something in Windows to optimize this delay or there is a different programming technique/API call. If there is nothing like that on the software side, I have to try to solve the issue on the hardware/battery side. – jonas Nov 22 '17 at 09:58
  • You might want to look into the IOCTL_BATTERY_QUERY_STATUS. Instead of polling the status, you set up parameters that specify the battery condition you want to wait for, and it doesn't return the status until those conditions are met. BTW, you still haven't told us what problem you're trying to solve. – Adrian McCarthy Nov 22 '17 at 18:15
  • @AdrianMcCarthy Thanks for the hint. It looks like I first have to somehow sort out if this is a hardware issue. I have just updated my initial question to clarify my problem. – jonas Nov 22 '17 at 19:55

0 Answers0