Help needed testing this script

Here’s a script I developed which may be useful in determining whether or not the Notes client is running on a laptop. The presumption is that if a battery is detected, it must be a laptop (that may be a dangerous assumption!). I’ve done limited testing and it appears to work OK. Note that this is for Win32 only.

I’d appreciate broader testing to see how useful this is. Thanks in advance if you can help out.


Option Declare

Private Type SYSTEM_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 SYSTEM_POWER_STATUS) As Long

Sub Click(Source As Button)

Dim SysPower As SYSTEM_POWER_STATUS	

Dim msg As String



GetSystemPowerStatus SysPower



Select Case SysPower.BatteryFlag

Case	1,2,4,8:

	msg = "You are working on a Laptop"

Case 128: 

	msg = "You are working on a Desktop" 

Case 255: 

	msg = "Unable to determine PC type"

End Select



Msgbox msg ,, "Status"

End Sub

Subject: Help needed testing this script

Worked ok for me.IBM ThinkPad - docked.

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

Subject: RE: Sorry - that’s not quite right

Thanks Martin. Very thorough.

Subject: RE: Sorry - that’s not quite right

I was running on AC on my laptop and it worked

Subject: You can’t use BatteryFlag property values 1,2,4 to detect a battery

See SYSTEM_POWER_STATUS (winbase.h) - Win32 apps | Microsoft Learn

BatteryFlag

The battery charge status. This member can contain one or more of the following flags.

Value 	Meaning

1 	High—the battery capacity is at more than 66 percent

2 	Low—the battery capacity is at less than 33 percent

4 	Critical—the battery capacity is at less than five percent

8 	Charging

128 	No system battery

255 	Unknown status—unable to read the battery flag information



The value is zero if the battery is not being charged and the battery capacity is between low and high.

Subject: Help needed testing this script

worked very well for me.tested on an IBM Laptop, Lotus Notes R7.0.2

tested on a Gateway Tower, Lotus Notes R8 Beta

Nicely done

Mike

Subject: RE: Help needed testing this script

Thanks Mike.

…Jack