Last active
April 18, 2025 18:21
-
-
Save paulofreitas/7060627 to your computer and use it in GitHub Desktop.
Windows processor architecture via Windows API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MSDN info: http://msdn.microsoft.com/en-us/library/ms724958(VS.85).aspx | |
import ctypes | |
from enum import IntEnum | |
DWORD = ctypes.c_ulong | |
DWORD_PTR = DWORD | |
LPVOID = ctypes.c_void_p | |
WORD = ctypes.c_ushort | |
class ProcessorArchitecture(IntEnum): | |
# x86 | |
PROCESSOR_ARCHITECTURE_INTEL = 0 | |
# Intel Itanium-based | |
PROCESSOR_ARCHITECTURE_IA64 = 6 | |
# AMD/Intel x64 | |
PROCESSOR_ARCHITECTURE_AMD64 = 9 | |
# x86 emulator (WOW64) | |
PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 = 10 | |
# Neutral | |
PROCESSOR_ARCHITECTURE_NEUTRAL = 11 | |
# Unknown architecture | |
PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF | |
class SYSTEM_INFO_struct(ctypes.Structure): | |
_fields_ = [ | |
('wProcessorArchitecture', WORD), | |
('wReserved', WORD) | |
] | |
class SYSTEM_INFO_union(ctypes.Union): | |
_fields_ = [ | |
('dwOemId', DWORD), | |
('struct', SYSTEM_INFO_struct) | |
] | |
class SYSTEM_INFO(ctypes.Structure): | |
_fields_ = [ | |
('union', SYSTEM_INFO_union), | |
('dwPageSize', DWORD), | |
('lpMinimumApplicationAddress', LPVOID), | |
('lpMaximumApplicationAddress', LPVOID), | |
('dwActiveProcessorMask', DWORD_PTR), | |
('dwNumberOfProcessors', DWORD), | |
('dwProcessorType', DWORD), | |
('dwAllocationGranularity', DWORD), | |
('wProcessorLevel', WORD), | |
('wProcessorRevision', WORD) | |
] | |
LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO) | |
def GetSystemInfo(): | |
# Call GetNativeSystemInfo if supported or GetSystemInfo otherwise | |
SystemInfo = ctypes.windll.kernel32.GetSystemInfo | |
if ctypes.windll.kernel32.GetProcAddress( | |
ctypes.windll.kernel32.GetModuleHandleA('kernel32'), | |
'GetNativeSystemInfo', | |
): | |
SystemInfo = ctypes.windll.kernel32.GetNativeSystemInfo | |
SystemInfo.argtypes = [LPSYSTEM_INFO] | |
SystemInfo.restype = None | |
si = SYSTEM_INFO() | |
SystemInfo(ctypes.byref(si)) | |
return si | |
si = GetSystemInfo() | |
print('union.dwOemId', si.union.dwOemId) | |
print('union.struct.wProcessorArchitecture', si.union.struct.wProcessorArchitecture) | |
print('union.struct.wReserved', si.union.struct.wReserved) | |
print('dwPageSize', si.dwPageSize) | |
print('lpMinimumApplicationAddress', si.lpMinimumApplicationAddress) | |
print('lpMaximumApplicationAddress', si.lpMaximumApplicationAddress) | |
print('dwActiveProcessorMask', si.dwActiveProcessorMask) | |
print('dwNumberOfProcessors', si.dwNumberOfProcessors) | |
print('dwProcessorType', si.dwProcessorType) | |
print('dwAllocationGranularity', si.dwAllocationGranularity) | |
print('wProcessorLevel', si.wProcessorLevel) | |
print('wProcessorRevision', si.wProcessorRevision) | |
print() | |
print('Processor architecture: {}'.format({ | |
ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64: 'AMD/Intel x64', | |
ProcessorArchitecture.PROCESSOR_ARCHITECTURE_IA32_ON_WIN64: 'x86 emulator (WOW64)', | |
ProcessorArchitecture.PROCESSOR_ARCHITECTURE_IA64: 'Intel Itanium-based', | |
ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL: 'x86', | |
ProcessorArchitecture.PROCESSOR_ARCHITECTURE_NEUTRAL: 'Neutral', | |
ProcessorArchitecture.PROCESSOR_ARCHITECTURE_UNKNOWN: 'Unknown', | |
}.get(si.union.struct.wProcessorArchitecture))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment