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

  • As a result of corporate expansions, mergers, and acquisitions, the teams and technologies that support an organization's IT service management (ITSM) practices can over time become somewhat dispersed. Supporting an organization's strategic objectives, and providing consistent and quality IT support is essential, but this can be challenging where disconnected support environments exist. While physically centralizing support is not always possible, nor is it always desirable, delivering a consistent and …

  • As a development and deployment platform, RHEL offers an efficient, scalable, and robust operating environment with certified security and flexible deployment options in physical and virtualized environments. To assess and quantify the business benefits of RHEL, IDC recently conducted in-depth interviews with IT staff members of 21 companies using RHEL servers. The organizations represent a broad range of industries and have an average of 22,700 employees. RHEL servers accounted for 23% of the servers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds