Getting the System Version(获取操作系统版本)

 http://msdn.microsoft.com/en-us/library/ms724429(VS.85).aspx

The following example uses the GetVersionEx, GetSystemMetrics, GetProductInfo, and GetNativeSystemInfo functions to determine the version information of the currently running operating system. If compatibility mode is in effect, the example displays the operating system selected for application compatibility. The example displays the information to the console.

To obtain the full version number for the operating system, call the GetFileVersionInfo function on one of the system DLLs, such as Kernel32.dll, then call VerQueryValue to obtain the //StringFileInfo//<lang><codepage>//ProductVersion subblock of the file version information.

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.

  1. include <windows.h>
  2. #include <tchar.h>
  3. #include <stdio.h>
  4. #include <strsafe.h>
  5. #define BUFSIZE 256
  6. typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
  7. typedef BOOL (WINAPI *PGPI)(DWORDDWORDDWORDDWORD, PDWORD);
  8. BOOL GetOSDisplayString( LPTSTR pszOS)
  9. {
  10.    OSVERSIONINFOEX osvi;
  11.    SYSTEM_INFO si;
  12.    PGNSI pGNSI;
  13.    PGPI pGPI;
  14.    BOOL bOsVersionInfoEx;
  15.    DWORD dwType;
  16.    ZeroMemory(&si, sizeof(SYSTEM_INFO));
  17.    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  18.    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  19.    if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
  20.       return 1;
  21.    // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
  22.    pGNSI = (PGNSI) GetProcAddress(
  23.       GetModuleHandle(TEXT("kernel32.dll")), 
  24.       "GetNativeSystemInfo");
  25.    if(NULL != pGNSI)
  26.       pGNSI(&si);
  27.    else GetSystemInfo(&si);
  28.    if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && 
  29.         osvi.dwMajorVersion > 4 )
  30.    {
  31.       StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));
  32.       // Test for the specific product.
  33.       if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 )
  34.       {
  35.          if( osvi.wProductType == VER_NT_WORKSTATION )
  36.              StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));
  37.          else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " ));
  38.          pGPI = (PGPI) GetProcAddress(
  39.             GetModuleHandle(TEXT("kernel32.dll")), 
  40.             "GetProductInfo");
  41.          pGPI( 6, 0, 0, 0, &dwType);
  42.          switch( dwType )
  43.          {
  44.             case PRODUCT_ULTIMATE:
  45.                StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" ));
  46.                break;
  47.             case PRODUCT_HOME_PREMIUM:
  48.                StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" ));
  49.                break;
  50.             case PRODUCT_HOME_BASIC:
  51.                StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" ));
  52.                break;
  53.             case PRODUCT_ENTERPRISE:
  54.                StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
  55.                break;
  56.             case PRODUCT_BUSINESS:
  57.                StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" ));
  58.                break;
  59.             case PRODUCT_STARTER:
  60.                StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" ));
  61.                break;
  62.             case PRODUCT_CLUSTER_SERVER:
  63.                StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" ));
  64.                break;
  65.             case PRODUCT_DATACENTER_SERVER:
  66.                StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" ));
  67.                break;
  68.             case PRODUCT_DATACENTER_SERVER_CORE:
  69.                StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" ));
  70.                break;
  71.             case PRODUCT_ENTERPRISE_SERVER:
  72.                StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
  73.                break;
  74.             case PRODUCT_ENTERPRISE_SERVER_CORE:
  75.                StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" ));
  76.                break;
  77.             case PRODUCT_ENTERPRISE_SERVER_IA64:
  78.                StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" ));
  79.                break;
  80.             case PRODUCT_SMALLBUSINESS_SERVER:
  81.                StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" ));
  82.                break;
  83.             case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  84.                StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" ));
  85.                break;
  86.             case PRODUCT_STANDARD_SERVER:
  87.                StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" ));
  88.                break;
  89.             case PRODUCT_STANDARD_SERVER_CORE:
  90.                StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" ));
  91.                break;
  92.             case PRODUCT_WEB_SERVER:
  93.                StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" ));
  94.                break;
  95.          }
  96.          if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
  97.             StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" ));
  98.          else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
  99.             StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
  100.       }
  101.       if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
  102.       {
  103.          if( GetSystemMetrics(SM_SERVERR2) )
  104.             StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, "));
  105.          else if ( osvi.wSuiteMask==VER_SUITE_STORAGE_SERVER )
  106.             StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003"));
  107.          else if ( osvi.wSuiteMask==VER_SUITE_WH_SERVER )
  108.             StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Home Server"));
  109.          else if( osvi.wProductType == VER_NT_WORKSTATION &
  110.                   si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
  111.          {
  112.             StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition"));
  113.          }
  114.          else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, "));
  115.          // Test for the server type.
  116.          if ( osvi.wProductType != VER_NT_WORKSTATION )
  117.          {
  118.             if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
  119.             {
  120.                 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
  121.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" ));
  122.                 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
  123.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" ));
  124.             }
  125.             else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
  126.             {
  127.                 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
  128.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" ));
  129.                 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
  130.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" ));
  131.                 else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" ));
  132.             }
  133.             else
  134.             {
  135.                 if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
  136.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" ));
  137.                 else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
  138.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" ));
  139.                 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
  140.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" ));
  141.                 else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
  142.                    StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" ));
  143.                 else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" ));
  144.             }
  145.          }
  146.       }
  147.       if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
  148.       {
  149.          StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
  150.          if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
  151.             StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" ));
  152.          else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
  153.       }
  154.       if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
  155.       {
  156.          StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));
  157.          if ( osvi.wProductType == VER_NT_WORKSTATION )
  158.          {
  159.             StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
  160.          }
  161.          else 
  162.          {
  163.             if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
  164.                StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" ));
  165.             else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
  166.                StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" ));
  167.             else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" ));
  168.          }
  169.       }
  170.        // Include service pack (if any) and build number.
  171.       if( _tcslen(osvi.szCSDVersion) > 0 )
  172.       {
  173.           StringCchCat(pszOS, BUFSIZE, TEXT(" ") );
  174.           StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
  175.       }
  176.       TCHAR buf[80];
  177.       StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
  178.       StringCchCat(pszOS, BUFSIZE, buf);
  179.       return TRUE; 
  180.    }
  181.    else
  182.    {  
  183.       printf( "This sample does not support this version of Windows./n");
  184.       return FALSE;
  185.    }
  186. }
  187. int __cdecl _tmain()
  188. {
  189.     TCHAR szOS[BUFSIZE];
  190.     if( GetOSDisplayString( szOS ) )
  191.         _tprintf( TEXT("/n%s/n"), szOS );
  192. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值