How to receive battery information using the WinAPI

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read