Re: Enumeration with callback fails [SEC=UNOFFICIAL]

Radek Holý <[email protected]> Sat, 11 Aug 2012 12:45:04 +0200
Newsgroups gmane.comp.python.ctypes
Message-ID <CAJ9iWDmuJ6UG1drqtzdyrD6Hy5twkhfJCPfzrJVET4k1VQ40hQ@mail.gmail.com>
Oh, I understand. It works. Thank you very much!

-- 
Radek Holý
Czech republic



2012/8/10 Andrew MacIntyre <[email protected]>:
> You need to correctly specify the calling convention - when calling Windows API functions, the callback function should normally be WINFUNCTYPE (i.e stdcall) rather than CFUNCTYPE (ie. cdecl).
>
> -------------------------> "These thoughts are mine alone!" <---------
> Andrew MacIntyre           Operations Branch
> tel:   +61 2 6219 5356     Communications Infrastructure Division
> fax:   +61 2 6253 3277     Australian Communications & Media Authority
> email: [email protected]            http://www.acma.gov.au/
>
>
>> -----Original Message-----
>> From: Radek Holý [mailto:[email protected]]
>> Sent: Friday, 10 August 2012 3:02 AM
>> To: [email protected]
>> Subject: [ctypes-users] Enumeration with callback fails
>>
>> 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/
>> _______________________________________________
>> ctypes-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/ctypes-users
>
> NOTICE: This email message is for the sole use of the intended recipient(s)
>  and may contain confidential and privileged information. Any unauthorized
>  review, use, disclosure or distribution is prohibited. If you are not the
>  intended recipient, please contact the sender by reply email and destroy all
>  copies of the original message.
>
> ------------------------------------------------------------------------------
> 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/
> _______________________________________________
> ctypes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/ctypes-users

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