Subject: Sorry - that’s not quite right
Your code doesn’t identify laptops running on AC power correctly. My laptop correctly returns “9” for a full battery which running on mains power.
That’s because BatteryFlag is a composite bitmask of multiple values.
Try this alternative, imported into a new notes agent:
'Check System Power:
Option Public
Option Declare
%INCLUDE “LSCONST.LSS”
Public Const ERR_UNSUPPORTED_PLATFORM%=9001
Public Const ERR_POWER_STATUS_BATTERYFLAG_UNKNOWN%=9002
Public Const ERR_POWER_STATUS_ACLINESTATUS_UNKNOWN%=9003
Public Const ERR_TESTHARNESS_FAILED% = 8001
Private Type POWER_STATUS
ACLineStatus As Byte
BatteryFlag As Byte
BatteryLifePercent As Byte
Reserved As Byte
BatteryLifeTime As Long
BatteryFullLifeTime As Long
End Type
Declare Function GetSystemPowerStatus Lib “kernel32” (lpSystemPowerStatus As POWER_STATUS) As Long
Private Const POWER_STATUS_BATTERYFLAG_CHARGING% = 8 ’ AC charging
Private Const POWER_STATUS_BATTERYFLAG_NO_BATTERY% = 128 ’ No system battery
Private Const POWER_STATUS_BATTERYFLAG_UNKNOWN% = 255 ’ Unable to access power management battery status - maybe unsupported hardware?
Private Const POWER_STATUS_ACLINESTATUS_OFFLINE%=0 ’ Offline for AC power
Private Const POWER_STATUS_ACLINESTATUS_ONLINE%=1 ’ Online for AC power
Private Const POWER_STATUS_ACLINESTATUS_UNKNOWN%=255 ’ Unknown AC power status - maybe unsupported hardware?
’ =============================================================================================================================
’ Class: SystemPowerStatus
’
’ Dependencies: Requires Windows Vista, Windows XP, or Windows 2000 Professional.
’
’ Constructor
’ New()
’
’ Public Properties: NoSystemBattery() Boolean True if computer has no system battery (ie is a Desktop or is a laptop missing its battery). False for laptop with battery.
’ Online() Boolean True if on ac power (or a laptop being charged from ac line)
’ BatteryLifePercent() Integer Remaining battery life 0 through 100 if known. 255 if unknown or battery has been removed.
’
’ Written: 16/03/07 Martin Audley. Released to the Public Domain. Tested OK on Sony VAIO VGN-A217S, Windows XP, Notes 6.5.4. No warranty
’ =============================================================================================================================
Class SystemPowerStatus
Private m_SysPower As POWER_STATUS
Sub New
Const PLATFORM_WINDOWS_NT$ = "Windows/NT"
Dim vSpecificPlatform As Variant
vSpecificPlatform = Evaluate("@Platform([Specific])") ' Returns Windows/NT for Windows 32 bit platforms including Windows NT and Windows XP
If vSpecificPlatform(0)<> PLATFORM_WINDOWS_NT$ Then ' This code is tested for Windows XP. Untested for Vista or NT. Use at own risk - or test and extend.
Error 9001,Getthreadinfo(2) & |: Do not attempt to instantiate SystemPowerStatus objects on this unsupported operating system platform ("| & vSpecificPlatform(0) & |").| ' Return error. (Class object returned is Nothing)
End If
' Populates content of POWER_STATUS composite type using Win32 "GetSystemPowerStatus" power management function
GetSystemPowerStatus m_SysPower
End Sub
Public Property Get Online As Boolean
' Return True if this computer is a desktop, or a laptop on charge.
If (m_SysPower.ACLineStatus And POWER_STATUS_ACLINESTATUS_UNKNOWN%) = POWER_STATUS_ACLINESTATUS_UNKNOWN% Then
Error ERR_POWER_STATUS_ACLINESTATUS_UNKNOWN%,"System Power Status Error: Unknown status—unable to read POWER_STATUS.ACLineStatus information" ' Bit-wise usage
End If
If ( (m_SysPower.ACLineStatus And POWER_STATUS_ACLINESTATUS_ONLINE%) = POWER_STATUS_ACLINESTATUS_ONLINE%) Then Online = True
End Property
Public Property Get BatteryLifePercent As Integer
' Return percentage battery life from 0 through 100, or 255 if not known.
BatteryLifePercent = m_SysPower.BatteryLifePercent
End Property
Public Property Get NoSystemBattery As Boolean
' Return true if this computer does not have a battery
' You may use this as a mechanism to recognise Laptops, assuming that only a laptop has a battery, and that it always has a battery.
' However, this assumption is untrue for laptops with their battery removed, running on AC power !
If (m_SysPower.BatteryFlag And POWER_STATUS_BATTERYFLAG_UNKNOWN%) = POWER_STATUS_BATTERYFLAG_UNKNOWN% Then
Error ERR_POWER_STATUS_BATTERYFLAG_UNKNOWN%,"System Power Status Error: Unknown status—unable to read POWER_STATUS.BatteryFlag information" ' Bit-wise usage
End If
If ( (m_SysPower.BatteryFlag And POWER_STATUS_BATTERYFLAG_NO_BATTERY%) = POWER_STATUS_BATTERYFLAG_NO_BATTERY%) Then NoSystemBattery = True
End Property
End Class
’ =============================================================================================================================
’ Class: SystemPowerStatusTestHarness
’
’ Purpose: Run a test of SystemPowerStatus properties
’
’ Dependencies: Class SystemPowerStatus
’
‘’ Written: 16/03/07 Martin Audley. Released to the Public Domain. Tested OK on Sony VAIO VGN-A217S, Windows XP, Notes 6.5.4. No warranty
’ =============================================================================================================================
Class SystemPowerStatusTestHarness
Function Run As Boolean
On Error Goto ErrHandler
Dim sps As New SystemPowerStatus
Msgbox "Please inspect your computer now. Confirm whether it is a laptop, whether its battery is fitted, and whether it is currently on AC power. Repeat after removing the laptop battery, if your hardware supports this.",0,"Request"
Msgbox |Has no system battery = "| & Cstr(sps.NoSystemBattery) & |"|,0, "System Power Status Test Result"
Msgbox |Is on AC power = "| & Cstr(sps.Online) & |"|,0, "System Power Status Test Result"
Msgbox |Battery life remaining ("255" means "Unknown") = | & Cstr(sps.BatteryLifePercent) & |%|,0, "System Power Status Test Result"
Run = True
ExitPoint:
Exit Function
ErrHandler:
Error Erl,Error$ & " at line " & Erl & " in " & Getthreadinfo(LSI_THREAD_PROC)
End Function
End Class
Sub Initialize
’ =============================================================================================================================
’ Agent: Check System Power
’
’ Purpose: Report laptop battery status.
’
’ Usage: Save as a menu-triggered agent, with Target = None.
’
’ Written: 16/03/07 Martin Audley. Released to the Public Domain.
’ =============================================================================================================================
On Error Goto ErrHandler
Dim testHarness As New SystemPowerStatusTestHarness
Print testHarness.Run
ExitPoint:
Exit Sub
ErrHandler:
Error Erl,Error$ & " at line " & Erl & " in " & Getthreadinfo(LSI_THREAD_PROC)
End Sub