#ifndef SM_SERVERR2
# define SM_SERVERR2 89
#endif
#ifndef VER_SUITE_STORAGE_SERVER
# define VER_SUITE_STORAGE_SERVER 0x00002000
#endif
#ifndef VER_SUITE_COMPUTE_SERVER
# define VER_SUITE_COMPUTE_SERVER 0x00004000
#endif
CString GetSystemName()
{
SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器
GetSystemInfo(&info); //调用GetSystemInfo函数填充结构
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
if(GetVersionEx((OSVERSIONINFO *)&os))
{
CString vname;
//下面根据版本信息判断操作系统名称
switch(os.dwMajorVersion){ //判断主版本号
case 4:
switch(os.dwMinorVersion){ //判断次版本号
case 0:
if(os.dwPlatformId==VER_PLATFORM_WIN32_NT)
vname=_T("Windows NT 4.0"); //1996年7月发布
else if(os.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
vname=_T("Windows 95");
break;
case 10:
vname=_T("Windows 98");
break;
case 90:
vname=_T("Windows Me");
break;
}
break;
case 5:
switch(os.dwMinorVersion){ //再比较dwMinorVersion的值
case 0:
vname=_T("Windows 2000"); //1999年12月发布
break;
case 1:
vname=_T("Windows XP"); //2001年8月发布
break;
case 2:
if(os.wProductType==VER_NT_WORKSTATION && \
info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
vname=_T("Windows XP Professional x64 Edition");
else if(GetSystemMetrics(SM_SERVERR2)==0)
vname=_T("Windows Server 2003"); //2003年3月发布
else if(GetSystemMetrics(SM_SERVERR2)!=0)
vname=_T("Windows Server 2003 R2");
break;
}
break;
case 6:
switch(os.dwMinorVersion){
case 0:
if(os.wProductType==VER_NT_WORKSTATION)
vname=_T("Windows Vista");
else
vname=_T("Windows Server 2008"); //服务器版本
break;
case 1:
if(os.wProductType==VER_NT_WORKSTATION)
vname=_T("Windows 7");
else
vname=_T("Windows Server 2008 R2");
break;
case 2:
if(os.wProductType==VER_NT_WORKSTATION)
vname=_T("Windows 8");
else
vname=_T("Windows Server 2012");
break;
case 3:
if(os.wProductType==VER_NT_WORKSTATION)
vname=_T("Windows 8.1");
else
vname=_T("Windows Server 2012 R2");
break;
}
break;
default:
vname=_T("未知操作系统");
}
return vname;
}
else
return _T("");
}
from:http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx
OSVERSIONINFOEX structure
Contains operating system version information. The information includes major and minor version numbers, a build number, a platform identifier, and information about product suites and the latest Service Pack installed on the system. This structure is used with the GetVersionEx and VerifyVersionInfo functions.
Syntax
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;
Members
-
dwOSVersionInfoSize
-
The size of this data structure, in bytes. Set this member to
sizeof(OSVERSIONINFOEX)
.
dwMajorVersion
-
The major version number of the operating system. For more information, see Remarks.
dwMinorVersion
-
The minor version number of the operating system. For more information, see Remarks.
dwBuildNumber
-
The build number of the operating system.
dwPlatformId
-
The operating system platform. This member can be VER_PLATFORM_WIN32_NT (2).
szCSDVersion
-
A null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty.
wServicePackMajor
-
The major version number of the latest Service Pack installed on the system. For example, for Service Pack 3, the major version number is 3. If no Service Pack has been installed, the value is zero.
wServicePackMinor
-
The minor version number of the latest Service Pack installed on the system. For example, for Service Pack 3, the minor version number is 0.
wSuiteMask
-
A bit mask that identifies the product suites available on the system. This member can be a combination of the following values.
wProductType
-
Any additional information about the system. This member can be one of the following values.
wReserved
-
Reserved for future use.
Remarks
Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version.
If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows.
The following table summarizes the values returned by supported versions of Windows. Use the information in the column labeled "Other" to distinguish between operating systems with identical version numbers.
Operating system | Version number | dwMajorVersion | dwMinorVersion | Other |
---|---|---|---|---|
Windows 8.1 | 6.3* | 6 | 3 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2012 R2 | 6.3* | 6 | 3 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows 8 | 6.2 | 6 | 2 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2012 | 6.2 | 6 | 2 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows 7 | 6.1 | 6 | 1 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2008 R2 | 6.1 | 6 | 1 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Server 2008 | 6.0 | 6 | 0 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Vista | 6.0 | 6 | 0 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2003 R2 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) != 0 |
Windows Home Server | 5.2 | 5 | 2 | OSVERSIONINFOEX.wSuiteMask & VER_SUITE_WH_SERVER |
Windows Server 2003 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) == 0 |
Windows XP Professional x64 Edition | 5.2 | 5 | 2 | (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) |
Windows XP | 5.1 | 5 | 1 | Not applicable |
Windows 2000 | 5.0 | 5 | 0 | Not applicable |
* For applications that have been manifested for Windows 8.1. Applications not manifested for 8.1 will return the Windows 8 OS version value (6.2). To manifest your applications for Windows 8.1 please refer to Targeting your application for Windows 8.1. |
You should not rely upon only the VER_SUITE_SMALLBUSINESS flag to determine whether Small Business Server has been installed on the system, as both this flag and the VER_SUITE_SMALLBUSINESS_RESTRICTEDflag are set when this product suite is installed. If you upgrade this installation to Windows Server, Standard Edition, the VER_SUITE_SMALLBUSINESS_RESTRICTED flag will be cleared—however, theVER_SUITE_SMALLBUSINESS flag will remain set. In this case, this indicates that Small Business Server was once installed on this system. If this installation is further upgraded to Windows Server, Enterprise Edition, theVER_SUITE_SMALLBUSINESS flag will remain set.
If compatibility mode is in effect, the OSVERSIONINFOEX structure contains information about the operating system that is selected for application compatibility.
To determine whether a Win32-based application is running on WOW64, call the IsWow64Process function. To determine whether the system is running a 64-bit version of Windows, call the GetNativeSystemInfo function.
The GetSystemMetrics function provides the following additional information about the current operating system.
Product | Setting |
---|---|
Windows Server 2003 R2 | SM_SERVERR2 |
Windows XP Media Center Edition | SM_MEDIACENTER |
Windows XP Starter Edition | SM_STARTER |
Windows XP Tablet PC Edition | SM_TABLETPC |
Examples
For an example, see Getting the System Version.
Requirements
Minimum supported client | Windows 2000 Professional [desktop apps only] |
---|---|
Minimum supported server | Windows 2000 Server [desktop apps only] |
Header |
|
Unicode and ANSI names | OSVERSIONINFOEXW (Unicode) and OSVERSIONINFOEXA (ANSI) |