CBattery Non-MFC Class | CodeGuru

CBattery Non-MFC Class

Environment: Visual C++ 6 SP3 CBattery is a non-MFC class wrapper for the battery related Windows Power Management API functions. It may be used by notebook/laptops system utilities developed with Visual C++. CBattery implements the following public methods:  – BOOL SystemPowerStatusLoaded()  – BOOL ReloadSystemPowerStatus()  – SYSTEM_POWER_STATUS SystemPowerStatus()  – CBattery::AC_LINE_STATUS ACLineStatus()  – CBattery::BATTERY_FLAG BatteryFlag()  – CBattery::BOOL […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 21, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Visual C++ 6 SP3

CBattery is a non-MFC class wrapper for the battery related Windows Power
Management API functions. It may be used by notebook/laptops system utilities
developed with Visual C++.

CBattery implements the following public methods:
 – BOOL SystemPowerStatusLoaded()
 – BOOL ReloadSystemPowerStatus()
 – SYSTEM_POWER_STATUS
SystemPowerStatus()
 – CBattery::AC_LINE_STATUS
ACLineStatus()
 – CBattery::BATTERY_FLAG
BatteryFlag()
 – CBattery::BOOL BatteryCharging()
 – BYTE BatteryLifePercent()
 – long BatteryFullTime()
 – long BatteryLifeTime()
 – BOOL BatteryIsUnknown()
 – BOOL BatteryIsCharging()
 – CBattery::DEVICE_POWER_STATE
GetDevicePowerState()
 – void SetDevice(HANDLE hDevice)
 – HANDLE GetDevice()

CBattery contains the following public constants:
 – Line status:
  
ACLS_OFFLINE
   ACLS_ONLINE
   ACLS_UNKNOWN

 – Battery status flags:
  
BF_HIGH
   BF_LOW
   BF_CRITICAL
   BF_CHARGING
   BF_NO_BATTERY
   BF_UNKNOWN
 
– Device power state:
  
DPS_OFF
   DPS_ON
   DPS_UNKNOWN

 – General constants:
  
UNKNOWN_BATTERY_LIFE_PERCENT
   UNKNOWN_BATTERY_FULL_TIME
   UNKNOWN_BATTERY_LIFE_TIME
   UNKNOWN_DEVICE_POWER_STATE

Notes:
 
– The system is only capable of estimating BatteryFullTime based on
calculations on BatteryLifeTime and BatteryLifePercent. Without
smart battery subsystems, this value may not be accurate enough to be useful.
 – The class does not use the
Sysinfo OCX control.
 – The system broadcasts the PBT_APMPOWERSTATUSCHANGE
event to all applications when the power status changes. When your application
receives this event, it should call the methods of the class again to
determine what changed.
 – Power management specifications
and white papers from the OnNow initiative home page are located at www.microsoft.com/hwdev/onnow.htm.
 – The PBT_* messages are
documented in the SDKs PBT.H.
 – An application can use CBattery::GetDevicePowerState
to determine whether a disk or other device is spun up. If the device is not
spun up, the application should defer accessing it.
 – For more details about APM, one
should check the Platform SDK: Hardware.
 – Unfortunately, power management
features dont work in Windows NT 4.0! However, if youre using a
specialized version of Windows NT, modified by your computer manufacturer, the
methods may work.
 – The demo included application
does not use all the all methods implemented in the CBattery class. Check the
Battery.cpp and Battery.h files for the class source code.

Downloads

Download source – 2 Kb
Download demo application – 40 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.