Detecting Windows 8.1 in Python?

We have a script that uses the platform module to determine the OS version of our different clients.

Looking through the source for the .py platform, I see that on Windows it uses sys.getwindowsverion (). Unfortunately, on Windows 8.1 systems, this function reports:

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

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 understand that I can write additional logic for my .release () platform call, and if this returns 8, do a second check and try running ver , but that seems a bit confusing.

Does anyone know a better way?

Running ActivePython 2.7.2.5 in case it matters.,

+6
source share
2 answers

Microsoft has changed the way the version works. See this page for more information.

The way I worked on the problem is to use ctypes and the kernel mode function, RtlGetVersion . Although this is a kernel mode function, it can be called from user mode. I tried this on many versions of Windows and did not have a problem.

 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 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 
+3
source

You can simply get it from the registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion there you have the value name CurrentVersion And the data in Windows 8.1 will be 6.3 It will work on any Windows platform

+1
source

Source: https://habr.com/ru/post/955064/


All Articles