How to receive battery information using the WinAPI

This sample show's how to retrieve the current battery information for your laptop without the need for the SysInfo.ocx control.

Screen Shot

The program utilises the following API call's and structure to obtain the information :


private Declare Function GetSystemPowerStatus Lib "kernel32" _
        (lpSystemPowerStatus as SYSTEM_POWER_STATUS) as Long
private Declare Function SetSystemPowerState Lib "kernel32" _
        (byval fSuspend as Long, byval fForce as Long) as Long

private Type SYSTEM_POWER_STATUS
    ACLineStatus as Byte 'Checks to see if you're connected to the
    'wall outlet or not
    BatteryFlag as Byte 'Battery status
    BatteryLifePercent as Byte 'precentage left
    Reserved1 as Byte 'Dont use
    BatteryLifeTime as Long 'Total time left of your battery
    BatteryFullLifeTime as Long 'Total UPtime of your battery
End Type





As you can see, it takes up much less space in your program and distributable than the 67k required by SysInfo.ocx

Download zipped project file (7k)


Comments

  • battery level programming

    Posted by Legacy on 11/19/2003 12:00am

    Originally posted by: jerome

    very useful, thanks...

    Reply
  • SetSystemPowerState

    Posted by Legacy on 01/13/2000 12:00am

    Originally posted by: Niraj Bhatt

    I can not see the use of this API in your sample..

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

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds