Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Last active August 27, 2020 04:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulofreitas/7060627 to your computer and use it in GitHub Desktop.
Save paulofreitas/7060627 to your computer and use it in GitHub Desktop.
Windows processor architecture via Windows API
import ctypes
DWORD = ctypes.c_ulong
DWORD_PTR = DWORD
LPVOID = ctypes.c_void_p
WORD = ctypes.c_ushort
# MSDN info: http://msdn.microsoft.com/en-us/library/ms724958(VS.85).aspx
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():
_GetSystemInfo = ctypes.windll.kernel32.GetSystemInfo
_GetSystemInfo.argtypes = [LPSYSTEM_INFO]
_GetSystemInfo.restype = None
si = SYSTEM_INFO()
_GetSystemInfo(ctypes.byref(si))
return si
def GetNativeSystemInfo():
_GetNativeSystemInfo = ctypes.windll.kernel32.GetNativeSystemInfo
_GetNativeSystemInfo.argtypes = [LPSYSTEM_INFO]
_GetNativeSystemInfo.restype = None
si = SYSTEM_INFO()
_GetNativeSystemInfo(ctypes.byref(si))
return si
pGNSI = ctypes.windll.kernel32.GetProcAddress(
ctypes.windll.kernel32.GetModuleHandleA('kernel32'),
'GetNativeSystemInfo')
# Call GetNativeSystemInfo if supported or GetSystemInfo otherwise
if pGNSI:
si = GetNativeSystemInfo()
else:
si = GetSystemInfo()
print('Processor architecture: {}'.format(si.union.struct.wProcessorArchitecture))
'''
# 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
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment