VersionHelpers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // 必须与系统的宏相同,否则使用VS2高版本时会出现重命名;
  2. #ifndef _versionhelpers_H_INCLUDED_
  3. #define _versionhelpers_H_INCLUDED_
  4. #ifdef _MSC_VER
  5. #pragma once
  6. #endif // _MSC_VER
  7. #pragma region Application Family
  8. #if (NTDDI_VERSION >= NTDDI_WINXP)
  9. #ifdef __cplusplus
  10. #define VERSIONHELPERAPI inline bool
  11. #else // __cplusplus
  12. #define VERSIONHELPERAPI FORCEINLINE BOOL
  13. #endif // __cplusplus
  14. #define _WIN32_WINNT_NT4 0x0400
  15. #define _WIN32_WINNT_WIN2K 0x0500 // windows 2000
  16. #define _WIN32_WINNT_WINXP 0x0501 // windows xp
  17. #define _WIN32_WINNT_WS03 0x0502 // widnows xp sp3
  18. #define _WIN32_WINNT_WIN6 0x0600 //
  19. #define _WIN32_WINNT_VISTA 0x0600 // windows vista
  20. #define _WIN32_WINNT_WS08 0x0600 // windows server 2008
  21. #define _WIN32_WINNT_LONGHORN 0x0600 //
  22. #define _WIN32_WINNT_WIN7 0x0601 // windows 7
  23. #define _WIN32_WINNT_WIN8 0x0602 // windows 8
  24. #define _WIN32_WINNT_WINBLUE 0x0603 //
  25. #define _WIN32_WINNT_WINTHRESHOLD 0x0A00 /* ABRACADABRA_THRESHOLD*/
  26. #define _WIN32_WINNT_WIN10 0x0A00 /* ABRACADABRA_THRESHOLD*/
  27. VERSIONHELPERAPI IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
  28. {
  29. OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0 };
  30. DWORDLONG const dwlConditionMask = VerSetConditionMask(
  31. VerSetConditionMask(
  32. VerSetConditionMask(
  33. 0, VER_MAJORVERSION, VER_GREATER_EQUAL),
  34. VER_MINORVERSION, VER_GREATER_EQUAL),
  35. VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  36. osvi.dwMajorVersion = wMajorVersion;
  37. osvi.dwMinorVersion = wMinorVersion;
  38. osvi.wServicePackMajor = wServicePackMajor;
  39. // 注意:VerifyVersionInfoW只适用于Win 8以下操作系统。Win8以上该函数无效,需要加上特殊清单文件;
  40. return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
  41. }
  42. VERSIONHELPERAPI IsWindowsXPOrGreater()
  43. {
  44. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 0);
  45. }
  46. VERSIONHELPERAPI IsWindowsXPSP1OrGreater()
  47. {
  48. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 1);
  49. }
  50. VERSIONHELPERAPI IsWindowsXPSP2OrGreater()
  51. {
  52. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 2);
  53. }
  54. VERSIONHELPERAPI IsWindowsXPSP3OrGreater()
  55. {
  56. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 3);
  57. }
  58. VERSIONHELPERAPI IsWindowsVistaOrGreater()
  59. {
  60. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
  61. }
  62. VERSIONHELPERAPI IsWindowsVistaSP1OrGreater()
  63. {
  64. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 1);
  65. }
  66. VERSIONHELPERAPI IsWindowsVistaSP2OrGreater()
  67. {
  68. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 2);
  69. }
  70. VERSIONHELPERAPI IsWindows7OrGreater()
  71. {
  72. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0);
  73. }
  74. VERSIONHELPERAPI IsWindows7SP1OrGreater()
  75. {
  76. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 1);
  77. }
  78. VERSIONHELPERAPI IsWindows8OrGreater()
  79. {
  80. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
  81. }
  82. VERSIONHELPERAPI IsWindows8Point1OrGreater()
  83. {
  84. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINBLUE), LOBYTE(_WIN32_WINNT_WINBLUE), 0);
  85. }
  86. VERSIONHELPERAPI IsWindowsThresholdOrGreater()
  87. {
  88. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINTHRESHOLD), LOBYTE(_WIN32_WINNT_WINTHRESHOLD), 0);
  89. }
  90. VERSIONHELPERAPI IsWindows10OrGreater()
  91. {
  92. return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINTHRESHOLD), LOBYTE(_WIN32_WINNT_WINTHRESHOLD), 0);
  93. }
  94. VERSIONHELPERAPI IsWindowsServer()
  95. {
  96. OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0, 0, VER_NT_WORKSTATION };
  97. DWORDLONG const dwlConditionMask = VerSetConditionMask( 0, VER_PRODUCT_TYPE, VER_EQUAL );
  98. return !VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlConditionMask);
  99. }
  100. /************************************************************************/
  101. /* 函数:[9/13/2016 IT];
  102. /* 描述:;
  103. /* 参数:;
  104. /* [IN] :;
  105. /* [OUT] :;
  106. /* [IN/OUT] :;
  107. /* 返回:void;
  108. /* 注意:;
  109. /* 示例:;
  110. /*
  111. /* 修改:;
  112. /* 日期:;
  113. /* 内容:;
  114. /************************************************************************/
  115. VERSIONHELPERAPI IsWow64()
  116. {
  117. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  118. LPFN_ISWOW64PROCESS fnIsWow64Process;
  119. BOOL bIsWow64 = FALSE;
  120. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  121. if (NULL != fnIsWow64Process)
  122. {
  123. if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
  124. {
  125. // 句柄错误,则系统不是64位;
  126. //AfxMessageBox(_T("句柄错误"));
  127. }
  128. }
  129. return bIsWow64;
  130. }
  131. /************************************************************************/
  132. /* 函数:[9/13/2016 IT];
  133. /* 描述:;
  134. /* 参数:;
  135. /* [IN] :;
  136. /* [OUT] :;
  137. /* [IN/OUT] :;
  138. /* 返回:void;
  139. /* 注意:;
  140. /* 示例:;
  141. /*
  142. /* 修改:;
  143. /* 日期:;
  144. /* 内容:;
  145. /************************************************************************/
  146. VERSIONHELPERAPI IsOSVerIs64()
  147. {
  148. typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
  149. typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
  150. BOOL bOSVerIs64 = FALSE;
  151. SYSTEM_INFO si;
  152. PGNSI pGNSI;
  153. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  154. if (NULL != pGNSI)
  155. pGNSI(&si);
  156. else
  157. GetSystemInfo(&si);
  158. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  159. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  160. {
  161. bOSVerIs64 = TRUE;
  162. }
  163. return bOSVerIs64;
  164. }
  165. /************************************************************************/
  166. /* 函数:[9/13/2016 IT];
  167. /* 描述:;
  168. /* 参数:;
  169. /* [IN] :;
  170. /* [OUT] :;
  171. /* [IN/OUT] :;
  172. /* 返回:void;
  173. /* 注意:;
  174. /* 示例:;
  175. /*
  176. /* 修改:;
  177. /* 日期:;
  178. /* 内容:;
  179. /************************************************************************/
  180. VERSIONHELPERAPI IsWow64Process(HANDLE hprocess)
  181. {
  182. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  183. LPFN_ISWOW64PROCESS fnIsWow64Process;
  184. INT nRet = 0;
  185. BOOL bIsWow64 = FALSE;
  186. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  187. if (NULL != fnIsWow64Process)
  188. {
  189. if (!fnIsWow64Process(hprocess, &bIsWow64))
  190. {
  191. // 句柄错误;
  192. //AfxMessageBox(_T("句柄错误"));
  193. //DWORD dwError = GetLastError();
  194. return false;
  195. }
  196. }
  197. return bIsWow64;
  198. }
  199. /************************************************************************/
  200. /* 函数:IsWinVerEqualTo[5/3/2017 Jeff];
  201. /* 描述:判断是否=某个特定的系统版本;
  202. /* 参数:;
  203. /* [IN] :;
  204. /* [OUT] :;
  205. /* [IN/OUT] :;
  206. /* 返回:void;
  207. /* 注意:;
  208. /* 示例:;
  209. /*
  210. /* 修改:;
  211. /* 日期:;
  212. /* 内容:;
  213. /************************************************************************/
  214. VERSIONHELPERAPI IsWinVerEqualTo(DWORD dwMajorVersion, DWORD dwMinorVersion)
  215. {
  216. OSVERSIONINFOEXW osvi = {0};
  217. DWORDLONG dwlConditionMask = 0;
  218. // 1、初始化系统版本信息数据结构
  219. ZeroMemory(&osvi, sizeof(osvi));
  220. osvi.dwOSVersionInfoSize= sizeof(osvi);
  221. osvi.dwMajorVersion= dwMajorVersion;
  222. osvi.dwMinorVersion= dwMinorVersion;
  223. // 2、初始化条件掩码
  224. VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
  225. VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);
  226. return ::VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask);
  227. }
  228. /************************************************************************/
  229. /* 函数:GetNtVersionNumbers[5/3/2017 Jeff];
  230. /* 描述:调用RtlGetNtVersionNumbers获取系统版本信息;
  231. /* 参数:;
  232. /* [IN] :;
  233. /* [OUT] :;
  234. /* [IN/OUT] :;
  235. /* 返回:void;
  236. /* 注意:;
  237. /* 示例:;
  238. /*
  239. /* 修改:;
  240. /* 日期:;
  241. /* 内容:;
  242. /************************************************************************/
  243. VERSIONHELPERAPI GetNtVersionNumbers(DWORD dwMajorVer, DWORD dwMinorVer, DWORD dwBuildNumber)
  244. {
  245. BOOL bRet= FALSE;
  246. HMODULE hModNtdll= NULL;
  247. if (hModNtdll= ::LoadLibrary(_T("ntdll.dll")))
  248. {
  249. typedef void (WINAPI *pfRTLGETNTVERSIONNUMBERS)(DWORD*,DWORD*, DWORD*);
  250. pfRTLGETNTVERSIONNUMBERS pfRtlGetNtVersionNumbers;
  251. pfRtlGetNtVersionNumbers = (pfRTLGETNTVERSIONNUMBERS)::GetProcAddress(hModNtdll, "RtlGetNtVersionNumbers");
  252. if (pfRtlGetNtVersionNumbers)
  253. {
  254. pfRtlGetNtVersionNumbers(&dwMajorVer, &dwMinorVer,&dwBuildNumber);
  255. dwBuildNumber&= 0x0ffff;
  256. bRet = TRUE;
  257. }
  258. ::FreeLibrary(hModNtdll);
  259. hModNtdll = NULL;
  260. }
  261. return bRet;
  262. }
  263. void test()
  264. {
  265. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  266. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  267. IsWindowsXPOrGreater() ? _tprintf(_T("是IsWindowsXPOrGreater\n")) : _tprintf(_T("不是IsWindowsXPOrGreater\n"));
  268. IsWindowsXPSP1OrGreater() ? _tprintf(_T("是IsWindowsXPSP1OrGreater\n")) : _tprintf(_T("不是IsWindowsXPSP1OrGreater\n"));
  269. IsWindowsXPSP2OrGreater() ? _tprintf(_T("是IsWindowsXPSP2OrGreater\n")) : _tprintf(_T("不是IsWindowsXPSP2OrGreater\n"));
  270. IsWindowsXPSP3OrGreater() ? _tprintf(_T("是IsWindowsXPSP3OrGreater\n")) : _tprintf(_T("不是IsWindowsXPSP3OrGreater\n"));
  271. IsWindowsVistaOrGreater() ? _tprintf(_T("是IsWindowsVistaOrGreater\n")) : _tprintf(_T("不是IsWindowsVistaOrGreater\n"));
  272. IsWindowsVistaSP1OrGreater() ? _tprintf(_T("是IsWindowsVistaSP1OrGreater\n")) : _tprintf(_T("不是IsWindowsVistaSP1OrGreater\n"));
  273. IsWindowsVistaSP2OrGreater() ? _tprintf(_T("是IsWindowsVistaSP2OrGreater\n")) : _tprintf(_T("不是IsWindowsVistaSP2OrGreater\n"));
  274. IsWindows7OrGreater() ? _tprintf(_T("是IsWindows7OrGreater\n")) : _tprintf(_T("不是IsWindows7OrGreater\n"));
  275. IsWindows7SP1OrGreater() ? _tprintf(_T("是IsWindows7SP1OrGreater\n")) : _tprintf(_T("不是IsWindows7SP1OrGreater\n"));
  276. IsWindows8OrGreater() ? _tprintf(_T("是IsWindows8OrGreater\n")) : _tprintf(_T("不是IsWindows8OrGreater\n"));
  277. IsWindows8Point1OrGreater() ? _tprintf(_T("是IsWindows8Point1OrGreater\n")) : _tprintf(_T("不是IsWindows8Point1OrGreater\n"));
  278. IsWindowsThresholdOrGreater() ? _tprintf(_T("是IsWindowsThresholdOrGreater\n")) : _tprintf(_T("不是IsWindowsThresholdOrGreater\n"));
  279. IsWindows10OrGreater() ? _tprintf(_T("是IsWindows10OrGreater\n")) : _tprintf(_T("不是IsWindows10OrGreater\n"));
  280. IsWindowsServer() ? _tprintf(_T("是IsWindowsServer\n")) : _tprintf(_T("不是IsWindowsServer\n"));
  281. IsWinVerEqualTo(10, 1) ? _tprintf(_T("是IsWinVerEqualTo\n")) : _tprintf(_T("不是IsWinVerEqualTo\n"));
  282. GetNtVersionNumbers(10, 0, 14393) ? _tprintf(_T("是GetNtVersionNumbers\n")) : _tprintf(_T("不是GetNtVersionNumbers\n"));
  283. if (IsWinVerEqualTo(5, 0))
  284. _tprintf(_T("是 Windows 2000\n"));
  285. if ( IsWinVerEqualTo(5, 1) )
  286. _tprintf(_T("是 Windows XP\n"));
  287. if ( IsWinVerEqualTo(5, 2) )
  288. {
  289. IsWindowsServer() ? _tprintf(_T("是 Windows Server 2003 或 Windows Server 2003 R2\n")) : _tprintf(_T("是 Windows XP 64-Bit Edition\n"));
  290. }
  291. if ( IsWinVerEqualTo(6, 0) )
  292. {
  293. IsWindowsServer() ? _tprintf(_T("是 Windows Server 2008\n")) : _tprintf(_T("是 Windows Vista\n"));
  294. }
  295. if ( IsWinVerEqualTo(6, 1) )
  296. {
  297. IsWindowsServer() ? _tprintf(_T("是 Windows Server 2008 R2\n")) : _tprintf(_T("是 Windows 7\n"));
  298. }
  299. if ( IsWinVerEqualTo(6, 2) )
  300. {
  301. IsWindowsServer() ? _tprintf(_T("是 Windows Server 2012\n")) : _tprintf(_T("是 Windows 8\n"));
  302. }
  303. if ( IsWinVerEqualTo(6, 3) )
  304. {
  305. IsWindowsServer() ? _tprintf(_T("是 Windows Server 2012 R2\n")) : _tprintf(_T("是 Windows 8.1\n"));
  306. }
  307. if ( IsWinVerEqualTo(10, 0) )
  308. {
  309. IsWindowsServer() ? _tprintf(_T("是 Windows Server 2016\n")) : _tprintf(_T("是 Windows 10\n"));
  310. }
  311. IsWow64() ? _tprintf(_T("函数:IsWow64验证过程:是64位\n")) : _tprintf(_T("函数:IsWow64验证过程:是32位\n"));
  312. IsOSVerIs64() ? _tprintf(_T("函数:IsOSVerIs64验证过程:是64位\n")) : _tprintf(_T("函数:IsOSVerIs64验证过程:是32位\n"));
  313. _tsetlocale(LC_CTYPE, old_locale);
  314. free(old_locale);//还原区域设定;
  315. }
  316. #endif // NTDDI_VERSION
  317. #pragma endregion
  318. #endif // _VERSIONHELPERS_H_INCLUDED_