Working on a project and need to be able to determine whether the O/S is Windows 7, Vista or default to XP. I understand I could run into Win2K and earlier versions but let's just say that's not a concern as other code will catch that before it gets to this point. My application will be in C++ for the time being using VS2005. I've found articles and sample code alike but they seem way bloated for my uses. Just looking for a quick and dirty return.
http://msdn.microsoft.com/en-us/library/ms724358%28VS.85%29.aspx
List of Windows Version, using GetVersionEx
:
Version Number Description
6.1 Windows 7 / Windows 2008 R2
6.0 Windows Vista / Windows 2008
5.2 Windows 2003
5.1 Windows XP
5.0 Windows 2000
In general, you don't want to be testing against a specific version number, but rather checking for a particular feature. If you really want to detect "Windows 7 or later," however...
#include <windows.h>
bool IsWin7OrLater() {
DWORD version = GetVersion();
DWORD major = (DWORD) (LOBYTE(LOWORD(version)));
DWORD minor = (DWORD) (HIBYTE(LOWORD(version)));
return (major > 6) || ((major == 6) && (minor >= 1));
}
For 2000, compare major and minor against 5 and 0, respectively. For XP, compare against 5 and 1. For Vista, 6 and 0.
The Windows 8.1 SDK1) introduced a number of Version Helper functions, that help determine the version of the OS an application is running on:
#include <VersionHelpers.h>
...
if ( IsWindows7OrGreater() ) {
// Windows 7 or above
} else if ( IsWindowsVistaOrGreater() ) {
// Windows Vista
} else if ( IsWindowsXPOrGreater() ) {
// Windows XP
} else {
// Unsupported version of Windows
}
If you need to distinguish between client and server editions of Windows, you can call IsWindowsServer.
1) The Windows 8.1 SDK can be used to build applications for all supported versions of Windows.
Generally, you can use GetVersionEx
to find the Windows version. A safer way would perhaps be to use VerifyVersionInfo
. There are C examples for both GetVersionEx
and VerifyVersionInfo
.
However, as repeatedly stated on MSDN checking for the operating system version is usually not the best way of determining whether a particular feature is present.