methods
1. GetVersion function
Retrieves the version number of the current operating system.
Note This function has been superseded by GetVersionEx. New applications should use GetVersionEx or VerifyVersionInfo.
DWORD WINAPI GetVersion(void);The following illustration shows the format of the bits in system version.
example:
DWORD dwVersion = 0; DWORD dwMajorVersion = 0; DWORD dwMinorVersion = 0; DWORD dwBuild = 0; dwVersion = GetVersion(); // Get the Windows version. dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion))); dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion))); // Get the build number. if (dwVersion < 0x80000000) dwBuild = (DWORD)(HIWORD(dwVersion)); printf("Version is %d.%d (%d)\n", dwMajorVersion, dwMinorVersion, dwBuild);
2. GetVersionEx function
The GetVersionEx function retrieves the major and minor version numbers and other information about the currently running version of the operating system.
BOOL WINAPI GetVersionEx( __inout LPOSVERSIONINFO lpVersionInfo );
lpVersionInfo
An OSVERSIONINFO or OSVERSIONINFOEX structure that receives the operating system information.Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of this structure as appropriate.
OSVERSIONINFO struct:
typedef struct _OSVERSIONINFO { DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; TCHAR szCSDVersion[128]; } OSVERSIONINFO;OSVERSIONINFOEX struct:
typedef struct _OSVERSIONINFOEX { DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; TCHAR szCSDVersion[128]; WORD wServicePackMajor; WORD wServicePackMinor; WORD wSuiteMask; BYTE wProductType; BYTE wReserved; } OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;example:
OSVERSIONINFO _osv; ZeroMemory(&_osv, sizeof(OSVERSIONINFO)); _osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); ::GetVersionEx(&_osv); cout<<"Major:"<<_osv.dwMajorVersion<<endl; cout<<"Minor:"<<_osv.dwMinorVersion<<endl; cout<<"Build:"<<_osv.dwBuildNumber<<endl; cout<<"Platform:"<<_osv.dwPlatformId<<endl; cout<<"Others:"<<_osv.szCSDVersion<<endl;dwMajorVersion, dwMinorVersion, dwBuildNumber与 GetVersion的返回值含义一样,只不过不用自己通过宏来提取,同时加入了一个关键字段 dwPlatformId,参考值如下
Value | Description |
---|---|
VER_PLATFORM_WIN32s | Specifies the Windows 3.1 OS. |
VER_PLATFORM_WIN32_WINDOWS | Specifies the Windows 95 or Windows 98 OS. For Windows 95, dwMinorVersion is zero. For Windows 98, dwMinorVersion is greater than zero. |
VER_PLATFORM_WIN32_NT | Specifies the Windows NT OS. |
VER_PLATFORM_WIN32_CE | Specifies the Windows CE OS. |
3. VerifyVersionInfo function
Compares a set of operating system version requirements to the corresponding values for the currently running version of the system.
BOOL WINAPI VerifyVersionInfo( __in LPOSVERSIONINFOEX lpVersionInfo, __in DWORD dwTypeMask, __in DWORDLONG dwlConditionMask );Note:lpVersionInfo是你期望的operating system version,需要初始化其内容包括dwOSVersionInfoSize成员需要设置;dwTypeMask是你想要比较的OSVERSIONINFOEX中那个成员,使用|可以实现比较多个成员;dwlConditionMask设置operator,是大于,小于等。
使用步骤如下:
OSVERSIONINFOEX osvi; DWORDLONG dwlConditionMask = 0; int op=VER_GREATER_EQUAL; // Initialize the OSVERSIONINFOEX structure. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); osvi.dwMajorVersion = 5; osvi.dwMinorVersion = 1; osvi.wServicePackMajor = 2; osvi.wServicePackMinor = 0; // Initialize the condition mask. VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op ); VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op ); VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op ); VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op ); // Perform the test. return VerifyVersionInfo( &osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);VerifyVersionInfo enables you to easily determine the presence of a required set of operating system version conditions. It is preferable to use VerifyVersionInfo rather than calling the GetVersionEx function to perform your own comparisons.
The following table summarizes the most recent operating system version numbers.
Operating system | Version number |
---|---|
Windows 8 | 6.2 |
Windows Server 2012 | 6.2 |
Windows 7 | 6.1 |
Windows Server 2008 R2 | 6.1 |
Windows Server 2008 | 6.0 |
Windows Vista | 6.0 |
Windows Server 2003 R2 | 5.2 |
Windows Server 2003 | 5.2 |
Windows XP | 5.1 |
Windows 2000 | 5.0 |