#include <windows.h> #include <stdio.h> BOOL DisplaySystemVersion() { OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; //Try calling GetVersionEx using the OSVERSIONINFOEX structure. //If that fails, try using the OSVERSIONINFO structure. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi))) { //If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if(!GetVersionEx( ( OSVERSIONINFO*)&osvi)) return FALSE; } switch(osvi.dwPlatformId) { //Tests for Windows NT product family. case VER_PLATFORM_WIN32_NT : //Test for the product. if( osvi.dwMajorVersion <= 4 ) printf("Microsoft Windows NT "); if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) printf("Microsoft Windows 2000 "); if( bOsVersionInfoEx )//Use information from GetVersionEx. { //Test for the workstation type. if( osvi.wProductType == VER_NT_WORKSTATION ) { if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) printf("Microsoft Windows XP "); if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) printf("Home Edition "); else printf("Professional "); } //Test for the server type. else if( osvi.wProductType == VER_NT_SERVER ) { if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) printf("Microsoft Windows .NET "); if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) printf("DataCenter Server "); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) if( osvi.dwMajorVersion == 4 ) printf("Advanced Server "); else printf("Enterprise Server " ); else if( osvi.wSuiteMask == VER_SUITE_BLADE ) printf("Web Server "); else printf("Server "); } } else //Use the registry on early versions of Windows NT. { HKEY hKey; char szProductType[80]; DWORD dwBufLen; RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM//CurrentControlSet//Control//ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); RegQueryValueEx( hKey, "ProductType", NULL, NULL, (LPBYTE)szProductType, &dwBufLen ); RegCloseKey( hKey ); if( lstrcmpi( "WINNT", szProductType) == 0 ) printf( "Professional" ); if( lstrcmpi( "LANMANNT", szProductType) == 0 ) printf( "Server "); if( lstrcmpi( "SERVERNT", szProductType) == 0 ) printf( "Advanced Server" ); } //Display version, service pack (if any), and build number. if( osvi.dwMajorVersion <= 4 ) { printf( "version %d.%d %s (Build %d)/n", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } else { printf("%s (Build %d)/n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } break; //Test for the Windows 95 product family. case VER_PLATFORM_WIN32_WINDOWS : if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { printf( "Microsoft Windows 9 " ); if( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) printf( " OSR2 " ); } if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10 ) { printf ("Microsoft Windows 98"); if( osvi.szCSDVersion[1] == 'A' ) printf( "SE " ); } if( osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90 ) { printf( "Microsoft Windows Millennium Edition" ); } break; } return TRUE; }
Operating system | Version number | dwMajorVersion | dwMinorVersion | Other |
---|---|---|---|---|
Windows7 | 6.1 | 6 | 1 | OSVERIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server2008R2 | 6.1 | 6 | 1 | OSVERIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Server2008 | 6.0 | 6 | 0 | OSVERIONINFOEX.wProductType != VER_NT_WORKSTATION |
WindowsVista | 6.0 | 6 | 0 | OSVERIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server2003 R2 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) != 0 |
Windows Server2003 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) == 0 |
WindowsXP | 5.1 | 5 | 1 | Not applicable |
Windows2000 | 5.0 | 5 | 0 | Not applicable |
typedef struct _OSVERSIONINFO { DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; TCHAR szCSDVersion[128];} OSVERSIONINFO;
-
dwOSVersionInfoSize
-
The size of this data structure, in bytes. Set this member to
sizeof(OSVERSIONINFO)
.
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 the following value.
Value Meaning -
VER_PLATFORM_WIN32_NT2
The operating system is Windows Server2008, WindowsVista, Windows Server2003, WindowsXP, or Windows2000.
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.
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;
dwOSVersionInfoSizeThe size of this data structure, in bytes. Set this member to sizeof(OSVERSIONINFOEX)
.
The major version number of the operating system. For more information, see Remarks.
dwMinorVersionThe minor version number of the operating system. For more information, see Remarks.
dwBuildNumberThe build number of the operating system.
dwPlatformIdThe operating system platform. This member can be VER_PLATFORM_WIN32_NT (2).
szCSDVersionA 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.
wServicePackMajorThe 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.
wServicePackMinorThe minor version number of the latest Service Pack installed on the system. For example, for Service Pack 3, the minor version number is 0.
wSuiteMaskA bit mask that identifies the product suites available on the system. This member can be a combination of the following values.
Value | Meaning |
---|---|
| Microsoft BackOffice components are installed. |
| Windows Server2003, Web Edition is installed. |
| Windows Server2003, Compute Cluster Edition is installed. |
| Windows Server2008 Datacenter, Windows Server2003, Datacenter Edition, or Windows2000 Datacenter Server is installed. |
| Windows Server2008 Enterprise, Windows Server2003, Enterprise Edition, or Windows2000 Advanced Server is installed. Refer to the Remarks section for more information about this bit flag. |
| WindowsXP Embedded is installed. |
| WindowsVista Home Premium, WindowsVista Home Basic, or WindowsXP Home Edition is installed. |
| Remote Desktop is supported, but only one interactive session is supported. This value is set unless the system is running in application server mode. |
| Microsoft Small Business Server was once installed on the system, but may have been upgraded to another version of Windows. Refer to the Remarks section for more information about this bit flag. |
| Microsoft Small Business Server is installed with the restrictive client license in force. Refer to the Remarks section for more information about this bit flag. |
| Windows Storage Server2003 R2 or Windows Storage Server2003is installed. |
| Terminal Services is installed. This value is always set. If VER_SUITE_TERMINAL is set but VER_SUITE_SINGLEUSERTS is not set, the system is running in application server mode. |
| Windows Home Server is installed. |
Any additional information about the system. This member can be one of the following values.
Value | Meaning |
---|---|
| The system is a domain controller and the operating system is Windows Server2008, Windows Server2003, or Windows2000 Server. |
| The operating system is Windows Server2008, Windows Server2003, or Windows2000 Server. Note that a server that is also a domain controller is reported as VER_NT_DOMAIN_CONTROLLER, not VER_NT_SERVER. |
| The operating system is WindowsVista, WindowsXP Professional, WindowsXP Home Edition, or Windows2000 Professional. |
Reserved for future use.