首页 > 编程资源分享区 > C/C++源代码共享 > 完善的 Windows 版本检测代码
2006
07-05

完善的 Windows 版本检测代码

关于 Windows 版本检测 的目前比较完善的代码段:

    // ===============================================
    // Getting the System Version.
    // Copyrights(C)2002 YangTze presents – 09/24/2002
    // ===============================================
    {
        OSVERSIONINF OEX 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.dwOSVer sionInfoSize = sizeof( OSVERSIONINFOEX );
        if( !( bOsVersionInfoEx = GetVersionEx( ( OSVERSIONINFO * )&osvi ) ) ) {
             // If OSVERSIONINFOEX doesn’t work,try OSVERSIONINFO.
             osvi.dwOSVer sionInfoSize = 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\\Contr ol\\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!” );
        }

        AfxMessageBo x( szBuf ); // 版本信息保存在 szBuf 中!
    }


留下一个回复