Get OSVersion in Windows using C++

2020-07-27 05:23发布

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?

4条回答
贼婆χ
2楼-- · 2020-07-27 05:36

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);

}
查看更多
beautiful°
3楼-- · 2020-07-27 05:37

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.

查看更多
你好瞎i
4楼-- · 2020-07-27 05:42

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.

查看更多
霸刀☆藐视天下
5楼-- · 2020-07-27 05:43

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

查看更多
登录 后发表回答