Windows API ——GetVersionEx——获取操作系统版本信息

来源:互联网 发布:mac 网页关闭 复原 编辑:程序博客网 时间:2024/05/01 16:13
  1 //获取系统版本  2 BOOL GetOSName( CString& csOsName )  3 {  4  OSVERSIONINFOEX osvi;  5  SYSTEM_INFO si;  6  BOOL bOsVersionInfoEx;  7  ZeroMemory(&si, sizeof(SYSTEM_INFO));  8  ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));  9  10  // Try calling GetVersionEx using the OSVERSIONINFOEX structure. 11  // If that fails, try using the OSVERSIONINFO structure. 12  13  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 14  15  bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *) &osvi); 16  if( !bOsVersionInfoEx ) 17  { 18   osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); 19   if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )  20    return FALSE; 21  } 22  23  // Call GetNativeSystemInfo if supported 24  // or GetSystemInfo otherwise. 25  else GetSystemInfo(&si); 26  27  switch (osvi.dwPlatformId) 28  { 29   // Test for the Windows NT product family. 30  31  case VER_PLATFORM_WIN32_NT: 32  33   // Test for the specific product. 34  35   if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 ) 36   { 37    if( osvi.wProductType == VER_NT_WORKSTATION ) 38    { 39     csOsName += _T( "Windows Vista " ); 40    } 41    else  42    { 43     csOsName += _T( "Windows Server \"Longhorn\" " ); 44    } 45   } 46  47   if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) 48   { 49    if( osvi.wProductType == VER_NT_WORKSTATION && 50     si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) 51    { 52     csOsName += _T( "Microsoft Windows XP Professional x64 Edition " ); 53    } 54    else  55    { 56     csOsName += _T( "Microsoft Windows Server 2003, " ); 57    } 58   } 59  60   if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) 61   { 62    csOsName += _T( "Microsoft Windows XP " ); 63   } 64  65   if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) 66   { 67    csOsName += _T( "Microsoft Windows 2000 " ); 68   } 69  70   if ( osvi.dwMajorVersion <= 4 ) 71   { 72    csOsName += _T( "Microsoft Windows NT " ); 73   } 74  75   // Test for specific product on Windows NT 4.0 SP6 and later. 76   if( bOsVersionInfoEx ) 77   { 78    // Test for the workstation type. 79    if ( osvi.wProductType == VER_NT_WORKSTATION && 80     si.wProcessorArchitecture!=PROCESSOR_ARCHITECTURE_AMD64) 81    { 82     if( osvi.dwMajorVersion == 4 ) 83     { 84      csOsName += _T( "Workstation 4.0 " ); 85     } 86     else if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) 87     { 88      csOsName += _T( "Home Edition " ); 89     } 90     else  91     { 92      csOsName += _T( "Professional " ); 93     } 94    } 95  96    // Test for the server type. 97    else if ( osvi.wProductType == VER_NT_SERVER ||  98     osvi.wProductType == VER_NT_DOMAIN_CONTROLLER ) 99    {100     if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==2)101     {102      if ( si.wProcessorArchitecture ==103       PROCESSOR_ARCHITECTURE_IA64 )104      {105       if( osvi.wSuiteMask & VER_SUITE_DATACENTER )106       {107        csOsName += _T("Datacenter Edition for Itanium-based Systems");108       }109       else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )110       {111        csOsName += _T("Enterprise Edition for Itanium-based Systems");112       }113      }114 115      else if ( si.wProcessorArchitecture ==116       PROCESSOR_ARCHITECTURE_AMD64 )117      {118       if( osvi.wSuiteMask & VER_SUITE_DATACENTER )119       {120        csOsName += _T( "Datacenter x64 Edition " );121       }122       else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )123       {124 125       }126       else 127       {128        csOsName += _T( "Standard x64 Edition " );129       }130      }131      else132      {133       if( osvi.wSuiteMask & VER_SUITE_DATACENTER )134       {135        csOsName += _T( "Datacenter Edition " );136       }137       else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )138       {139        csOsName += _T( "Enterprise Edition " );140       }141       else if ( osvi.wSuiteMask & VER_SUITE_BLADE )142       {143        csOsName += _T( "Web Edition " );144       }145       else 146       {147        csOsName += _T( "Standard Edition " );148       }149      }150     }151     else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)152     {153      if( osvi.wSuiteMask & VER_SUITE_DATACENTER )154      {155       csOsName += _T( "Datacenter Server " );156      }157      else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )158      {159       csOsName += _T( "Advanced Server " );160      }161      else 162      {163       csOsName += _T( "Server " );164      }165     }166     else  // Windows NT 4.0 167     {168      if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )169      {170       csOsName += _T( "Server 4.0, Enterprise Edition " );171      }172      else 173      {174       csOsName += _T( "Server 4.0 " );175      }176     }177    }178   }179   // Test for specific product on Windows NT 4.0 SP5 and earlier180   else  181   {182    HKEY hKey;183    TCHAR szProductType[256];184    DWORD dwBufLen=256*sizeof(TCHAR);185    LONG lRet;186 187    lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,188     _T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"), 189     0, KEY_QUERY_VALUE, &hKey );190    if( lRet != ERROR_SUCCESS )191     return FALSE;192 193    lRet = RegQueryValueEx( hKey, TEXT("ProductType"),194     NULL, NULL, (LPBYTE) szProductType, &dwBufLen);195    RegCloseKey( hKey );196 197    if( (lRet != ERROR_SUCCESS) ||198     (dwBufLen > 256*sizeof(TCHAR)) )199     return FALSE;200 201    if ( lstrcmpi( TEXT("WINNT"), szProductType) == 0 )202    {203     csOsName += _T( "Workstation " );204    }205    if ( lstrcmpi( TEXT("LANMANNT"), szProductType) == 0 )206    {207     csOsName += _T( "Server " );208    }209    if ( lstrcmpi( TEXT("SERVERNT"), szProductType) == 0 )210    {211     csOsName += _T( "Advanced Server " );212    }213    CString cstmp;214    cstmp.Format( _T( "%d.%d " ), osvi.dwMajorVersion, osvi.dwMinorVersion );215    csOsName += cstmp;216   }217 218   // Display service pack (if any) and build number.219 220   if( osvi.dwMajorVersion == 4 && 221    lstrcmpi( osvi.szCSDVersion, TEXT("Service Pack 6") ) == 0 )222   { 223    HKEY hKey;224    LONG lRet;225 226    // Test for SP6 versus SP6a.227    lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,228     _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"), 229     0, KEY_QUERY_VALUE, &hKey );230    if( lRet == ERROR_SUCCESS )231    {232     CString cstmp;233     cstmp.Format( _T( "Service Pack 6a (Build %d)" ), osvi.dwBuildNumber & 0xFFFF );234     csOsName += cstmp;235    }        236    else // Windows NT 4.0 prior to SP6a237    {238     CString cstmp;239     cstmp.Format( _T( "%s (Build %d)" ), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF );240     csOsName += cstmp;241    }242    RegCloseKey( hKey );243   }244   else // not Windows NT 4.0 245   {246    CString cstmp;247    cstmp.Format( _T( "%s (Build %d)" ), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF );248    csOsName += cstmp;249   }250 251   break;252 253   // Test for the Windows Me/98/95.254  case VER_PLATFORM_WIN32_WINDOWS:255 256   if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)257   {258    csOsName += _T( "Microsoft Windows 95 " );259    if (osvi.szCSDVersion[1]=='C' || osvi.szCSDVersion[1]=='B')260    {261     csOsName += _T( "OSR2 " );262    }263   } 264 265   if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)266   {267    csOsName += _T("Microsoft Windows 98 ");268    if ( osvi.szCSDVersion[1]=='A' || osvi.szCSDVersion[1]=='B')269    {270     csOsName += _T("SE " );271    }272   } 273 274   if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)275   {276    csOsName += _T("Microsoft Windows Millennium Edition");277   } 278   break;279 280  case VER_PLATFORM_WIN32s:281 282   csOsName += _T("Microsoft Win32s\n");283   break;284  }285  return TRUE; 286 }

 

原创粉丝点击