Skip to main content

History: DetectAC

Source of version: 3 (current)

Copy to clipboard
            {maketoc}

!!!!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
{CODE(wrap="1", colors="c++")}  bool
  Utils::GetBatteryCharging()
  {
       SYSTEM_POWER_STATUS _status;
       GetSystemPowerStatus(&_status);
       if(_status.ACLineStatus == 0)
       {
           return false;
       } else {
           return true;
       }
  }
{CODE}
Dont forget to include windows headers also for this to work:

{CODE(wrap="1", colors="c++")}  #include "windows.h"
{CODE}
--((User:Borundin|Borundin)) 20:26, 4 July 2008 (BST)