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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds