Python uses the cTYPES module to invoke the Windows API GetVersionEx get the current system version without using Python32
Copy the Code code as follows:
#!c:/python27/python.exe
#-*-Coding:utf-8-*-
"Judging the current system version by invoking the window API"
# demonstrates calling Windows API functions via cTYPES.
# The author already knows Python32 can achieve the same function
# Add a semicolon at the end of the statement, purely personal habits
# only partial version judgment, more detailed version judging recommendation system OSVERSIONINFOEX structure
Import cTYPES;
Class OSINFO (cTYPES. Structure):
_fields_ = [
("dwOSVersionInfoSize", Ctypes.c_long),
("dwMajorVersion", Ctypes.c_long),
("dwMinorVersion", Ctypes.c_long),
("dwBuildNumber", Ctypes.c_long),
("dwPlatformId", Ctypes.c_long),
("szCSDVersion", ctypes.c_char*128)
];
Def getsystemversionstring ():
KERNEL32 = Ctypes.windll.LoadLibrary ("kernel32.dll");
OS = OSINFO ();
Os.dwosversioninfosize = ctypes.sizeof (OS);
If Kernel32. GetVersionExA (Ctypes.byref (OS)) ==0:
Return "Null Version";
If os.dwplatformid==1: #windows 95/98/me
If Os.dwmajorversion==4 and os.dwminorversion==0:
VERSTR = "Windows 95";
Elif os.dwmajorversion==4 and os.dwminorversion==10:
VERSTR = "Windows 98";
Elif os.dwmajorversion==4 and os.dwminorversion==90:
Verstr = "Windows Me";
Else
VERSTR = "Unknown version";
Elif os.dwplatformid==2: #windows vista/server 2008/server 2003/xp/2000/nt
If Os.dwmajorversion==4 and os.dwminorversion==0:
VERSTR = "Windows NT 4.0";
Elif os.dwmajorversion==5 and Os.dwminorversion==0:
VERSTR = "Windows 2000";
Elif os.dwmajorversion==5 and Os.dwminorversion==1:
VERSTR = "Windows XP";
Elif os.dwmajorversion==5 and os.dwminorversion==2:
VERSTR = "Windows 2003";
Elif os.dwmajorversion==6 and Os.dwminorversion==0:
VERSTR = "Windows Vista"; # or 2008
Elif os.dwmajorversion>=0:
VERSTR = "Windows 7";
Else
VERSTR = "Unknown version";
Else
Return "Unknown Version";
return verstr+ "Build" +str (os.dwbuildnumber) + "" +ctypes.string_at (os.szcsdversion);
if __name__ = = "__main__":
Print (getsystemversionstring ());