Skip to main content
DetectAC         Detect AC power status on Windows

Why detect AC power status?

Laptops are becoming more and more popular. One advantage is that they can be used without power supply with the help of a battery. Unfortunately when a laptop is not connected to a power supply the performace will be degraded (sometimes considerably). It can be useful to detect this and show a message to the user explaining why the application might not perform very well.
This code snippet shows how the "detecting part" can be done using the Windows API.

Code snippet

Copy to clipboard
bool Utils::GetBatteryCharging() { SYSTEM_POWER_STATUS _status; GetSystemPowerStatus(&_status); if(_status.ACLineStatus == 0) { return false; } else { return true; } }

Dont forget to include windows headers also for this to work:

Copy to clipboard
#include "windows.h"

--Borundin 20:26, 4 July 2008 (BST)