Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDetermine whether the computer is running from battery or line power in Visual Basic 6
DescriptionThis example shows how to determine whether the computer is running from battery or line power in Visual Basic 6.
Keywordspower, battery, charging, GetSystemPowerStatus, power status, Visual Basic 6
CategoriesWindows, API
 
The program uses the GetSystemPowerStatus API function to get power information. When the program starts, it calls this function and looks at the returned fields to determine whether the computer is running on battery or line power, to get the power status (high, low, critical, charging, no battery, or unknown), and to get information about the battery's total and remaining life time.
 
Private Declare Function GetSystemPowerStatus Lib _
    "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) _
    As Long
Private Type SYSTEM_POWER_STATUS
    ACLineStatus As Byte
    BatteryFlag As Byte
    BatteryLifePercent As Byte
    Reserved1 As Byte
    BatteryLifeTime As Long
    BatteryFullLifeTime As Long
End Type

Private Sub Form_Load()
Dim power_status As SYSTEM_POWER_STATUS
Dim txt As String

    If GetSystemPowerStatus(power_status) = 0 Then
        lblACStatus.Caption = "Error"
    Else
        Select Case power_status.ACLineStatus
            Case 0
                lblACStatus.Caption = "Battery"
            Case 1
                lblACStatus.Caption = "On line"
            Case 255
                lblACStatus.Caption = "Unknown"
        End Select

        If power_status.BatteryFlag And 1 Then txt = txt & _
            ", High (> 66%)"
        If power_status.BatteryFlag And 2 Then txt = txt & _
            ", Low (< 33%)"
        If power_status.BatteryFlag And 4 Then txt = txt & _
            ", Critical (< 5%)"
        If power_status.BatteryFlag And 8 Then txt = txt & _
            ", Charging"
        If power_status.BatteryFlag And 128 Then txt = txt _
            & ", No system battery"
        If power_status.BatteryFlag = 255 Then txt = txt & _
            ", Unknown"
        If Len(txt) > 0 Then txt = Mid$(txt, 3)
        lblBatteryStatus.Caption = txt

        If power_status.BatteryFullLifeTime = -1 Then
            lblFullLifetime.Caption = "Unknown"
        Else
            lblFullLifetime.Caption = _
                power_status.BatteryFullLifeTime & " " & _
                "seconds"
        End If

        If power_status.BatteryLifeTime = -1 Then
            lblRemainingLifetime.Caption = "Unknown"
        Else
            lblRemainingLifetime.Caption = _
                power_status.BatteryLifeTime & " seconds"
        End If

        If power_status.BatteryLifePercent = 255 Then
            lblPercentLifetime.Caption = "Unknown"
        Else
            lblPercentLifetime.Caption = _
                power_status.BatteryLifePercent & "%"
        End If
    End If
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated