博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]python使用ctypes模块调用windowsapi获取系统版本
阅读量:5119 次
发布时间:2019-06-13

本文共 2674 字,大约阅读时间需要 8 分钟。

#coding: utf-8import win32uiimport win32guiimport win32conimport win32api#https://mail.python.org/pipermail/python-win32/2009-April/009078.html'''ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)large, small = win32gui.ExtractIconEx("F:\\nginx\\nginx-1.13.0\\nginx.exe",0)win32gui.DestroyIcon(large[0])hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) )hbmp = win32ui.CreateBitmap()hbmp.CreateCompatibleBitmap( hdc, ico_x, ico_x )hdc = hdc.CreateCompatibleDC()hdc.SelectObject( hbmp )hdc.DrawIcon( (0,0), small[0] )hbmp.SaveBitmapFile( hdc, "save.bmp" )print(large)print(small)'''"通过调用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)+" "+str(ctypes.string_at(os.szCSDVersion)) if __name__ == "__main__":    print(GetSystemVersionString())

https://www.oschina.net/code/snippet_157181_13215

转载于:https://www.cnblogs.com/tinyos/p/7296665.html

你可能感兴趣的文章
Wpf 之Canvas介绍
查看>>
Java工程师学习指南 入门篇
查看>>
linux history
查看>>
rpm软件包类型
查看>>
除去内容中的空格与换行
查看>>
jQuery on(),live(),trigger()
查看>>
卡尔曼滤波的原理说明
查看>>
对Kalman(卡尔曼)滤波器的理解@@zz
查看>>
局部敏感哈希(Locality-Sensitive Hashing, LSH)
查看>>
Python2.7 urlparse
查看>>
sencha touch在华为emotion ui 2.0自带浏览器中圆角溢出的bug
查看>>
[WinAPI] API 2 [MessageBox API][消息框API]
查看>>
BZOJ 1264 动态规划 + 树状数组
查看>>
[BZOJ5248] 2018九省联考 D1T1 一双木棋 | 博弈论 状压DP
查看>>
super 小记
查看>>
C语言实现<读取>和<写入> *.ini文件(转)
查看>>
【架构】Linux的架构(architecture)
查看>>
从解决Cocos2dx-2.x arm64 Crash 来看C的奇技淫巧
查看>>
ASM 图解
查看>>
Django之数据库连表操作
查看>>