From MSDN:Getting the System Version
The following example uses the GetVersionEx function to retrieve version information about the currently running operating system. Note If your application requires a particular operating system version, use the VerifyVersionInfo function to determine whether the currently running system satisfies your requirements. For sample code that uses VerifyVersionInfo, see Verifying the System Version. #include <windows.h>
#include <stdio.h>BOOL DisplaySystemVersion()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx; // Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// which is supported on Windows 2000.
//
// 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)
{
case VER_PLATFORM_WIN32_NT: // Test for the product. if ( osvi.dwMajorVersion <= 4 )
printf( "Microsoft Windows NT "); if ( osvi.dwMajorVersion == 5 )
printf ("Microsoft Windows 2000 "); // Test for workstation versus server. if( bOsVersionInfoEx )
{
if ( osvi.wProductType == VER_NT_WORKSTATION )
printf ( "Professional " ); if ( osvi.wProductType == VER_NT_SERVER )
printf ( "Server " );
}
else
{
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( "Workstation " );
if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
printf( "Server " );
} // Display version, service pack (if any), and build number. printf ("version %d.%d %s (Build %d)\n",
osvi.dwMajorVersion,
osvi.dwMinorVersion,
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF); break; case VER_PLATFORM_WIN32_WINDOWS: if ((osvi.dwMajorVersion > 4) ||
((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0)))
{
printf ("Microsoft Windows 98 ");
}
else printf ("Microsoft Windows 95 "); break; case VER_PLATFORM_WIN32s: printf ("Microsoft Win32s ");
break;
}
return TRUE;
}
The following example uses the GetVersionEx function to retrieve version information about the currently running operating system. Note If your application requires a particular operating system version, use the VerifyVersionInfo function to determine whether the currently running system satisfies your requirements. For sample code that uses VerifyVersionInfo, see Verifying the System Version. #include <windows.h>
#include <stdio.h>BOOL DisplaySystemVersion()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx; // Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// which is supported on Windows 2000.
//
// 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)
{
case VER_PLATFORM_WIN32_NT: // Test for the product. if ( osvi.dwMajorVersion <= 4 )
printf( "Microsoft Windows NT "); if ( osvi.dwMajorVersion == 5 )
printf ("Microsoft Windows 2000 "); // Test for workstation versus server. if( bOsVersionInfoEx )
{
if ( osvi.wProductType == VER_NT_WORKSTATION )
printf ( "Professional " ); if ( osvi.wProductType == VER_NT_SERVER )
printf ( "Server " );
}
else
{
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( "Workstation " );
if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
printf( "Server " );
} // Display version, service pack (if any), and build number. printf ("version %d.%d %s (Build %d)\n",
osvi.dwMajorVersion,
osvi.dwMinorVersion,
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF); break; case VER_PLATFORM_WIN32_WINDOWS: if ((osvi.dwMajorVersion > 4) ||
((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0)))
{
printf ("Microsoft Windows 98 ");
}
else printf ("Microsoft Windows 95 "); break; case VER_PLATFORM_WIN32s: printf ("Microsoft Win32s ");
break;
}
return TRUE;
}
ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if ( GetVersionEx( &ovi) )
{
// 此时可得到以下操作系统信息
// DWORD ovi.dwMajorVersion - 操作系统主版本号
// DWORD ovi.dwMinorVersion - 操作系统从版本号
// DWORD ovi.dwBuildNumber - 操作系统的build number
// DWORD dwPlatformId - VER_PLATFORM_WIN32s : windows 3.1
// VER_PLATFORM_WIN32_WINDOWS : Win95(dwMinorVersion为0),Win98(dwMinorVersion大于0)
// VER_PLATFORM_WIN32_NT : Windows NT
// TCHAR szCSDVersion[128] - 操作系统的附加信息,比如"Service Pack 3"
}