在 Python 中检测 Windows 8.1? [英] Detect Windows 8.1 in Python?

查看:32
本文介绍了在 Python 中检测 Windows 8.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个脚本,它使用平台模块来检测我们各种客户端的操作系统版本.

查看platform.py 的源代码,我可以看到在Windows 系统上,它使用的是sys.getwindowsverion().不幸的是,在 Windows 8.1 系统上,该特定功能报告:

<预><代码>>>>sys.getwindowsversion()sys.getwindowsversion(major=6,minor=2,build=9200,platform=2,service_pack='')

Windows 8.1 是 6.3.9600:

Microsoft Windows [版本 6.3.9600](c) 2013 年微软公司.版权所有.C:\Windows\system32>verMicrosoft Windows [版本 6.3.9600]

所以,我意识到我可以围绕我对 platform.release() 的调用编写一些额外的逻辑,如果返回 8,则进行二次检查并尝试运行 ver,但这似乎有点错综复杂.

有人知道更好的方法吗?

运行 ActivePython 2.7.2.5 以防万一...

解决方案

Microsoft 更改了版本函数的行为方式.请参阅此页面了解更多信息.

我解决这个问题的方法是使用 ctypes 和内核模式函数,RtlGetVersion.尽管这是一个内核模式函数,但它可以从用户模式调用就好了.我已经在许多版本的 Windows 上尝试过这个,但没有出现问题.

import ctypes类 OSVERSIONINFOEXW(ctypes.Structure):_fields_ = [('dwOSVersionInfoSize', ctypes.c_ulong),('dwMajorVersion', ctypes.c_ulong),('dwMinorVersion', ctypes.c_ulong),('dwBuildNumber', ctypes.c_ulong),('dwPlatformId', ctypes.c_ulong),('szCSDVersion', ctypes.c_wchar*128),('wServicePackMajor', ctypes.c_ushort),('wServicePackMinor', ctypes.c_ushort),('wSuiteMask', ctypes.c_ushort),('wProductType', ctypes.c_byte),('wReserved', ctypes.c_byte)]def get_os_version():"""获取操作系统的主要和次要版本.返回一个元组(OS_MAJOR, OS_MINOR)."""os_version = OSVERSIONINFOEXW()os_version.dwOSVersionInfoSize = ctypes.sizeof(os_version)retcode = ctypes.windll.Ntdll.RtlGetVersion(ctypes.byref(os_version))如果重新编码 != 0:raise Exception("获取操作系统版本失败")返回 os_version.dwMajorVersion, os_version.dwMinorVersion

We've got a script that uses the platform module to detect OS version of our various clients.

Looking through the source for platform.py, I can see that on Windows systems, it's using sys.getwindowsverion(). Unfortunately, on Windows 8.1 systems, that particular function reports:

>>> sys.getwindowsversion()
sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack='')

Windows 8.1 is 6.3.9600:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Windows\system32>ver

Microsoft Windows [Version 6.3.9600]

So, I realize I could write some extra logic around my call to platform.release(), and if that returns 8, do a secondary check and try to run ver, but that seems slightly convoluted.

Does anyone know of a better way?

Running ActivePython 2.7.2.5 in case that matters . . .

解决方案

Microsoft changed the way that the version function behave. See this page for some more information.

The way that I worked around the problem is to use ctypes and a kernel mode function, RtlGetVersion. Despite this being a kernel mode function it can be called from user mode just fine. I've tried this on many versions of Windows and not had an issue.

import ctypes

class OSVERSIONINFOEXW(ctypes.Structure):
    _fields_ = [('dwOSVersionInfoSize', ctypes.c_ulong),
                ('dwMajorVersion', ctypes.c_ulong),
                ('dwMinorVersion', ctypes.c_ulong),
                ('dwBuildNumber', ctypes.c_ulong),
                ('dwPlatformId', ctypes.c_ulong),
                ('szCSDVersion', ctypes.c_wchar*128),
                ('wServicePackMajor', ctypes.c_ushort),
                ('wServicePackMinor', ctypes.c_ushort),
                ('wSuiteMask', ctypes.c_ushort),
                ('wProductType', ctypes.c_byte),
                ('wReserved', ctypes.c_byte)]

def get_os_version():
    """
    Get's the OS major and minor versions.  Returns a tuple of
    (OS_MAJOR, OS_MINOR).
    """
    os_version = OSVERSIONINFOEXW()
    os_version.dwOSVersionInfoSize = ctypes.sizeof(os_version)
    retcode = ctypes.windll.Ntdll.RtlGetVersion(ctypes.byref(os_version))
    if retcode != 0:
        raise Exception("Failed to get OS version")

    return os_version.dwMajorVersion, os_version.dwMinorVersion

这篇关于在 Python 中检测 Windows 8.1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆