Jump to content

ReadGlobalPwrPolicy


Recommended Posts

Hi,

I am having issues with defining structures for ReadGlobalPwrPolicy API function, namely GLOBAL_POWER_POLICY.

From MSDN

typedef struct _GLOBAL_POWER_POLICY { GLOBAL_USER_POWER_POLICY user; GLOBAL_MACHINE_POWER_POLICY mach;

} GLOBAL_POWER_POLICY, *PGLOBAL_POWER_POLICY;

My question is how to define structure when it contains substructures.

My Code

#include<Security.au3>

#include<StructureConstants.au3>

#include<WinAPI.au3>

#include<Constants.au3>

#include <Array.au3>

tag_GLOBAL_USER_POWER_POLICY = "ulong Revision;byte PowerButtonAc[3];byte PowerButtonDc[3];byte SleepButtonAc[3];byte SleepButtonDc[3];byte LidCloseAc[3];byte LidCloseDc[3];byte DischargePolicy[16];ulong GlobalFlags"

$GLOBAL_USER_POWER_POLICY_Struct = DllStructCreate($tag_GLOBAL_USER_POWER_POLICY)

;DllStructSetData($GLOBAL_USER_POWER_POLICY_Struct,"Revision","14")

$GLOBAL_USER_POWER_POLICY = DllStructGetPtr($GLOBAL_USER_POWER_POLICY_Struct)

$tagGLOBAL_MACHINE_POWER_POLICY = "ulong Revision;dword LidOpenWakeAc[8];dword LidOpenWakeDc[8];ulong BroadcastCapacityResolution"

$GLOBAL_MACHINE_POWER_POLICY_Struct = DllStructCreate($tagGLOBAL_MACHINE_POWER_POLICY)

$GLOBAL_POWER_POLICY_Struct = DllStructCreate($tag_GLOBAL_USER_POWER_POLICY &";"& $tagGLOBAL_MACHINE_POWER_POLICY)

$GLOBAL_POWER_POLICY = DllStructGetPtr($GLOBAL_POWER_POLICY_Struct)

ReadGlobalPwrPolicy($GLOBAL_POWER_POLICY)

Func ReadGlobalPwrPolicy($pGlobalPowerPolicy)

$result = DllCall("PowrProf.dll", "int", "ReadGlobalPwrPolicy","ptr*",$pGlobalPowerPolicy)

If @error Then

MsgBox(0, "", "@ERROR: " & @error & @CRLF & "@EXTENDED: " & @extended)

Else

$WinAPI_Error = _WinAPI_GetLastError ()

If $WinAPI_Error <> 0 Then

MsgBox(0, "ReadGlobalPwrPolicy", "Error " & _WinAPI_GetLastError () & @CRLF & _WinAPI_GetLastErrorMessage ()) ;Error 13: The data is invalid.

Else

Return $result[0]

EndIf

EndIf

EndFunc

I get errror : The parameter is incorrect - which I belive is caused by incorrect strucure definition

Any help on that matter is appreciated

Link to comment
Share on other sites

first, you have to use "ptr", not "ptr*". Then the structure has loads of substructures which you sized wrong. The easiest way is create a byte-placeholder as you did, but use _SIZEOF :)

#include<Security.au3>
#include<StructureConstants.au3>
#include<WinAPI.au3>
#include<Constants.au3>
#include <Array.au3>

Func _SIZEOF($struct)
    Return DllStructGetSize(DllStructCreate($struct,1))
EndFunc

$tagPOWER_ACTION_POLICY = "dword Action; ULONG Flags; ULONG EventCode;"

$NUM_DISCHARGE_POLICIES = 4 

$tagSYSTEM_POWER_LEVEL = "int Enable;CHAR Spare[3]; ULONG BatteryLevel;byte PowerPolicy["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];dword MinSystemState;"

; create tag with substructs as byte-placeholders
$tag_GLOBAL_USER_POWER_POLICY = "ulong Revision;byte PowerButtonAc["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];byte PowerButtonDc["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];" & _ 
    "byte SleepButtonAc["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];byte SleepButtonDc["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];byte LidCloseAc["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];"& _ 
    "byte LidCloseDc["&_SIZEOF($tagPOWER_ACTION_POLICY)&"];byte DischargePolicy["&(_SIZEOF($tagSYSTEM_POWER_LEVEL)*$NUM_DISCHARGE_POLICIES)&"];ulong GlobalFlags;"

$tagGLOBAL_MACHINE_POWER_POLICY = "ulong Revision;dword LidOpenWakeAc;dword LidOpenWakeDc;ulong BroadcastCapacityResolution;"

$tagGLOBAL_POWER_POLICY = $tag_GLOBAL_USER_POWER_POLICY & $tagGLOBAL_MACHINE_POWER_POLICY

$GLOBAL_POWER_POLICY_Struct = DllStructCreate($tagGLOBAL_POWER_POLICY)
$GLOBAL_POWER_POLICY = DllStructGetPtr($GLOBAL_POWER_POLICY_Struct)

ReadGlobalPwrPolicy($GLOBAL_POWER_POLICY)

; Read Data from substruct created as byte placeholders
$PowerButtonAc = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"PowerButtonAc"))
$PowerButtonDc = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"PowerButtonDc"))
$SleepButtonAc = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"SleepButtonAc"))
$SleepButtonDc = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"SleepButtonDc"))
$LidCloseAc = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"LidCloseAc"))
$LidCloseDc = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"LidCloseDc"))


$DischargePolicy1 = DllStructCreate($tagSYSTEM_POWER_LEVEL,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"DischargePolicy"))
$PowerPolicy1 = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($DischargePolicy1,"PowerPolicy"))

$DischargePolicy2 = DllStructCreate($tagSYSTEM_POWER_LEVEL,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"DischargePolicy")+_SIZEOF($tagSYSTEM_POWER_LEVEL))
$PowerPolicy2 = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($DischargePolicy2,"PowerPolicy"))

$DischargePolicy3 = DllStructCreate($tagSYSTEM_POWER_LEVEL,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"DischargePolicy")+2*_SIZEOF($tagSYSTEM_POWER_LEVEL))
$PowerPolicy3 = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($DischargePolicy3,"PowerPolicy"))

$DischargePolicy4 = DllStructCreate($tagSYSTEM_POWER_LEVEL,DllStructGetPtr($GLOBAL_POWER_POLICY_Struct,"DischargePolicy")+3*_SIZEOF($tagSYSTEM_POWER_LEVEL))
$PowerPolicy4 = DllStructCreate($tagPOWER_ACTION_POLICY,DllStructGetPtr($DischargePolicy4,"PowerPolicy"))



Func ReadGlobalPwrPolicy($pGlobalPowerPolicy)

    Local $result = DllCall("PowrProf.dll", "int", "ReadGlobalPwrPolicy","ptr",$pGlobalPowerPolicy)
    If @error Then
        MsgBox(0, "", "@ERROR: " & @error & @CRLF & "@EXTENDED: " & @extended)
    Else
        $WinAPI_Error = _WinAPI_GetLastError ()
        If $WinAPI_Error <> 0 Then
            MsgBox(0, "ReadGlobalPwrPolicy", "Error " & _WinAPI_GetLastError () & @CRLF & _WinAPI_GetLastErrorMessage ()) ;Error 13: The data is invalid.
        Else
            Return $result[0]
        EndIf
    EndIf
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Thank you very much!! It works !!!

BTW may ask about one thing . You wrote that I should have used ptr instead of ptr*. My understanding of using * is whenever structure needs to be filled. MSDN marks structure memeber as OUT. I see now that I do not understand it properly. If you could explain that would be great.

Link to comment
Share on other sites

_out just means, that this parameter will be changed by the function.

* (by reference) is just needed, if you want to receive a changed value in a non-structure parameter, or if the structure will be created from the function.

if you have to use *, you can see this way:

- PMYSTRUCT *structptr

- LPMYSTRUCT *structptr

- always, when there is a standard-datatype with a *

You don't use it for e.g.:

-MYSTRUCT *struct

- PMYSTRUCT structptr

- LPMYSTRUCT structptr

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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...