Jump to content
Sign in to follow this  
targeter

Dllcall syntax? for cooling status (winapi)

Recommended Posts

targeter

This might be up your alley again Yashied... Sorry about the winapi questions. They have me in a dizzy this week trying to figure all of them out....

I am trying to pull the System_Power_Information strucutre's cooling value

#Region Header
#Include-once

#Include <WinAPI.au3>

#EndRegion Header


Global Const $AdministratorPowerPolicy = 9
Global Const $LastSleepTime = 15
Global Const $LastWakeTime = 14
Global Const $ProcessorInformation = 11
Global Const $ProcessorPowerPolicyAC = 18
Global Const $ProcessorPowerPolicyCurrent = 22
Global Const $ProcessrPowerPolicyDc = 19
Global Const $SystemBatteryState = 5
Global Const $SystemExecutionState = 16
Global Const $SystemPowerCapabilities = 4
Global Const $SystemPowerInformation = 12
Global Const $SystemPowerPolicyAc = 8
Global Const $SystemPowerPolicyCurrent = 8
Global Const $SystemPowerPolicyDc = 1
Global Const $SystemReserveHiberFile = 10
Global Const $VerifyProcessorPowerPolicyAc = 20
Global Const $VerifyProcessorPolicyDc = 21
Global Const $VerifySystemPowerAc = 2
Global Const $VerifySystemPolicyDc = 3


Global Const $tagSYSTEM_POWER_INFORMATION = 'ulong MaxIdlenessAllowed;ulong Idleness;ulong TimeRemaining;char CoolingMode'


#EndRegion Global Variables and Constants

Func _NTCallPowerInfo()


    Local $tSPI = DllStructCreate($tagSystem_Power_Information)
    Local $Ret, $array

   If @error Then
        SetError(-1)
        Return $array
    EndIf

        $Ret = DllCall('PowrProf.dll', 'int', 'CallNTPowerInformation', 'int', $SystemPowerInformation,'int',0, 'int',0, 'int', DllStructGetPtr($tSPI), 'int', DllStructGetSize($tSPI))
        If @error Then;DllCall Failed
        SetError(-2)
        $tSPI = 0
        Return $array
    EndIf

    If Not $ret[0] Then; Call for SystemPowerInformation Failed
        SetError(-3)
        $tSPI = 0
        Return $array
    EndIf

consolewrite(DllStructGetData($tSPI, 3))



EndFunc

http://msdn.microsoft.com/en-us/library/aa372675(VS.85).aspx - CallNTPowerInformation

What am I missing here? It runs but no return to console.....

Edited by targeter

Share this post


Link to post
Share on other sites
targeter

http://www.autoitscript.com/forum/index.php?showtopic=88401

Found a semi solution but the consolewrite seems to be stuck in a loop when it gets to coolingmode

Share this post


Link to post
Share on other sites
SmOke_N

Wow, that's not even a fun one to attempt IMO. Pointers to Structs that need Pointers etc.

Anyway, for giggles, I googled it ( giggles and google were no attempt in humor ):

http://www.google.com/search?q=CallNTPowerInformation+site%3Ahttp%3A%2F%2Fautoitscript.com&ie=utf-8&oe=utf-8&aq=t

The Google query: CallNTPowerInformation site:http://autoitscript.com

Amusingly ... yours is already there for me ;)

Edit:

I see you found the thread. If I were you, I'd attempt to see what he did.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
targeter

Wow, that's not even a fun one to attempt IMO. Pointers to Structs that need Pointers etc.

Anyway, for giggles, I googled it ( giggles and google were no attempt in humor ):

http://www.google.com/search?q=CallNTPowerInformation+site%3Ahttp%3A%2F%2Fautoitscript.com&ie=utf-8&oe=utf-8&aq=t

The Google query: CallNTPowerInformation site:http://autoitscript.com

Amusingly ... yours is already there for me :)

Edit:

I see you found the thread. If I were you, I'd attempt to see what he did.

Google is quick to index apparently ;)

Doh! I didn't see the for loop. Well that solved it. ;) Now I just feel sheepish.

Thanks for making me take a second look at the code.

Edited by targeter

Share this post


Link to post
Share on other sites
targeter

Google is quick to index apparently :)

Doh! I didn't see the for loop. Well that solved it. ;) Now I just feel sheepish.

Thanks for making me take a second look at the code.

I may have spoken too soon. I just changed the pass back value to the CoolingMode. Can anyone else confirm that they are seeing no value in the console?

#include <GUIConstantsEx.au3>

Global $Idleness = 0

For $n = 1 To 20
    GetPowerInformation($Idleness)
    ConsoleWrite("TEST " & $n & ":  " & $Idleness & @LF & @LF)
    Sleep(1000)
Next

Func GetPowerInformation(ByRef $A)
;Create structure for SYSTEM_POWER_INFORMATION
    $SystemPowerInformation = DllStructCreate("long;long;long;char")
    If @error Then
        MsgBox(0, "DllStructureCreate", @error)
    EndIf

;Execute DllCall
    $Ret = DllCall("PowrProf.dll", "ptr", "CallNtPowerInformation", _
            "int", "12", _
            "ptr", "", _
            "long", "0", _
            "ptr", DllStructGetPtr($SystemPowerInformation), _
            "long", DllStructGetSize($SystemPowerInformation))
    If @error Then ConsoleWrite("Debug:  DllCall error:  " & @error & @LF)
 ;   ConsoleWrite("Debug: $Ret[0] = " & $Ret[0] & @LF)
 ;   ConsoleWrite("Debug: ULONG MaxIdlenessAllowed = " & DllStructGetData($SystemPowerInformation, 1) & @LF)
 ;   ConsoleWrite("Debug: ULONG Idleness = " & DllStructGetData($SystemPowerInformation, 2) & @LF)
 ;   ConsoleWrite("Debug: ULONG TimeRemaining = " & DllStructGetData($SystemPowerInformation, 3) & @LF)
 ;   ConsoleWrite("Debug: UCHAR CoolingMode = " & DllStructGetData($SystemPowerInformation, 4) & @LF)

    $A = DllStructGetData($SystemPowerInformation, 4);CoolingMode
    $SystemPowerInformation = 0
EndFunc  ;==>GetPowerInformation
Edited by targeter

Share this post


Link to post
Share on other sites
jaberwacky

'CallNTPowerInformation' doesn't return an array. It returns NTSTATUS. You have to get the data from the structure. I got this so far but the struct doesn't seem to contain any information? I dunno. Don't worry, I'm not going to post endless reams of unstructured information this time!

#region Header
#include-once
#include <WinAPI.au3>
#include <Array.au3>
#endregion Header

#region Global Variables and Constants
Global Const $AdministratorPowerPolicy = 9
Global Const $LastSleepTime = 15
Global Const $LastWakeTime = 14
Global Const $ProcessorInformation = 11
Global Const $ProcessorPowerPolicyAC = 18
Global Const $ProcessorPowerPolicyCurrent = 22
Global Const $ProcessrPowerPolicyDc = 19
Global Const $SystemBatteryState = 5
Global Const $SystemExecutionState = 16
Global Const $SystemPowerCapabilities = 4
Global Const $SystemPowerInformation = 12
Global Const $SystemPowerPolicyAc = 8
Global Const $SystemPowerPolicyCurrent = 8
Global Const $SystemPowerPolicyDc = 1
Global Const $SystemReserveHiberFile = 10
Global Const $VerifyProcessorPowerPolicyAc = 20
Global Const $VerifyProcessorPolicyDc = 21
Global Const $VerifySystemPowerAc = 2
Global Const $VerifySystemPolicyDc = 3
Global Const $tagSYSTEM_POWER_INFORMATION = 'ulong MaxIdlenessAllowed;ulong Idleness;ulong TimeRemaining;uchar CoolingMode'
#endregion Global Variables and Constants

_NTCallPowerInfo()

Func _NTCallPowerInfo()

    Local $tSPI = DllStructCreate($tagSYSTEM_POWER_INFORMATION)

    Local $Ret = DllCall('PowrProf.dll', 'int', 'CallNTPowerInformation', _
            'uint', $SystemPowerInformation, _
            'none', '', _
            'ulong', '', _
            'ptr', DllStructGetPtr($tSPI), _
            'ulong', DllStructGetSize($tSPI))

    ConsoleWrite(@LF & "===============================================================" & @LF)
    ConsoleWrite("CallNTPowerInformation(): " & _WinAPI_GetLastErrorMessage())
    ConsoleWrite(@TAB & "MaxIdlenessAllowed: " & DllStructGetData($tSPI, "MaxIdlenessAllowed"))
    ConsoleWrite(@LF & @TAB & "Idleness:    " & DllStructGetData($tSPI, "Idleness"))
    ConsoleWrite(@LF & @TAB & "TimeRemaining: " & DllStructGetData($tSPI, "TimeRemaining"))
    ConsoleWrite(@LF & @TAB & "CoolingMode: " & DllStructGetData($tSPI, "CoolingMode"))
    ConsoleWrite(@LF & "===============================================================" & @LF)

    ; or

    Local $array[4]

    For $i = 0 To 3
        $array[$i] = DllStructGetData($tSPI, $i + 1)
    Next

    _ArrayDisplay($array)
EndFunc ;==>_NTCallPowerInfo
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
targeter

'CallNTPowerInformation' doesn't return an array. It returns NTSTATUS. You have to get the data from the structure. I got this so far but the struct doesn't seem to contain any information? I dunno. Don't worry, I'm not going to post endless reams of unstructured information this time!

#region Header
#include-once
#include <WinAPI.au3>
#endregion Header

#region Global Variables and Constants
Global Const $AdministratorPowerPolicy = 9
Global Const $LastSleepTime = 15
Global Const $LastWakeTime = 14
Global Const $ProcessorInformation = 11
Global Const $ProcessorPowerPolicyAC = 18
Global Const $ProcessorPowerPolicyCurrent = 22

Global Const $ProcessrPowerPolicyDc = 19
Global Const $SystemBatteryState = 5
Global Const $SystemExecutionState = 16
Global Const $SystemPowerCapabilities = 4
Global Const $SystemPowerInformation = 12
Global Const $SystemPowerPolicyAc = 8
Global Const $SystemPowerPolicyCurrent = 8
Global Const $SystemPowerPolicyDc = 1
Global Const $SystemReserveHiberFile = 10
Global Const $VerifyProcessorPowerPolicyAc = 20
Global Const $VerifyProcessorPolicyDc = 21
Global Const $VerifySystemPowerAc = 2
Global Const $VerifySystemPolicyDc = 3
Global Const $tagSYSTEM_POWER_INFORMATION = 'ulong MaxIdlenessAllowed;ulong Idleness;ulong TimeRemaining;uchar CoolingMode'
#endregion Global Variables and Constants

_NTCallPowerInfo()

Func _NTCallPowerInfo()

    Local $tSPI = DllStructCreate($tagSYSTEM_POWER_INFORMATION)

    Local $Ret = DllCall('PowrProf.dll', 'int', 'CallNTPowerInformation', _
            'uint', $SystemPowerInformation, _
            'none', '', _
            'ulong', '', _
            'ptr', DllStructGetPtr($tSPI), _
            'ulong', DllStructGetSize($tSPI))

    ConsoleWrite(@LF & "===============================================================" & @LF)
    ConsoleWrite("CallNTPowerInformation(): " & _WinAPI_GetLastErrorMessage())
    ConsoleWrite(@TAB & "MaxIdlenessAllowed: " & DllStructGetData($tSPI, "MaxIdlenessAllowed"))
    ConsoleWrite(@LF & @TAB & "Idleness:    " & DllStructGetData($tSPI, "Idleness"))
    ConsoleWrite(@LF & @TAB & "TimeRemaining: " & DllStructGetData($tSPI, "TimeRemaining"))
    ConsoleWrite(@LF & @TAB & "CoolingMode: " & DllStructGetData($tSPI, "CoolingMode"))
    ConsoleWrite(@LF & "===============================================================" & @LF)
EndFunc ;==>_NTCallPowerInfo

---- missing up my terms again

I thought based on the Information Level it would return an struct for sys info... I mean if you use $A(Dllstructdata($systempowerinformation, 2) it works to get idleness....... For some reason the struct where cooling mode would be stored is empty......

Edited by targeter

Share this post


Link to post
Share on other sites
jaberwacky

I thought based on the Information Level it would return an array for sys info... I mean if you use $A(Dllstructdata($systempowerinformation, 2) it works to get idleness....... For some reason the array where cooling mode would be stored is empty......

It says for InformationLevel 12 that "The lpOutputBuffer buffer receives a system_power_information structure." And 'CallNTPowerInformation' returns an NTSTATUS which is just a constant. Maybe I misunderstood.

Share this post


Link to post
Share on other sites
targeter

It says for InformationLevel 12 that "The lpOutputBuffer buffer receives a system_power_information structure." And 'CallNTPowerInformation' returns an NTSTATUS which is just a constant. Maybe I misunderstood.

#include <GUIConstantsEx.au3>

Global $Idleness = 0

For $n = 1 To 100
    GetPowerInformation($Idleness)
    ConsoleWrite("TEST " & $n & ":  " & $Idleness & @LF & @LF)
    Sleep(1000)
Next

Func GetPowerInformation(ByRef $A)
;Create structure for SYSTEM_POWER_INFORMATION
    $SystemPowerInformation = DllStructCreate("long;long;long;char")
    If @error Then
        MsgBox(0, "DllStructureCreate", @error)
    EndIf

;Execute DllCall
    $Ret = DllCall("PowrProf.dll", "ptr", "CallNtPowerInformation", _
            "int", "12", _
            "ptr", "", _
            "long", "0", _
            "ptr", DllStructGetPtr($SystemPowerInformation), _
            "long", DllStructGetSize($SystemPowerInformation))
    If @error Then ConsoleWrite("Debug:  DllCall error:  " & @error & @LF)
 ;   ConsoleWrite("Debug: $Ret[0] = " & $Ret[0] & @LF)
 ;   ConsoleWrite("Debug: ULONG MaxIdlenessAllowed = " & DllStructGetData($SystemPowerInformation, 1) & @LF)
 ;   ConsoleWrite("Debug: ULONG Idleness = " & DllStructGetData($SystemPowerInformation, 2) & @LF)
 ;   ConsoleWrite("Debug: ULONG TimeRemaining = " & DllStructGetData($SystemPowerInformation, 3) & @LF)
 ;   ConsoleWrite("Debug: UCHAR CoolingMode = " & DllStructGetData($SystemPowerInformation, 4) & @LF)

    $A = DllStructGetData($SystemPowerInformation, 4);CoolingMode
    $SystemPowerInformation = 0
EndFunc  ;==>GetPowerInformation

This is the original code. I just commented the consolewrite to make it simpler. And used the coolingmode. Compare this to a the following which works. I am just plain stumped.

#include <GUIConstantsEx.au3>

Global $Idleness = 0

For $n = 1 To 100
    GetPowerInformation($Idleness)
    ConsoleWrite("TEST " & $n & ":  " & $Idleness & @LF & @LF)
    Sleep(1000)
Next

Func GetPowerInformation(ByRef $A)
;Create structure for SYSTEM_POWER_INFORMATION
    $SystemPowerInformation = DllStructCreate("long;long;long;char")
    If @error Then
        MsgBox(0, "DllStructureCreate", @error)
    EndIf

;Execute DllCall
    $Ret = DllCall("PowrProf.dll", "ptr", "CallNtPowerInformation", _
            "int", "12", _
            "ptr", "", _
            "long", "0", _
            "ptr", DllStructGetPtr($SystemPowerInformation), _
            "long", DllStructGetSize($SystemPowerInformation))
    If @error Then ConsoleWrite("Debug:  DllCall error:  " & @error & @LF)
 ;   ConsoleWrite("Debug: $Ret[0] = " & $Ret[0] & @LF)
 ;   ConsoleWrite("Debug: ULONG MaxIdlenessAllowed = " & DllStructGetData($SystemPowerInformation, 1) & @LF)
 ;   ConsoleWrite("Debug: ULONG Idleness = " & DllStructGetData($SystemPowerInformation, 2) & @LF)
 ;   ConsoleWrite("Debug: ULONG TimeRemaining = " & DllStructGetData($SystemPowerInformation, 3) & @LF)
 ;   ConsoleWrite("Debug: UCHAR CoolingMode = " & DllStructGetData($SystemPowerInformation, 4) & @LF)

    $A = DllStructGetData($SystemPowerInformation, 2);CoolingMode
    $SystemPowerInformation = 0
EndFunc  ;==>GetPowerInformation

Share this post


Link to post
Share on other sites
jaberwacky

You're onto something. Run this:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Global $Idleness = 0

For $n = 1 To 100
    GetPowerInformation($Idleness)
    ConsoleWrite("TEST " & $n & ": " & $Idleness & @LF & @LF)
    Sleep(1000)
Next

Func GetPowerInformation(ByRef $A)
    ;Create structure for SYSTEM_POWER_INFORMATION
    $SystemPowerInformation = DllStructCreate("long;long;long;char")
    
    If @error Then
    MsgBox(0, "DllStructureCreate", @error)
    EndIf

    ;Execute DllCall
    $Ret = DllCall("PowrProf.dll", "ptr", "CallNtPowerInformation", _
    "int", "12", _
    "ptr", "", _
    "long", "0", _
    "ptr", DllStructGetPtr($SystemPowerInformation), _
    "long", DllStructGetSize($SystemPowerInformation))
            
    If @error Then ConsoleWrite("Debug: DllCall error: " & @error & @LF)
    
    ConsoleWrite(@LF & "===============================================================" & @LF)
    ConsoleWrite("CallNTPowerInformation(): " & _WinAPI_GetLastErrorMessage())
    ConsoleWrite(@TAB & "MaxIdlenessAllowed: " & DllStructGetData($SystemPowerInformation, 1))
    ConsoleWrite(@LF & @TAB & "Idleness:    " & DllStructGetData($SystemPowerInformation, 2))
    ConsoleWrite(@LF & @TAB & "TimeRemaining: " & DllStructGetData($SystemPowerInformation, 3))
    ConsoleWrite(@LF & @TAB & "CoolingMode: " & DllStructGetData($SystemPowerInformation, 4))
    ConsoleWrite(@LF & "===============================================================" & @LF)
 EndFunc

I get this as a result:

===============================================================
CallNTPowerInformation(): The operation completed successfully.
    MaxIdlenessAllowed: 0
    Idleness:   97
    TimeRemaining: 1800
    CoolingMode: 
===============================================================

Well, ok, it does return an array but "CallNTPowerInformation" still puts the results into the SystemPowerInformation struct.

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
Yashied

@jaberwocky6669

UCHAR

In spite of the data type, this is not an ASCII encoded value. It is an unsigned byte.

Global Const $SystemPowerInformation = 12

Global Const $PO_TZ_ACTIVE = 0
Global Const $PO_TZ_INVALID_MODE = 2
Global Const $PO_TZ_PASSIVE = 1

Global Const $tagSYSTEM_POWER_INFORMATION = 'ulong MaxIdlenessAllowed;ulong Idleness;ulong TimeRemaining;byte CoolingMode'

Global $aData = 0

$tSPI = DllStructCreate($tagSYSTEM_POWER_INFORMATION)
$Ret = DllCall('powrprof.dll', 'ulong', 'CallNtPowerInformation', 'ulong', $SystemPowerInformation, 'ptr', 0, 'ulong', 0, 'ptr', DllStructGetPtr($tSPI), 'ulong', DllStructGetSize($tSPI))
If (Not @error) And (Not $Ret[0]) Then
    Dim $aData[4]
    For $i = 0 To 3
        $aData[$i] = DllStructGetData($tSPI, $i + 1)
    Next
EndIf

If IsArray($aData) Then
    ConsoleWrite('MaxIdlenessAllowed :' & $aData[0] & @CR)
    ConsoleWrite('Idleness           :' & $aData[1] & @CR)
    ConsoleWrite('TimeRemaining      :' & $aData[2] & @CR)
    ConsoleWrite('CoolingMode        :' & $aData[3] & @CR)
Else
    ConsoleWrite('Error!' & @CR)
EndIf

@targeter

You want to build Karlsson?

;)

Edited by Yashied

Share this post


Link to post
Share on other sites
targeter

@jaberwocky6669

UCHAR

In spite of the data type, this is not an ASCII encoded value. It is an unsigned byte.

Global Const $SystemPowerInformation = 12

Global Const $PO_TZ_ACTIVE = 0
Global Const $PO_TZ_INVALID_MODE = 2
Global Const $PO_TZ_PASSIVE = 1

Global Const $tagSYSTEM_POWER_INFORMATION = 'ulong MaxIdlenessAllowed;ulong Idleness;ulong TimeRemaining;byte CoolingMode'

Global $aData = 0

$tSPI = DllStructCreate($tagSYSTEM_POWER_INFORMATION)
$Ret = DllCall('powrprof.dll', 'ulong', 'CallNtPowerInformation', 'ulong', $SystemPowerInformation, 'ptr', 0, 'ulong', 0, 'ptr', DllStructGetPtr($tSPI), 'ulong', DllStructGetSize($tSPI))
If (Not @error) And (Not $Ret[0]) Then
    Dim $aData[4]
    For $i = 0 To 3
        $aData[$i] = DllStructGetData($tSPI, $i + 1)
    Next
EndIf

If IsArray($aData) Then
    ConsoleWrite('MaxIdlenessAllowed :' & $aData[0] & @CR)
    ConsoleWrite('Idleness           :' & $aData[1] & @CR)
    ConsoleWrite('TimeRemaining      :' & $aData[2] & @CR)
    ConsoleWrite('CoolingMode        :' & $aData[3] & @CR)
Else
    ConsoleWrite('Error!' & @CR)
EndIf

@targeter

You want to build Karlsson?

;)

JC, what is Karlsson? Is that a project code name or something?....

I'm just trying to do some api level server monitoring....

Share this post


Link to post
Share on other sites
Yashied

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.