Descripci贸n

Esta receta es un ejemplo de c贸mo utilizar funciones nativas de las bibliotecas de Windows desde Python (usando ctypes).

Por ejemplo, en este caso se usa para consultar el estado de la bater铆a (carga en Wh y tiempos estimados, consumo de energ铆a en Watts, etc.) usando la funci贸n CallNtPowerInformation de la biblioteca de windows PowrProf.lib (PowrProf.dll). Tambi茅n se ilustra c贸mo usar estructuras de C en python para llamar a funciones externas.

Es necesario tener una versi贸n de Python con ctypes y estar sobre windows.

Ejemplo:

El ejemplo en Python (winbatt.py) registra un objeto python MiMiniInterpretePython, exponiendo:

  • carga la biblioteca 麓powrprof.dll麓

  • define la constante SystemBatteryState (InformationLevel) de POWER_INFORMATION_LEVEL

  • define la estructura SYSTEM_BATTERY_STATE

  • inicializa la estructura en blanco

  • llama a la funci贸n CallNtPowerInformation

  • imprime los resultados

Archivo winbatt.py

# -*- coding: iso-8859-1 -*-

import ctypes

l = ctypes.cdll.LoadLibrary("powrprof")

SystemBatteryState = 5  # POWER_INFORMATION_LEVEL enum

class SYSTEM_BATTERY_STATE(ctypes.Structure):
    _fields_ = [
                ("AcOnLine", ctypes.c_bool),
                ("BatteryPresent", ctypes.c_bool),
                ("Charging", ctypes.c_bool),
                ("Discharging", ctypes.c_bool),
                ("Spare1", ctypes.c_bool * 4),
                ("MaxCapacity", ctypes.c_long),
                ("RemainingCapacity", ctypes.c_long),
                ("Rate", ctypes.c_long),
                ("EstimatedTime", ctypes.c_long),
                ("DefaultAlert1", ctypes.c_long),
                ("DefaultAlert2", ctypes.c_long),
                ]


sb = SYSTEM_BATTERY_STATE(0)
retval = l.CallNtPowerInformation(SystemBatteryState,
                                  None, 0,
                                  ctypes.addressof(sb), ctypes.sizeof(sb))
assert retval == 0  # debe devolver 0 si no hay error
print "AcOnLine:", sb.AcOnLine
print "Charging:", sb.Charging
print "Discharging:", sb.Discharging
print "Capacity:", sb.MaxCapacity, "mWh max", sb.RemainingCapacity, "mWh remaining",
print sb.RemainingCapacity*100/sb.MaxCapacity, "%"
print "Rate:", sb.Rate / 1000.0, "W"
print "Estimated Time:", sb.EstimatedTime / 3600, "h", sb.EstimatedTime / 60 % 60, "min"

Para usarlo se puede ejecutar desde l铆nea de comando:

C:\src>python winbatt.py
AcOnLine: False
Charging: False
Discharging: True
Capacity: 45140 mWh max 5417 mWh remaining 12 %
Rate: -9.435 W
Estimated Time: 0 h 34 min

Para Descargar Fuentes:

Autor / Autores:

MarianoReingart