解释:我正在尝试获取函数GetVersionEx的结果,但我一直在输出中得到'0':
class op_info(Structure):
_fields_ = [
('dwOSVersionInfoSize', DWORD),
('dwMajorVersion', DWORD),
('dwMinorVersion', DWORD),
('dwBuildNumber', DWORD),
('dwPlatformId', DWORD),
('szCSDVersion', DWORD)
]
def info():
op = op_info()
ctypes.windll.Kernel32.GetVersion(byref(op))
return op.dwMajorVersion
print info()
问题:如何使此功能正常运行并获得真实效果?
编辑:@eryksun我在调用GetVersionExW函数之前就这样做了:
import sys
op.dwOSVersionInfoSize = sys.getsizeof(op_info)
,输出为:452
但是info()的最终结果仍为0
答案 0 :(得分:3)
以下是调用GetVersionExW
来填写OSVERSIONINFO
或OSVERSIONINFOEX
记录的示例设置。
import ctypes
from ctypes.wintypes import BYTE, WORD, DWORD, WCHAR
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
class OSVERSIONINFO(ctypes.Structure):
_fields_ = (('dwOSVersionInfoSize', DWORD),
('dwMajorVersion', DWORD),
('dwMinorVersion', DWORD),
('dwBuildNumber', DWORD),
('dwPlatformId', DWORD),
('szCSDVersion', WCHAR * 128))
def __init__(self, *args, **kwds):
super(OSVERSIONINFO, self).__init__(*args, **kwds)
self.dwOSVersionInfoSize = ctypes.sizeof(self)
kernel32.GetVersionExW(ctypes.byref(self))
class OSVERSIONINFOEX(OSVERSIONINFO):
_fields_ = (('wServicePackMajor', WORD),
('wServicePackMinor', WORD),
('wSuiteMask', WORD),
('wProductType', BYTE),
('wReserved', BYTE))
def errcheck_bool(result, func, args):
if not result:
raise ctypes.WinError(ctypes.get_last_error())
return args
kernel32.GetVersionExW.errcheck = errcheck_bool
kernel32.GetVersionExW.argtypes = (ctypes.POINTER(OSVERSIONINFO),)
def get_os_major_version():
info = OSVERSIONINFO()
return info.dwMajorVersion
if __name__ == '__main__':
print get_os_major_version()