#Region Example
#include <array.au3>
$batch = ""
If $CmdLine[0] = 1 Then
$batch = $CmdLine[1]
EndIf
While 1
$batt = _BatteryQueryEx()
;_ArrayDisplay($batt)
If $batt[0] = 0 Then
;MsgBox(0, "", "The laptop is NOT plugged in")
If $batch Then
ShellExecute($batch)
EndIf
ElseIf $batt[0] = 1 Then
MsgBox(0, "", "The laptop IS plugged in")
EndIf
Sleep(5000) ; check every 5 seconds
WEnd
#EndRegion
#include-once
;======================================================
; _BatteryQueryEx()
; original author PsaltyDS http://www.autoitscript.com/forum/index.php?showtopic=10993&view=findpost&p=531988
; Rajesh V R - addition of BatteryFullLifeTime
; Return information on the Battery
; Sets @Error on error
; Returns an array:
; $array[0] = ACPower(0=offline, 1=online, 255=unknown)
; $array[1] = BatteryFlag(1=High>66%, 2=Low<33%, 4=Critical<5%,
; 8=Charging 128=No Battery, 255=Unknown
; Use BitAnd to test, ie BitAnd($array[1],128)
; $array[2] = BatteryLife %(0-100, 255=unknown)
; $array[3] = Seconds left of charge, estimate(4294967295=unknown)
; $array[4] = BatteryLife @ Full Capacity (Seconds of battery life if fully charged -1 if info not available Not applicable for AC Source)
; @Link : http://msdn.microsoft.com/en-us/library/aa373232(VS.85).aspx
;======================================================
Func _BatteryQueryEx()
Local $SystemPower, $ret, $array[5]
; Setup $array and $SystemPower
$SystemPower = DllStructCreate("ubyte;ubyte;ubyte;ubyte;ulong;ulong")
If @error Then
SetError(-1)
Return $array
EndIf
; make the DllCall
$ret = DllCall("kernel32.dll", "int", "GetSystemPowerStatus", "ptr", DllStructGetPtr($SystemPower))
If @error Then;DllCall Failed
SetError(-2)
$SystemPower = 0
Return $array
EndIf
If Not $ret[0] Then; GetSystemPowerStatus Failed
SetError(-3)
$SystemPower = 0
Return $array
EndIf
; Fill the array
$array[0] = DllStructGetData($SystemPower, 1); AC
$array[1] = DllStructGetData($SystemPower, 2); Battery Charge
$array[2] = DllStructGetData($SystemPower, 3); Battery Charge %
$array[3] = DllStructGetData($SystemPower, 5); Sec Battery Left
$array[4] = DllStructGetData($SystemPower, 6); Sec Battery Capacity @ Full Charge
; free the struct
$SystemPower = 0
Return $array
EndFunc ;==>_BatteryQueryEx