[ACCEPTED]-Check Windows version-version

Accepted answer
Score: 31

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

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

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

If 22 you are using the VS 2013 v120_xp platform toolset 21 to target Windows XP, you are actually using 20 the Windows 7.1A SDK, so you need to use 19 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 18 work on Windows 2000 or later and give you 17 a robust result. If you really needed this 16 test to run on Windows 98 or Windows ME 15 -and- you are using a compiler toolset old 14 enough to actually run on that platform, you'd 13 do the same test but with explicit rather 12 than implicit linking. What's in a version number?

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

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

See also this stack overflow thread.

Score: 20

Use GetVersionEx API function defined in kernel32.dll:

bool IsWindowsVistaOrHigher() {
   OSVERSIONINFO osvi;
   ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
   GetVersionEx(&osvi);
   return osvi.dwMajorVersion >= 6;
}

0

Score: 18

Similar to other tests for checking the 1 version of Windows NT:

OSVERSIONINFO   vi;

memset (&vi, 0, sizeof vi);
vi .dwOSVersionInfoSize = sizeof vi;
GetVersionEx (&vi);
if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT  &&  vi.dwMajorVersion >= 6)
Score: 13

In Visual Studio 2013 or higher, you can 3 also use the new Version Helper functions.

There 2 are methods for many different Windows versions. Example:

#include <VersionHelpers.h>

if (!IsWindowsVistaOrGreater())
{
   MessageBox(NULL, "You need at least Windows Vista", "Version Not Supported", MB_OK);
}

More 1 information here

Score: 6

I think you're looking for the GetVersionEx function.

0

Score: 4

This Microsoft support page gives you details for older versions.

To 7 determine the operating system that is running 6 on a given system, the following data is 5 needed:

              95  98  ME  NT 4  2000  XP
PlatformID    1   1   1   2     2     2
Major version 4   4   4   4     5     5
Minor version 0   10  90  0     0     1

You could implement the code and 4 run it on a Vista and Windows-7 machine 3 to check the values returned.

To get the 2 operating system version information make 1 the following call:

System::OperatingSystem *osInfo = System::Environment::OSVersion;
Score: 1

You could use the GetVersion() or GetVersionEx() function 3 in the kernel32.dll. This two functions 2 are only available on Windows 2000 or later.

To 1 read more about this look at http://msdn.microsoft.com/en-us/library/ms724451%28VS.85%29.aspx.

More Related questions