码迷,mamicode.com
首页 > Windows程序 > 详细

Windows 系统版本判断

时间:2016-10-08 19:35:21      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

Windows 系统版本判断

博客分类:
 

很多情况下,需要软件判断操作系统,其实网上写的都很少,我希望这篇文章能给大家帮助

首先我们要在.h中定义下面的东西

Cpp代码  技术分享
  1. #define PRODUCT_UNDEFINED                       0x00000000  
  2.   
  3. #define PRODUCT_ULTIMATE                        0x00000001  
  4. #define PRODUCT_HOME_BASIC                      0x00000002  
  5. #define PRODUCT_HOME_PREMIUM                    0x00000003  
  6. #define PRODUCT_ENTERPRISE                      0x00000004  
  7. #define PRODUCT_HOME_BASIC_N                    0x00000005  
  8. #define PRODUCT_BUSINESS                        0x00000006  
  9. #define PRODUCT_STANDARD_SERVER                 0x00000007  
  10. #define PRODUCT_DATACENTER_SERVER               0x00000008  
  11. #define PRODUCT_SMALLBUSINESS_SERVER            0x00000009  
  12. #define PRODUCT_ENTERPRISE_SERVER               0x0000000A  
  13. #define PRODUCT_STARTER                         0x0000000B  
  14. #define PRODUCT_DATACENTER_SERVER_CORE          0x0000000C  
  15. #define PRODUCT_STANDARD_SERVER_CORE            0x0000000D  
  16. #define PRODUCT_ENTERPRISE_SERVER_CORE          0x0000000E  
  17. #define PRODUCT_ENTERPRISE_SERVER_IA64          0x0000000F  
  18. #define PRODUCT_BUSINESS_N                      0x00000010  
  19. #define PRODUCT_WEB_SERVER                      0x00000011  
  20. #define PRODUCT_CLUSTER_SERVER                  0x00000012  
  21. #define PRODUCT_HOME_SERVER                     0x00000013  
  22. #define PRODUCT_STORAGE_EXPRESS_SERVER          0x00000014  
  23. #define PRODUCT_STORAGE_STANDARD_SERVER         0x00000015  
  24. #define PRODUCT_STORAGE_WORKGROUP_SERVER        0x00000016  
  25. #define PRODUCT_STORAGE_ENTERPRISE_SERVER       0x00000017  
  26. #define PRODUCT_SERVER_FOR_SMALLBUSINESS        0x00000018  
  27. #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM    0x00000019  
  28.   
  29. #define PRODUCT_UNLICENSED                      0xABCDABCD  

 

好了,这是必须的定义,为了就是判断操作系统

 

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

 

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)(DWORD, DWORD, DWORD, DWORD, 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 Other
Windows 8 6.2 6 2 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2012 6.2 6 2 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
Windows 7 6.1 6 1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2008 R2 6.1 6 1 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
Windows Server 2008 6.0 6 0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
Windows Vista 6.0 6 0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2003 R2 5.2 5 2 GetSystemMetrics(SM_SERVERR2) != 0
Windows Home Server 5.2 5 2 OSVERSIONINFOEX.wSuiteMask & VER_SUITE_WH_SERVER
Windows Server 2003 5.2 5 2 GetSystemMetrics(SM_SERVERR2) == 0
Windows XP Professional x64 Edition 5.2 5 2 (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
Windows XP 5.1 5 1 Not applicable
Windows 2000 5.0 5 0 Not applicable

 

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

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

http://msdn.microsoft.com/ja-jp/library/windows/desktop/ms724833(v=vs.85).aspx

http://www.usefullcode.net/2006/12/windows_vista_2.html

Windows 系统版本判断

标签:

原文地址:http://www.cnblogs.com/micro-chen/p/5939662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!