简体   繁体   English

用Python检测Windows 8.1?

[英]Detect Windows 8.1 in Python?

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(). 通过platform.py的源代码,我可以看到在Windows系统上,它使用的是sys.getwindowsverion()。 Unfortunately, on Windows 8.1 systems, that particular function reports: 不幸的是,在Windows 8.1系统上,该特定功能报告:

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

Windows 8.1 is 6.3.9600: Windows 8.1是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. 所以,我意识到我可以在调用platform.release()时编写一些额外的逻辑,如果返回8,则进行二次检查并尝试运行ver ,但这似乎有点费解。

Does anyone know of a better way? 有谁知道更好的方法?

Running ActivePython 2.7.2.5 in case that matters . 在重要的情况下运行ActivePython 2.7.2.5。 . .

Microsoft changed the way that the version function behave. Microsoft改变了版本功能的行为方式。 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 . 我解决这个问题的方法是使用ctypes和内核模式函数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. 我已经在许多版本的Windows上尝试了这个并没有出现问题。

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

你可以从注册表HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows NT \\ CurrentVersion简单地获得它你有值名称CurrentVersion和Windows 8.1中的数据将是6.3它将适用于任何Windows平台

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM