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 2005
DescriptionThis example shows how to determine whether the computer is running from battery or line power in Visual Basic 2005.
Keywordspower, battery, charging, GetSystemPowerStatus, power status, VB 2005
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 Structure SYSTEM_POWER_STATUS
    Public ACLineStatus As Byte
    Public BatteryFlag As Byte
    Public BatteryLifePercent As Byte
    Public Reserved1 As Byte
    Public BatteryLifeTime As Integer
    Public BatteryFullLifeTime As Integer
End Structure
Private Declare Function GetSystemPowerStatus Lib _
    "kernel32" (ByRef lpSystemPowerStatus As _
    SYSTEM_POWER_STATUS) As Long

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim power_status As SYSTEM_POWER_STATUS
    If GetSystemPowerStatus(power_status) = 0 Then
        lblACStatus.Text = "Error"
    Else
        Select Case power_status.ACLineStatus
            Case 0
                lblACStatus.Text = "Battery"
            Case 1
                lblACStatus.Text = "On line"
            Case 255
                lblACStatus.Text = "Unknown"
        End Select

        Dim txt As String = ""
        If power_status.BatteryFlag And 1 Then txt &= ", " & _
            "High (> 66%)"
        If power_status.BatteryFlag And 2 Then txt &= ", " & _
            "Low (< 33%)"
        If power_status.BatteryFlag And 4 Then txt &= ", " & _
            "Critical (< 5%)"
        If power_status.BatteryFlag And 8 Then txt &= ", " & _
            "Charging"
        If power_status.BatteryFlag And 128 Then txt &= ", " & _
            "No system battery"
        If power_status.BatteryFlag = 255 Then txt &= ", " & _
            "Unknown"
        If txt.Length > 0 Then txt = txt.Substring(2)
        lblBatteryStatus.Text = txt

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

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

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