python调用windowsapi python使用ctypes模块调用windowsapi获取系统版本示例(python调用windows api)

网友投稿 414 2022-06-06


想了解python使用ctypes模块调用windowsapi获取系统版本示例的相关内容吗,在本文为您仔细讲解python调用windowsapi的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:ctypes模块,调用windowsapi,获取系统版本,下面大家一起来学习吧。

python使用ctypes模块调用windows api GetVersionEx获取当前系统版本,没有使用python32

复制代码 代码如下:

#!c:/python27/python.exe
#-*- coding:utf-8 -*-

"通过调用Window API判断当前系统版本"
# 演示通过ctypes调用windows api函数.
# 作者已经知道python32能够实现相同功能
# 语句末尾加分号,纯属个人习惯
# 仅作部分版本判断,更详细的版本判断推荐系统OSVERSIONINFOEX结构体

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());



版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python调用windows api python调用windows api锁定计算机示例(python调用windows任务计划)
下一篇:window所有进程 window调用api列出当前所有进程示例(windows进程管理)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~