From C++ determine if we are running on Windows 10 or not

tullhead
tullhead used Ask the Experts™
on
Since VS 2013 in seems we can use VersionHelpers.h to allow us to ask which version of Windows we are running on.  I looks simple up thru Windows 7 -- but reading the documentation, once we get to Windows 8 or Windows 10, the doc starts talking about the need for a Manifest and mentions GUID.   Can somebody help me understand what is required to get this to work?   My goal: from a C++ program be able to detect if we are running on Windows 10 (or later).   Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I did not work with the project for some time. Anyway, it seems I have detected also the Windows 10 with the following code:
std::string GetThisOSVersionStr()
{
    // Get the binary information about the version.
    OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  
    if ( ! GetVersionEx(&osvi))
        return "OS detection failed";
  
    // If the information was retrieved, build the version string.
    ostringstream oss;

    switch (osvi.dwPlatformId)
    {
    case VER_PLATFORM_WIN32_NT:

        // Version, service pack, build number.
        if (osvi.dwMajorVersion <= 4)
        {
            oss << "Microsoft Windows NT " << osvi.dwMajorVersion
                 << '.' << osvi.dwMinorVersion
                 << ' ' << osvi.szCSDVersion
                 << " (build " << (osvi.dwBuildNumber & 0xFFFF) << ')';
        }
        else if (osvi.dwMajorVersion == 5)
        {
            if (osvi.dwMinorVersion == 0)
                oss << "Microsoft Windows 2000";
            else if (osvi.dwMinorVersion == 1)
                oss << "Microsoft Windows XP";
            else if (osvi.dwMinorVersion == 2)
                oss << "Microsoft Windows Server 2003";
            else
                oss << "Microsoft Windows ??";

            oss << " " << osvi.szCSDVersion
                 << " (build " << (osvi.dwBuildNumber & 0xFFFF) << ')';
        }
        else if (osvi.dwMajorVersion >= 6)
        {
            // Get the detailed information about the version.
            RTL_OSVERSIONINFOEXW osviex;

            if (!GetOsVersion(osviex))     // calls RtlGetVersion() 
                return "OS ex. detection failed";

            if (osviex.dwMajorVersion == 6 && osviex.dwMinorVersion == 0)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows Vista";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2008";
                }
            }
            else if (osviex.dwMajorVersion == 6 && osviex.dwMinorVersion == 1)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 7";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2008 R2";
                }
            }
            else if (osviex.dwMajorVersion == 6 && osviex.dwMinorVersion == 2)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 8";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2012";
                }
            }
            else if (osviex.dwMajorVersion == 6 && osviex.dwMinorVersion == 3)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 8.1";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2012 ";
                }
            }
            else if (osviex.dwMajorVersion == 10 && osviex.dwMinorVersion == 0)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 10";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 10 preview ";
                }
            }
            else
            {
                // Unknown (newer) version of Windows.
                oss << "Microsoft Windows "
                    << osviex.dwMajorVersion << '.' << osviex.dwMinorVersion;
                if (osviex.wProductType != VER_NT_WORKSTATION)
                    oss << " [server]";
            }

            // RtlGetVersion() uses UNICODE. The string must be converted.
            USES_CONVERSION;
            oss << " " << W2A(osviex.szCSDVersion)
                << " (build " << (osviex.dwBuildNumber & 0xFFFF) << ')';
        }
        else if (osvi.dwMajorVersion == 10)
        {
            // Get the detailed information about the version.
            RTL_OSVERSIONINFOEXW osviex;

            if (!GetOsVersion(osviex))     // calls RtlGetVersion() 
                return "OS ex. detection failed";

            if (osviex.dwMinorVersion == 0)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows Vista";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2008";
                }
            }
            else if (osviex.dwMinorVersion == 1)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 7";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2008 R2";
                }
            }
            else if (osviex.dwMinorVersion == 2)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 8";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2012";
                }
            }
            else if (osviex.dwMinorVersion == 3)
            {
                if (osviex.wProductType == VER_NT_WORKSTATION)
                    oss << "Microsoft Windows 8.1";
                else
                {
                    ATLASSERT(osviex.wProductType != VER_NT_WORKSTATION);
                    oss << "Microsoft Windows Server 2012 ";
                }
            }
            else
            {
                // Unknown (newer) version of Windows.
                oss << "Microsoft Windows "
                    << osviex.dwMajorVersion << '.' << osviex.dwMinorVersion;
                if (osviex.wProductType != VER_NT_WORKSTATION)
                    oss << " [server]";
            }

            // RtlGetVersion() uses UNICODE, so the string should be converted.
            USES_CONVERSION;
            oss << " " << W2A(osviex.szCSDVersion)
                << " (build " << (osviex.dwBuildNumber & 0xFFFF) << ')';
        }
        else
            oss << "Unknown NT platform";

        break;

    case VER_PLATFORM_WIN32_WINDOWS:

        if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
        {
            oss << "Microsoft Windows 95 (build " 
                 << (osvi.dwBuildNumber & 0x0000FFFF) << ') ';

            if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
                oss << "OSR2";
            else 
                oss << "OSR1";
        } 

        else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
        {
            oss << "Microsoft Windows 98";

            if (osvi.szCSDVersion[1] == 'A')
                oss << " SE";
        } 

        else if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
        {
            oss << "Microsoft Windows Me";
        } 

        else oss << "unknown WIN32 platform";

        break;

    case VER_PLATFORM_WIN32s:
        oss << "WIN32s platform";
        break;
    }

    return oss.str();
}

Open in new window

The application was 32-bit only, so you may need to fix something.

Author

Commented:
pepr - Thanks, that may be helpful for me.  But one reason I posted the question is that I see that "GetVersionEx" is deprecated by Microsoft and so I'm wondering what the 'modern' way to do it is -- and my limited research suggests its to use things defined in VersionHelpers.h -- but then the doc started talking about Manifests and GUIDs and I got lost....
@tullhead: I remember I once met the functions. I understand what you mean, but I cannot help better. For the manifest, the only thing I remember to solve in the past was adding the manifest to get newer look of the native application.

Microsoft does not make things easy. So, good luck.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial