Jackzz Jackzz - 2 months ago 7
C++ Question

Get OSVersion in Windows using C++

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?

Answer

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.

Comments