2

I know how to use wmi, I have used it before, however, the wmi class it seems i need to call is GetSystemPowerStatus. but i am having trouble finding and documentation on it. to be able to access it, i need to know the namespace, and the format of the data inside the class. could someone help me? Also some sample code would be nice.

ryan27968
  • 437
  • 3
  • 7
  • 14

2 Answers2

7

Using ctypes, you can call win32 api:

from ctypes import *

class PowerClass(Structure):
    _fields_ = [('ACLineStatus', c_byte),
            ('BatteryFlag', c_byte),
            ('BatteryLifePercent', c_byte),
            ('Reserved1',c_byte),
            ('BatteryLifeTime',c_ulong),
            ('BatteryFullLifeTime',c_ulong)]    

powerclass = PowerClass()
result = windll.kernel32.GetSystemPowerStatus(byref(powerclass))
print(powerclass.BatteryLifePercent)

Above code comes from here.


Using Win32_Battery class (You need to install pywin32):

from win32com.client import GetObject

WMI = GetObject('winmgmts:')
for battery in WMI.InstancesOf('Win32_Battery'):
    print(battery.EstimatedChargeRemaining)

Alternative that use wmi package:

import wmi

w = wmi.WMI()
for battery in w.query('select * from Win32_Battery'):
    print battery.EstimatedChargeRemaining
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • can i not do this with wmi? hat would be much simpler. some of this code is just going over my head and i want to understand the code i am using not just use it without knowing what it is. – ryan27968 Jan 13 '14 at 04:08
  • @user1803425, `GetSystemPowerStatus` is win32 api. You can't call that using WMI. – falsetru Jan 13 '14 at 04:18
  • @user1803425, I updated the answer to include the code that use WMI. You need to install [pywin32](http://sourceforge.net/projects/pywin32/). – falsetru Jan 13 '14 at 04:23
  • and how would i go about getting the battery level in percentage? EDIT:: i figured it out. i just changed the battery.BatteryStatus to battery.EstimatedChargeRemaining and removed the ststus map. – ryan27968 Jan 13 '14 at 04:30
  • @user1803425, I updated the answer code according to your comment. Thank you. – falsetru Jan 13 '14 at 04:37
  • the problem i have with the way you have done that is that it is too flipping slow. it slows down my program to the point that it does not do its job effectively. – ryan27968 Jan 13 '14 at 04:52
  • @user1803425, I added another alternative solution. `ctypes` version run fastest in my machine. (Windows 7 64bit + Python 2.7.6) (ctypes: 0.00500011444092s, win32com.client: 0.266000032425s, wmi: 0.274999856949s) – falsetru Jan 13 '14 at 04:56
  • the ctypes does indeed run fastest, but it crashes my program for some reason. can't understand why. – ryan27968 Jan 13 '14 at 05:21
  • you can be pretty darn idiotic when coding. all the problems i had with ctype was just because i was passing it to a function which only accepted strings not ints. i added str() and it now works like a dream. thanks for the help. – ryan27968 Jan 13 '14 at 05:35
0

```

import subprocess

wmic =  subprocess.getoutput("wmic path win32_battery get EstimatedChargeRemaining")
print(wmic)

```

output:

EstimatedChargeRemaining  

96               
python必须死
  • 999
  • 10
  • 12