如何在Python中调用GetLogicalProcessorInformation()?

如何在Python中调用GetLogicalProcessorInformation()?,python,windows,ctypes,Python,Windows,Ctypes,我试图在Python中调用GetLogicalProcessorInformation进程,但不确定如何设置结构。我用GlobalMemoryStatusEx完成了它,代码如下 from ctypes import Structure, c_int32, c_uint64, sizeof, byref, windll class MemoryStatusEx(Structure): _fields_ = [ ("length", c_int32), (&qu

我试图在Python中调用GetLogicalProcessorInformation进程,但不确定如何设置结构。我用GlobalMemoryStatusEx完成了它,代码如下

from ctypes import Structure, c_int32, c_uint64, sizeof, byref, windll

class MemoryStatusEx(Structure):
_fields_ = [
    ("length", c_int32),
    ("memory_load", c_int32),
    ("total_physical", c_uint64),
    ("available_physical", c_uint64),
    ("total_page_file", c_uint64),
    ("available_page_file", c_uint64),
    ("total_virtual", c_uint64),
    ("available_virtual", c_uint64),
    ("available_extended_virtual", c_uint64)
]

def __init__(self):
    self.length = sizeof(self)
    assert windll.kernel32.GlobalMemoryStatusEx(byref(self))

正如我所说,我不知道如何在Python中实现它,因为它使用了UNION和LPVOID。非常感谢您的帮助。

Windows 10上的测试示例Python 3.8 64位:

from ctypes import *
from ctypes import wintypes as w

ULONG_PTR = c_size_t  # c_ulong on Win32, c_ulonglong on Win64.
ULONGLONG = c_ulonglong

ERROR_INSUFFICIENT_BUFFER = 122

class CACHE_DESCRIPTOR(Structure):
    _fields_ = (('Level',w.BYTE),
                ('Associativity',w.BYTE),
                ('LineSize',w.WORD),
                ('Size',w.DWORD),
                ('Type',c_int))

class ProcessorCore(Structure):
    _fields_ = ('Flags', w.BYTE),

class NumaNode(Structure):
    _fields_ = ('NodeNumber', w.DWORD),

class DUMMYUNIONNAME(Union):
    _fields_ = (('ProcessorCore',ProcessorCore),
                ('NumaNode',NumaNode),
                ('Cache',CACHE_DESCRIPTOR),
                ('Reserved',ULONGLONG * 2))

class SYSTEM_LOGICAL_PROCESSOR_INFORMATION(Structure):
    _anonymous_ = 'DUMMYUNIONNAME',
    _fields_ = (('ProcessorMask', ULONG_PTR),
                ('Relationship', c_int),
                ('DUMMYUNIONNAME',DUMMYUNIONNAME))

dll = WinDLL('kernel32',use_last_error=True)
dll.GetLogicalProcessorInformation.argtypes = POINTER(SYSTEM_LOGICAL_PROCESSOR_INFORMATION),w.LPDWORD
dll.GetLogicalProcessorInformation.restype = w.BOOL

# wrapper for easier use
def GetLogicalProcessorInformation():
    bytelength = w.DWORD()
    structlength = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)
    # Call with null buffer to get required buffer size
    result = dll.GetLogicalProcessorInformation(None,byref(bytelength))
    if (err := get_last_error()) != ERROR_INSUFFICIENT_BUFFER:
        raise WinError(err)
    no_of_structures = bytelength.value // structlength
    arr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION * no_of_structures)()
    result = dll.GetLogicalProcessorInformation(arr,byref(bytelength))
    if not result:
        raise WinError(get_last_error())
    return arr

infos = GetLogicalProcessorInformation()
print(f'nodes = {len(infos)}')
for info in infos:
    print(f'{info.ProcessorMask:8b} {info.Relationship} L{info.Cache.Level} {info.Cache.Size}')
节点=19
110l10
11 2 L1 32768
11 2 L1 32768
11 2 L2 262144
1100 0 L1 0
1100 2 L1 32768
1100 2 L1 32768
1100 2 L2 262144
110000 L1 0
110000 2 L1 32768
110000 2 L1 32768
110000 2 L2 262144
11111111 3 L0 0
11000000 L1 0
11000000 2 L1 32768
11000000 2 L1 32768
11000000 2 L2 262144
11111111 2 L3 8388608
11111111l0