Python uses the cTYPES module to call WindowsAPI to get the system version example

Source: Internet
Author: User
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 ());

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.