Using the PowerStatus Class

The PowerStatus class consists of a few read-only properties, so it’s simple to use. The following short sections demonstrate using each of the properties.

None of the following fragments will function correctly under standard Windows NT 4.x, because it doesn’t support power management functionality. However, if you’re using a specialized version of Windows NT, modified by your computer manufacturer, these functions may work.

The ACLineStatus Property

The ACLineStatus property indicates whether the power connection is online or offline. The property returns one of the flags pwrACLineOffline, pwrACLineOnline, pwrACLineBackupPower, or pwrACLineUnknown. For example, you could write code like the following to display the AC power status:

Dim ps As New PowerStatus
Dim strOut As String
Select Case ps.ACLineStatus
    Case ps.pwrACLineOffline
        strOut = "Batteries"
    Case ps.pwrACLineOnline
        strOut = "Plugged in"
    Case ps.pwrACLineBackupPower
        strOut = "Using backup power"
    Case ps.pwrACLineUnknown
        strOut = "Unknown"
End Select

The BatteryState Property

The BatteryState property returns information about the charge state of the computer’s battery. It returns one of the flags pwrBatteryStateHigh, pwrBatteryStateLow, pwrBatteryStateCritical, pwrBatteryStateNoBattery, or pwrBatteryStateUnknown. You could write code like this to display the battery-charging status:

Dim ps As New PowerStatus
Dim strOut As String
Select Case ps.BatteryState
    Case ps.pwrBatteryStateHigh
        strOut = "Full charge"
    Case ps.pwrBatteryStateLow
        strOut = "Low charge"
    Case ps.pwrBatteryStateCritical
        strOut = "Critical"
    Case ps.pwrBatteryStateNoBattery
        strOut = "No battery"
    Case ps.pwrBatteryStateUnknown
        strOut = "Unknown"
End Select

The BatteryCharging Property

The BatteryCharging property simply returns a Boolean True or False, indicating the current charging state of the battery. If the state is unknown, the property returns False.

The BatteryLifePercent, BatteryLifeTime, and BatteryFullLifetime Properties

These three properties work together to provide information about how long the battery will provide power. The BatteryLifeTime property returns the number of remaining seconds the battery has, and the BatteryFullLifeTime property returns the total number of seconds the battery should last if fully charged. The BatteryLifePercent property returns the percentage of lifetime remaining, as an integer between 0 and 100. Both the BatteryLifeTime and BatteryFullLifeTime properties return the prwBatteryLifeUnknown constant if their status is unknown. The BatteryLifePercent property returns the pwrBatteryPercentageUnknown if its value is unknown.

The Suspend Method

Call the Suspend method of the PowerStatus class to suspend the computer, if it supports the functionality. The method returns 0 if it fails, so you can check the return value to see whether the computer was actually suspended.

© 1997 by SYBEX Inc. All rights reserved.