Determine if O/S is Windows 7

徘徊边缘 提交于 2019-11-29 04:08:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!