Windows 系统版本判断

来源:互联网 发布:直播源码 诱导充值 编辑:程序博客网 时间:2024/05/22 07:53

下面是判断操作系统的代码

 

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

 

参照:

Operating system Version number dwMajorVersiondwMinorVersion OtherWindows 86.262OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATIONWindows Server 20126.262OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATIONWindows 76.161OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATIONWindows Server 2008 R26.161OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATIONWindows Server 20086.060OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATIONWindows Vista6.060OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATIONWindows Server 2003 R25.252GetSystemMetrics(SM_SERVERR2) != 0Windows Home Server5.252OSVERSIONINFOEX.wSuiteMask & VER_SUITE_WH_SERVERWindows Server 20035.252GetSystemMetrics(SM_SERVERR2) == 0Windows XP Professional x64 Edition5.252(OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)Windows XP5.151Not applicableWindows 20005.050Not applicable

 

好了,还有就是要提供出处了:

原创粉丝点击