python使用ctypes模块调用windowsapi获取系统版本示例
#!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());
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
上一篇: 如何在属于另一个类实例的类实例中调用他外面类的函数
下一篇: PHP判断客户端是否为手机移动设备
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论