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

  • Wednesday, September 24, 2014 8:00 AM - 9:00 AM PDT According to a recent Forrester Research report, many companies are choosing low-code platforms over traditional programming platforms, due to the speed with which low-code apps can be assembled and tested. With customer-facing applications on the rise, traditional programming platforms simply can't keep up with the "short schedules and rapid change cycles" required to develop these applications. Check out this upcoming webinar and join Clay Richardson from …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds