SoFunction
Updated on 2024-12-10

Example of python using ctypes module to call windowsapi to get the system version


#!c:/python27/
#-*- coding:utf-8 -*-

"Determine the current system version by calling the Window API."
# Demonstrate calling windows api functions via ctypes.
# The author already knows that python32 is capable of the same functionality
# A semicolon at the end of a statement is a purely personal convention
# Only part of the version judgment, more detailed version judgment recommended system OSVERSIONINFOEX structure

import ctypes;

class OSINFO():
    _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 = ("");
    os = OSINFO();
    = (os);
    if ((os))==0:
        return "Null Version";
    if ==1: #windows 95/98/me
        if ==4 and ==0:
            verStr = "windows 95";
        elif ==4 and ==10:
            verStr = "windows 98";
        elif ==4 and ==90:
            verStr = "windows me";
        else:
            verStr = "unknown version";
    elif ==2: #windows vista/server 2008/server 2003/xp/2000/nt
        if ==4 and ==0:
            verStr = "windows nt 4.0";
        elif ==5 and ==0:
            verStr = "windows 2000";
        elif ==5 and ==1:
            verStr = "windows xp";
        elif ==5 and ==2:
            verStr = "windows 2003";
        elif ==6 and ==0:
            verStr = "windows vista"; # or 2008
        elif >=0:
            verStr = "windows 7";
        else:
            verStr = "unknown version";
    else:
        return "unknown Version";
    return verStr+" build"+str()+" "+ctypes.string_at();

if __name__ == "__main__":
    print(GetSystemVersionString());