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
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
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.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.
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.
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
Post a Comment