Windows 系统版本判断

时间:2021-08-20 18:59:20

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

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

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

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

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

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

参照:

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