Jump to content

_BatteryQuery UDF


Ejoc
 Share

Recommended Posts

This gives you info about your Laptop Battery. Requires AutoIt beta 3.1.1.18 or higher.

UDF:

#include-once

;======================================================
;   _BatterQuery()
;   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, 2=Low, 4=Critical,
;                     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)
;======================================================
Func    _BatteryQuery()
    Local   $SystemPower,$ret,$array

    SetError(0)

;Setup $array and $SystemPower
    Dim $array[4]
    $SystemPower    = DllStructCreate("ubyte;ubyte;ubyte;ubyte;udword;udword")
    if @error Then
        SetError(-1)
        return $array
    EndIf

;make the DllCall
    $ret            = DllCall("kernel32.dll","int","GetSystemPowerStatus",_
                                "ptr",DllStructPtr($SystemPower))
    if @error then;DllCall Failed
        SetError(-2)
        DllStructFree($SystemPower)
        return $array
    EndIf

    if Not $ret[0] Then;GetSystemPowerStatus Failed
        SetError(-3)
        DllStructFree($SystemPower)
        return $array
    EndIf

;Fill the array
    $array[0]   = DllStructGet($SystemPower,1); AC
    $array[1]   = DllStructGet($SystemPower,2); Battery Charge
    $array[2]   = DllStructGet($SystemPower,3); Battery Charge %
    $array[3]   = DllStructGet($SystemPower,5); Sec Battery Left

;free the struct
    DllStructFree($SystemPower)

    Return $array
EndFunc

Example script:

#include <_BatteryQuery.au3>
$s          = ""
$battery    = _BatteryQuery()

;AC Power information
Select
    case $battery[0] = 0
        $s  &= "AC Power is Offline" & @CRLF
    case $battery[0] = 1
        $s  &= "AC Power is OnLine" & @CRLF
    case Else
        $s  &= "AC Power is Unknown" & @CRLF
EndSelect

;Battery Status
if BitAnd($battery[1],128) Then
    $s  &= "No Battery present" & @CRLF
Else
    $s  &= "Battery Status: "
    If BitAnd($battery[1],8) Then $s &= "Charging, "
    Select
        case BitAnd($battery[1],4)
            $s &= "Critical"
        case BitAnd($battery[1],2)
            $s &= "Low"
        case BitAnd($battery[1],1)
            $s &= "High"
    EndSelect
    if $battery[2] < 101 Then $s &= "(" & $battery[2] & "%)"
    If $battery[3] <> 4294967295 Then; Unknown
        $s &= @CRLF & "Minutes Left of Charge: " & $battery[3] / 60 & @CRLF
    Else
        $s &= @CRLF & "Minutes Left of Charge: Unknown" & @CRLF
    EndIf
EndIf

MsgBox(0,"Battery Information",$s)

_BatteryQuery.au3

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

This gives you info about your Laptop Battery.  Requires AutoIt beta 3.1.1.18 or higher.

UDF:

..

<{POST_SNAPBACK}>

Hello Ejoc,

Amazing piece of code. But I'm puzzled: Can you achieve the same information using WMI? (I currently don't have a laptop here, so I couldn't test it)

; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Battery", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "BatteryRechargeTime: " & $objItem.BatteryRechargeTime & @CRLF
      $Output = $Output & "BatteryStatus: " & $objItem.BatteryStatus & @CRLF
      $Output = $Output & "EstimatedChargeRemaining: " & $objItem.EstimatedChargeRemaining & @CRLF
      $Output = $Output & "EstimatedRunTime: " & $objItem.EstimatedRunTime & @CRLF
      $Output = $Output & "ExpectedBatteryLife: " & $objItem.ExpectedBatteryLife & @CRLF
      $Output = $Output & "ExpectedLife: " & $objItem.ExpectedLife & @CRLF
      $Output = $Output & "Status: " & $objItem.Status & @CRLF
      $Output = $Output & "TimeToFullCharge: " & $objItem.TimeToFullCharge & @CRLF
   Next
   ConsoleWrite($Output)
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Battery" )
Endif

Regards,

-Sven

Link to comment
Share on other sites

Hello Ejoc,

Amazing piece of code. But I'm puzzled: Can you achieve the same information using WMI? (I currently don't have a laptop here, so I couldn't test it)

:( I have no idea, never used WMI.

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

  • 2 years later...
  • 9 months later...

Update of the DLL call version from the OP, in line with current AutoIt DLL functions:

#include-once

;======================================================
;   _BatteryQuery()
;   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, 2=Low, 4=Critical,
;                     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)
;======================================================
Func _BatteryQuery()
    Local $SystemPower, $ret, $array[4]

; 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

; free the struct
    $SystemPower = 0

    Return $array
EndFunc  ;==>_BatteryQuery

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 2 months later...
  • 6 months later...
  • 2 months later...

Amazing piece of code. But I'm puzzled: Can you achieve the same information using WMI?

:D I have no idea, never used WMI.

guys, i know i am answering a question asked years ago, but i am putting my experience because it might be relevant to people like me who might still be looking for a WMI Call on BatteryStatus!

i have tried quiet a few windows xp (upto SP3) systems and WMI calls on battery from Win32_Battery , Win32_AssociatedBattery , Win32_SystemEnclosure classes dont give u any idea if u have a AC Main supply and not on Battery power (typical desktop models)

on the other hand, while running on Battery, there is good deal of information available from Win32_Battery Class which might be the answer to the question.

oops! forgot to thank Ejoc & PsaltyDS for the function
Edited by rajeshontheweb
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...