Skip to main content

How to get OS version

Have you ever tried to get correct OS version number on Windows 10? If yes, you know what the pain in the ass it is.

Old days we can simple call GetVersion() or GetVersionEx() and can be sure that information returned by the function is ansolutely correct.

However, starting from Windows 8.1 Microsoft has changed the behavior of those functions. Now it returns weather on the Mars but not OS version. This is how it is described in MSDN

With the release of Windows 8.1, the behavior of the GetVersion API has changed in the value it will return for the operating system version. The value returned by the GetVersion function now depends on how the application is manifested.

Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). Once an application is manifested for a given operating system version, GetVersion will always return the version that the application is manifested for in future releases.
They offer to use IsOSxxx functions. That is probably good way but absolutely unusable, especial when we have lot of builds of Windows 10 and each new build adds something new, adds new bugs and, sometimes, fixes old bugs. So it is very often situation (if you develop more or less complex application) when you need to know the exactly OS version, including the build number.

The other situation when you need exactly OS version is when your application starts from Windows Timeline and has no manifest with target platform specified. It can use all the features but GetOsVersion (and IsOSxxx) returns Windows 8 esteand of 10. So we have to find a correct (more or less) way to detect exactly OS version.

Possible solutions (can be found as very popular answers on StackOverflow) are: read OS version from Registry or from kernel32.dll. However it does not work because Windows 10 virtualizes Registry and for app it looks as Win 8 registry (the same appears with kernel32.dll).

Fortunately if version of kernel32.dll is wrong the registry reading is correct (in some situation it also can be incorrect but fortunately this may appear only if you select "Run App as under Win XP" or something similar). So the algorithm is the following:

1. Check version of kernel32.dll.
2. If it returns Win 8 or Win 8.1 (version number 6.2 or 6.3) check system registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion).

In System Registry we will find that the version number is still 6.3 (yes, for Windows 10 it is 6.3, the same as for Windows 81). But MS added new parameter: CurrentMajorVersionNumber.

If that parameter is available it returns exactly Windows version (10). If reading was success we must read OS build number from registry as well. The parameter is CurrentBuildNumber.

Using that algorithm we can be sure that we have correct OS version number (at least in 90% of cases).

Source code can be found in this article.

I use this method in my Wireless Communication Library and it works great on all supported platforms in any possible situation (at least for now).

Should you have any question please contact me by mike@btframework.com

Comments

Popular posts from this blog

Remote Control Joystick

It appeared that I need to connect my Spektrum DX9 to PC. Unfortunately I have no USB cable for that. And also I read that it may die when connected to PC through trainer port. Fortunately I have Orange 6-channel RX and Arduino UNO. So I decided to create own joystick that I can use with LiftOff simulator. Hardware To create your own Remote Control Joystick you need: Any receiver (currently up to eight channels is supported). Arduino UNO or Arduino MEGA with 8u2, 16u2  or 32u2 USB chip. 9 wires. Note : some cheap Arduino built with other USB-Serial bridge. Make sure your uses Atmeg as USB interface. Preparing First you must prepare your Arduino so it appears as USB HID device for your PC. By default Arduino appears as VCP only. To do so yo uhave to flash your 16u2 with HoodLoader.  There are 2 ways to flash it: Standalone Arduino or from other Arduino . Select which is better for you. I used first method. Note : There is 100nF capacitor required to fla

How to get OS version. Part 2.

Not so long time ago I have published short article about reading Windows OS version. I have got lot of e-mails asking me for source code. In fact, there is nothing hard to do what I described but after describing it lot of times I decided to create few units with read OS version functions. There are three units: for Delphi, for C# and for C++. You can download it on GitHub I use this method in my Wireless Communication Library and it works great on all supported platforms in any possible situation (at least for now). Should you have any question please contact me by mike@btframework.com