GetVersionEx可以的哦Getting the System VersionThe following example uses the GetVersionEx function to display the version of the currently running operating system. 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. #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 95 "); 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; }
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
TCHAR szBuf[ 4096 ]; lstrcpy( szBuf,"Error!" ); // 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 ) ) {
lstrcpy( szBuf,"Error!" );
return;
}
}
switch( osvi.dwPlatformId ) {
case VER_PLATFORM_WIN32_NT:
// Test for the product.
if( osvi.dwMajorVersion <= 4 )
lstrcpy( szBuf,_T("Microsoft Windows NT") );
if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
lstrcpy( szBuf,_T("Microsoft Windows 2000") );
if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
lstrcpy( szBuf,_T("Microsoft Windows XP") ); // Test for product type.
if( bOsVersionInfoEx ) {
if( osvi.wProductType == VER_NT_WORKSTATION ) {
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
lstrcat( szBuf,_T(" Personal") );
else
lstrcat( szBuf,_T(" Professional") );
}
else if( osvi.wProductType == VER_NT_SERVER ) {
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
lstrcat( szBuf,_T(" DataCenter Server") );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
lstrcat( szBuf,_T(" Advanced Server") );
else
lstrcat( szBuf,_T(" Server") );
}
}
else {
HKEY hKey;
TCHAR szProductType[ 80 ];
DWORD dwBufLen; RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
0,
KEY_QUERY_VALUE,
&hKey
);
RegQueryValueEx(
hKey,
_T("ProductType"),
NULL,
NULL,
( LPBYTE )szProductType,
&dwBufLen
);
RegCloseKey( hKey );
if( lstrcmpi( _T("WINNT"),szProductType) == 0 )
lstrcat( szBuf,_T(" Professional") );
if( lstrcmpi( _T("LANMANNT"),szProductType) == 0 )
lstrcat( szBuf,_T(" Server") );
if( lstrcmpi( _T("SERVERNT"),szProductType) == 0 )
lstrcat( szBuf,_T(" Advanced Server") );
} // Display version,service pack (if any),and build number.
if( osvi.dwMajorVersion <= 4 ) {
TCHAR tmp[ 80 ];
wsprintf(
tmp,
_T(" Version %d.%d %s (Build %d)"),
osvi.dwMajorVersion,
osvi.dwMinorVersion,
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF
);
lstrcat( szBuf,tmp );
}
else {
TCHAR tmp[ 80 ];
wsprintf( tmp,_T(" %s (Build %d)"),osvi.szCSDVersion,osvi.dwBuildNumber & 0xFFFF );
lstrcat( szBuf,tmp );
}
break; case VER_PLATFORM_WIN32_WINDOWS:
if( osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0 ) {
lstrcpy( szBuf,_T("Microsoft Windows 95") );
if( osvi.szCSDVersion[ 1 ] == 'C' || osvi.szCSDVersion[ 1 ] == 'B' ) {
lstrcat( szBuf,_T(" OSR2") );
}
}
if( osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10 ) {
lstrcpy( szBuf,_T("Microsoft Windows 98") );
if( osvi.szCSDVersion[ 1 ] == 'A' ) {
lstrcat( szBuf,_T(" SE") );
}
}
if( osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90 ) {
lstrcpy( szBuf,_T("Microsoft Windows Me") );
}
break;
case VER_PLATFORM_WIN32s:
lstrcpy( szBuf,_T("Microsoft Win32s") );
break;
default:
lstrcpy( szBuf,"Error!" );
} AfxMessageBox( szBuf ); // 版本信息保存在 szBuf 中!
}
#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 95 ");
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;
}
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[ 128 ];
} OSVERSIONINFO;