Jump to content
Sign in to follow this  
madhoinz

Unable to perform a DLL function call from PowrProf.dll

Recommended Posts

madhoinz

After creating some smaller programs (e.g. VoIPdial) with AutoIt, I know got stuck when I tried to use some 'more advanced' AutoIt features. Inspired by the UDF for reading some laptop battery informations, I wanted to get more informations about my laptops battery. Searching the internet I found the PowrProf.dll function CallNtPowerInformation which returns the data structure SYSTEM_BATTERY_STATE. Having found this information I started to write some code that hopefully would return me the desired information... But after some rounds of unsuccessful guessing which might be the correct parameters for calling this DLL function, I hope that someone on this forum might help me with this problem.

TIA Martin

;Create $Array for results
Dim $Array[10]

;Create structure for SYSTEM_BATTERY_STATE
$SystemBatteryState=DllStructCreate("ubyte;ubyte;ubyte;ubyte;ubyte;udword;udword;udword;udword;udword;udword")
If @Error Then
    MsgBox(0,"DllStructureCreate",@Error)
EndIf

;Execute DllCall
$Ret=DllCall("PowrProf.dll","ptr","CallNtPowerInformation","int","5","ptr","0","long_ptr","0","ptr",DllStructGetPtr($SystemBatteryState),"long_ptr","10000")
If @Error Then
    MsgBox(0,"DllCall",@Error)
EndIf

;Write results to array
$Array[0]=DllStructGetData($SystemBatteryState,1) ;AcOnLine
$Array[1]=DllStructGetData($SystemBatteryState,2) ;BatteryPresent
$Array[2]=DllStructGetData($SystemBatteryState,3) ;Charging
$Array[3]=DllStructGetData($SystemBatteryState,4) ;Discharging
$Array[4]=DllStructGetData($SystemBatteryState,6) ;MaxCapacity
$Array[5]=DllStructGetData($SystemBatteryState,7) ;RemainingCapacity
$Array[6]=DllStructGetData($SystemBatteryState,8) ;Rate
$Array[7]=DllStructGetData($SystemBatteryState,9) ;EstimatedTime
$Array[8]=DllStructGetData($SystemBatteryState,10) ;DefaultAlert1
$Array[9]=DllStructGetData($SystemBatteryState,11) ;DefaultAlert2

;Free memory allocated to DLL structure
$SystemBatteryState=0

;Show results
$Output="AcOnLine: " & $Array[0] & @CRLF
$Output=$Output & "BatteryPresent: " & $Array[1] & @CRLF
$Output=$Output&"Charging: " & $Array[2] & @CRLF
$Output=$Output&"Discharging: " & $Array[3] & @CRLF
$Output=$Output&"MaxCapacity: " & $Array[4] & @CRLF
$Output=$Output&"RemainingCapacity: " & $Array[5] & @CRLF
$Output=$Output&"Rate: " & $Array[6] & @CRLF
$Output=$Output&"EstimatedTime: " & $Array[7] & @CRLF
$Output=$Output&"DefaultAlert1: " & $Array[8] & @CRLF
$Output=$Output&"DefaultAlert2: " & $Array[9] & @CRLF
MsgBox(0,"SystemBatteryState",$Output)

Share this post


Link to post
Share on other sites
Zedna

Try this:

...

;Create structure for SYSTEM_BATTERY_STATE
$SystemBatteryState=DllStructCreate("int;int;int;int;int;dword;dword;dword;dword;dword;dword")
If @Error Then
    MsgBox(0,"DllStructureCreate",@Error)
EndIf

;Execute DllCall
$Ret=DllCall("PowrProf.dll","ptr","CallNtPowerInformation","int","5","ptr","0","long","0","ptr",DllStructGetPtr($SystemBatteryState),"long",DllStructGetSize($SystemBatteryState))
If @Error Then
    MsgBox(0,"DllCall",@Error)
EndIf

...

Share this post


Link to post
Share on other sites
madhoinz

@Zedna: Thank you very much for your code!

With the parameters proposed by Zedna I can receive some values that make sense. On the first sight the mapping of the variables to the array seemed not to be as documented on the MSDN website. The first thing I realized and changed was that 'Spare1[4];' means that there are 4 spare elements. After adding the missing spare elements, more valid results showed up. The problem that remains is that the first array element contains a large number, the second is always zero and then the variables defined as 'dword' are returned completely. Probably I have to try different parameters for the 'boolean' elements.

Greetings Martin

Share this post


Link to post
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
Sign in to follow this  

×