Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How I can check in C++ if Windows version installed on computer is Windows Vista and higher (Windows 7)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

All the answers in this thread point you to using GetVersion or GetVersionEx for this test, which is incorrect. It seems to work, but it is risky. The primary source of appcompat problems for Windows OS upgrades comes from poorly written tests based on GetVersion results with bad assumptions or buggy comparisons.

The correct way to do this test is to use VerifyVersionInfo, not GetVersion or GetVersionEx.

If you are using the VS 2013 compiler toolset and the Windows 8.1 SDK, you can use the VersionHelpers.h and just call IsWindowsVistaOrGreater.

If you are using the VS 2013 v120_xp platform toolset to target Windows XP, you are actually using the Windows 7.1A SDK, so you need to use VeriyVersionInfo directly.

Otherwise, use:

bool IsWindowsVistaOrGreater()
{
OSVERSIONINFOEXW osvi = {};
osvi.dwOSVersionInfoSize = sizeof(osvi);
DWORDLONG const dwlConditionMask = VerSetConditionMask(
    VerSetConditionMask(
    VerSetConditionMask(
            0, VER_MAJORVERSION, VER_GREATER_EQUAL),
               VER_MINORVERSION, VER_GREATER_EQUAL),
               VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_VISTA);
osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_VISTA);
osvi.wServicePackMajor = 0;

return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
}

This code will work on Windows 2000 or later and give you a robust result. If you really needed this test to run on Windows 98 or Windows ME -and- you are using a compiler toolset old enough to actually run on that platform, you'd do the same test but with explicit rather than implicit linking. What's in a version number?

Furthermore, using GetVersion or GetVersionEx will by default get the wrong version on Windows 8.1 and Windows 10. See Manifest Madness.

Note that with Windows 10 VerifyVersionInfo is also subject to the same manifest-based behavior (i.e. without the GUID element for Windows 10, VVI acts as if the OS version number is 6.2 rather than 10.0. That said, most real-world tests like IsWindowsVistaOrGreater, IsWindows7OrGreater, IsWindows7SP1OrGreater, IsWindows8OrGreater are all going to work just fine even without the manifest. It's only if you are using IsWindows8Point1OrGreater or IsWindows10OrGreater that the manifest-based behavior even matters.

See also this stack overflow thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...