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 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:

 - Battery status flags:
- Device power state:

 - General constants:

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


Download source - 2 Kb
Download demo application - 40 Kb


  • for display

    Posted by Legacy on 01/11/2002 12:00am

    Originally posted by: skon

    how use GetDevicePowerState for display??
    for i know monitor is off or on?

    HDC monitor;
    monitor = GetDC( NULL );
    if ( GetDevicePowerState (monitor) )

    not work.... :(

  • Getting battery information in detail

    Posted by Legacy on 06/05/2001 12:00am

    Originally posted by: Shravan kumar

    I am writing a test program for to test the energy or charge levels of my(ACPI)Battery , related
    information like Finding full charged capacity etc.... on windows platform. In this i am sending a IOCTL_BATTERY_SET_INFORMATION with a call to DeviceIoControl( ) function. The first parameter of this function is hDevice [in] Handle to the device which can be obtained by CreateFile( ) functiion. Till now it is ok but what shall i pass as the first parameter ti this CreateFile( ) function which is a Pointer to a null-terminated string that specifies the name of the object to create or open.
    If any one can help me regarding this it will be a great
    help for me. Or where can i get the related source code regarding this.??

    Please mail your suggestions to:

    Thanks & Regards

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 23, 2015 The cloud is not just about a runtime platform for your projects – now, you can do your development in the cloud, too. Check out this webcast to learn how the cloud improves your development experience and team collaboration. Join Dana Singleterry, Principal Product Manager for Oracle Dev Tools, as he discusses how to simplify every aspect of the development lifecycle, including requirements gathering, version management, code reviews, build automation, and …

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date