Enumeration with callback fails
Radek Holý <radekholypublic <at> gmail.com>
2012-08-09 17:02:04 GMT
Hello,
can you please advise why the following call correctly enumerates all
schemes and then fails?
>>> import ctypes
>>> import ctypes.wintypes
>>> callbacktype = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_uint, ctypes.wintypes.DWORD,
ctypes.wintypes.LPWSTR, ctypes.wintypes.DWORD, ctypes.wintypes.LPWSTR,
ctypes.POINTER(PowerPolicy), ctypes.wintypes.LPARAM)
>>>
>>> def callback(*args):
... print(args)
... return True
...
>>> callbackinstance = callbacktype(callback)
>>> ctypes.windll.powrprof.EnumPwrSchemes(callbackinstance, 0)
(0, 34, 'Home/Office Desk', 190, 'This scheme is suited to most home
or desktop computers that are left plugged in all the time.',
<__main__.LP_PowerPolicy object at 0x01A84F80>, 0)
(1, 32, 'Portable/Laptop', 172, 'This scheme is designed for extended
battery life for portable computers on the road.',
<__main__.LP_PowerPolicy object at 0x01A84F80>, 0)
(2, 26, 'Presentation', 116, 'This scheme keeps the monitor on for
doing presentations.', <__main__.LP_PowerPolicy object at 0x01A84F80>,
0)
(3, 20, 'Always On', 290, 'This scheme keeps the computer running so
that it can be accessed from the network. Use this scheme if you do
not have network wakeup hardware.', <__main__.LP_PowerPolicy object at
0x01A84F80>, 0)
(4, 50, 'Minimal Power Management', 146, 'This scheme keeps the
computer on and optimizes it for high performance.',
<__main__.LP_PowerPolicy object at 0x01A84F80>, 0)
(5, 24, 'Max Battery', 108, 'This scheme is extremely aggressive for
saving power.', <__main__.LP_PowerPolicy object at 0x01A84F80>, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with not enough arguments (2226876 bytes m
issing)
I tried different return values of the callback function.
Thus I have declared the used structures.
>>> class PowerActionPolicy(ctypes.Structure):
... _fields_ = [("Action", ctypes.c_int),
... ("Flags", ctypes.c_ulong),
... ("EventCode", ctypes.c_ulong)]
...
>>> class UserPowerPolicy(ctypes.Structure):
... _fields_ = [("Revision", ctypes.c_ulong),
... ("IdleAc", PowerActionPolicy),
... ("IdleDc", PowerActionPolicy),
... ("IdleTimeoutAc", ctypes.c_ulong),
... ("IdleTimeoutDc", ctypes.c_ulong),
... ("IdleSensitivityAc", ctypes.c_char),
... ("IdleSensitivityDc", ctypes.c_char),
... ("ThrottlePolicyAc", ctypes.c_char),
... ("ThrottlePolicyDc", ctypes.c_char),
... ("MaxSleepAc", ctypes.c_int),
... ("MaxSleepDc", ctypes.c_int),
... ("Reserved", ctypes.c_ulong * 2),
... ("VideoTimeoutAc", ctypes.c_ulong),
... ("VideoTimeoutDc", ctypes.c_ulong),
... ("SpindownTimeoutAc", ctypes.c_ulong),
... ("SpindownTimeoutDc", ctypes.c_ulong),
... ("OptimizeForPowerAc", ctypes.c_bool),
... ("OptimizeForPowerDc", ctypes.c_bool),
... ("FanThrottleToleranceAc", ctypes.c_char),
... ("FanThrottleToleranceDc", ctypes.c_char),
... ("ForcedThrottleAc", ctypes.c_char),
... ("ForcedThrottleDc", ctypes.c_char)]
...
>>> class MachinePowerPolicy(ctypes.Structure):
... _fields_ = [("Revision", ctypes.c_ulong),
... ("MinSleepAc", ctypes.c_int),
... ("MinSleepDc", ctypes.c_int),
... ("ReducedLatencySleepAc", ctypes.c_int),
... ("ReducedLatencySleepDc", ctypes.c_int),
... ("DozeTimeoutAc", ctypes.c_ulong),
... ("DozeTimeoutDc", ctypes.c_ulong),
... ("DozeS4TimeoutAc", ctypes.c_ulong),
... ("DozeS4TimeoutDc", ctypes.c_ulong),
... ("MinThrottleAc", ctypes.c_char),
... ("MinThrottleDc", ctypes.c_char),
... ("pad1", ctypes.c_char * 2),
... ("OverThrottledAc", PowerActionPolicy),
... ("OverThrottledDc", PowerActionPolicy)]
...
>>> class PowerPolicy(ctypes.Structure):
... _fields_ = [("user", UserPowerPolicy),
... ("mach", MachinePowerPolicy)]
...
Thank you very much
--
--
Radek Holý
Czech republic
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/