Ejoc Posted April 29, 2005 Share Posted April 29, 2005 This gives you info about your Laptop Battery. Requires AutoIt beta 3.1.1.18 or higher. UDF: expandcollapse popup#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: expandcollapse popup#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 More sharing options...
SvenP Posted April 29, 2005 Share Posted April 29, 2005 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" ) EndifRegards,-Sven Link to comment Share on other sites More sharing options...
Ejoc Posted April 29, 2005 Author Share Posted April 29, 2005 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 More sharing options...
possy_99 Posted September 1, 2007 Share Posted September 1, 2007 just a quick note, WMI method works with vista, you just have to change localhost to the computer name Link to comment Share on other sites More sharing options...
PsaltyDS Posted June 5, 2008 Share Posted June 5, 2008 Update of the DLL call version from the OP, in line with current AutoIt DLL functions: expandcollapse popup#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 More sharing options...
FreeBeing Posted September 4, 2008 Share Posted September 4, 2008 Thank You, I like that script, It help me to detect if a computer is Laptop or not Link to comment Share on other sites More sharing options...
duncanchaos Posted March 28, 2009 Share Posted March 28, 2009 (edited) Thanks for this script. I need to be able to find out how long it takes to charge the battery in a laptop and can use this for the basis of my script. Edited March 28, 2009 by duncanchaos Link to comment Share on other sites More sharing options...
rajeshontheweb Posted June 22, 2009 Share Posted June 22, 2009 (edited) Amazing piece of code. But I'm puzzled: Can you achieve the same information using WMI? 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 June 22, 2009 by rajeshontheweb Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now