views:

306

answers:

4

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

A: 

does this link help? http://msdn.microsoft.com/en-us/library/ak37a69s%28VS.80%29.aspx

JP Hellemons
That for managed C++ (.net)
BlueRaja - Danny Pflughoeft
+3  A: 

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.

Joey
+9  A: 

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
Paulo Santos
+4  A: 

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.

ChrisV