Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 64-bit value from WMI in python

Tags:

python

wmi

When I run the following code:

def get_process_info(pid):
    c = wmi.WMI(namespace='root\\cimv2')
    obj = c.Win32_Process(ProcessId = pid)[0]
    print "VirtualSize:", obj.wmi_property('VirtualSize').type
    print "VirtualSize:", obj.wmi_property('VirtualSize').Value

def get_perf_info(pid):
    c = wmi.WMI(namespace='root\\cimv2')
    obj = c.Win32_PerfFormattedData_PerfProc_Process(IDProcess = pid)[0]
    print "PrivateBytes:", obj.wmi_property('PrivateBytes').type
    print "PrivateBytes:", obj.wmi_property('PrivateBytes').Value

Against a process which is using a lot of memory I get this:

VirtualSize: uint64
VirtualSize: 5015498752
PrivateBytes: uint64
PrivateBytes: 4294967295

Note that both are listed as being 64-bit values but the PrivateBytes values is 0xFFFFFFFF. If I use "WMI Explorer" I can see the PrivateBytes value is larger than 32-bits: wmi image

My question is how can I access PrivateBytes in its full 64-bit glory?

Is there a completely other way to read the WMI from python besides this WMI module?

like image 398
Philip Avatar asked Aug 17 '26 13:08

Philip


1 Answers

You can use wmic which provides a command-line interface for WMI :

def find_privatebytes(pid):
    with os.popen('wmic process list full /format:csv') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        crow = 0
        col_pid = 0
        col_pb  = 0

        for row in reader:
            if len(row) == 0:
                continue
            if crow == 0:                
                col_pid = row.index("ProcessId")
                col_pb  = row.index("PrivatePageCount")
                crow += 1
            elif int(row[col_pid]) == pid:
                return int(row[col_pb])
        return 0
like image 134
napuzba Avatar answered Aug 20 '26 03:08

napuzba