8

I have to get the OSVersion of my Windows8 System (version should be NT 6.2) to use in a C++ application. I tried using GetVersion function call. but it returned me a raw value like 602931718. Is there some way by which I can get the versions as listed here or how can I convert this raw value to a readable form?

Jackzz
  • 1,417
  • 4
  • 24
  • 53
  • 4
    As I understand it, the page you linked has exactly what you need... – Borgleader Apr 29 '15 at 13:11
  • You might be able to do something like [this](https://msdn.microsoft.com/en-us/library/windows/desktop/dn424972(v=vs.85).aspx) if you're willing to use many `if` statements. – Javia1492 Apr 29 '15 at 13:11
  • 4
    Why do new programmers nowadays read only the first, like, six words of any documentation page? Read _all of it_. Everything you need is there. God. – Lightness Races in Orbit Apr 29 '15 at 13:12
  • From `MajorVersionNumber` and `MinorVersionNumber` I'll get `6.2`. but what about `NT`? – Jackzz Apr 29 '15 at 13:12
  • @Jackz Its in the comments at the bottom of the page. – Borgleader Apr 29 '15 at 13:14
  • @Jackz `NT` should come from `dwBuild` – Javia1492 Apr 29 '15 at 13:14
  • @Javia1492: Is it? I didnt try that.. will check it.. Thankyou – Jackzz Apr 29 '15 at 13:16
  • From Microsoft: "For all platforms, the low-order word contains the version number of the operating system. The low-order byte of this word specifies the major version number, in hexadecimal notation. The high-order byte specifies the minor version (revision) number, in hexadecimal notation. The high-order bit is zero, the next 7 bits represent the build number, and the low-order byte is 5." – Javia1492 Apr 29 '15 at 13:18

5 Answers5

14

Note that functions GetVersion and GetVersionEx have been deprecated. This is also reflected in what their doc. pages say (this one copied from GetVersion):

"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."

Vesa P.
  • 149
  • 2
  • 3
  • 13
    Sadly, the _replacement_ method `VerifyVersionInfo` does not semantically fill the same role. it answers the question `Is the current version good enough?` instead of the question `What is the current version?` – Jesse Chisholm Dec 16 '16 at 20:46
  • 3
    Still is there a way to get the current version? – mbaros Jan 30 '17 at 13:14
  • 4
    In Windows 10, [the VerifyVersionInfo function has also been deprecated](https://learn.microsoft.com/en-us/windows/desktop/sysinfo/targeting-your-application-at-windows-8-1). – Stevoisiak Mar 13 '19 at 17:33
  • 4
    You can use the kernel function [`RtlGetVersion`](https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlgetversion?redirectedfrom=MSDN) to get the correct version without relying on how your app is manifested as detailed here https://stackoverflow.com/a/36545162/9423231 – frido Apr 15 '20 at 11:12
3
  1. I have tried the above answer and seems it is not working properly to get the windows OS version above 8.
  2. So after long struggle i have found the source code example and it is returning the versions properly.
  3. With the help of 'GetProcAddress' , getting the version of OS is really easy. More over i have tried the below sample link and it's works fine for me.

Get OS version c++ source code

  1. In-case if anybody having problem to get the OS version , feel free to share your thoughts.
shivcena
  • 2,023
  • 8
  • 24
  • 50
  • This worked great for me, thank you. Just had to include ```#include ``` in addition to the includes in your example for some reason. – Skewjo Dec 16 '20 at 18:24
2

I have compiled This programmer With Dev-C++ on Windows 7.The Programme is same as the Ist answer on This Question But On Dev-C++ The OSVERSIONINFOEX info requires typecasting when called in GetVersionEx().So Here Is The Code

#include<windows.h>
#include<stdio.h>
int main()
{
     OSVERSIONINFOEX info;
     ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
     info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
     GetVersionEx((LPOSVERSIONINFO)&info);//info requires typecasting

     printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);

}
Mohit Dabas
  • 2,333
  • 1
  • 18
  • 12
1

Have you looked at GetVersionEx() function and OSVERSIONINFOEX structure?

Possible usage:

void print_os_info()
{
    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

    GetVersionEx(&info);

    printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);
}

I don't understand, what do you mean by NT. According to MSDN:

Version table

Since Windows XP, all versions are implicitly NT versions. If you want to test against Server versions, check value of info.wProductType:

if(info.dwMajorVersion == 6)
{
    if (info.dwMinorVersion == 0)
    {
        if (info.wProductType == VER_NT_WORKSTATION)
            //Windows Vista;
        else
            //Windows Server 2008
    }
    else if (info.dwMinorVersion == 1)
    {
        if (info.wProductType == VER_NT_WORKSTATION)
            //Windows 7
        else
            //Windows Server 2008 R2
    }
    else if (...) //etc...
}

And one more thing: you can also check value of info.dwBuildNumber. One of allowed values is VER_PLATFORM_WIN32_NT.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
-3

If you want to make it more convenient, you could try converting to a float instead of checking two integers:

double GetOsVersion() {
    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    GetVersionEx(&info);

    double version;

    version = info.dwMajorVersion 
            + (info.dwMinorVersion / 10.0)
            - (info.wProductType == VER_NT_WORKSTATION) ? 0.5 : 0.0;

    return ver;
}

// Windows 10             : 10.0
// Windows 8.1            : 6.3
// Windows 8.0            : 6.2
// Windows Server 2012    : 6.15
// Windows 7              : 6.1
// Windows Server 2008 R2 : 6.05
// Windows Vista          : 6.0

Regarding last term added to create float version, specifically, "using VER_NT_WORKSTATION", I had some debate about if the server version should be +0.05 or -0.05, or the same as non-server version. I'll leave that one up to you to decide.

drulex
  • 47
  • 4
Bimo
  • 5,987
  • 2
  • 39
  • 61
  • 4
    This is bad advice, as 6.3 and 6.2 or 6.05 cannot be represented exactly using a `double` type, so they will be approximated to a value slightly smaller or larger. Tests such as `GetOsVersion() >= 6.1` to detect Windows 7 or later may thus fail on Windows 7. – sam hocevar Aug 28 '19 at 13:36