version_win32.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2016 - 2020, Steve Holme, <steve_holme@hotmail.com>.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(WIN32)
  24. #include <curl/curl.h>
  25. #include "version_win32.h"
  26. /* The last #include files should be: */
  27. #include "curl_memory.h"
  28. #include "memdebug.h"
  29. /*
  30. * curlx_verify_windows_version()
  31. *
  32. * This is used to verify if we are running on a specific windows version.
  33. *
  34. * Parameters:
  35. *
  36. * majorVersion [in] - The major version number.
  37. * minorVersion [in] - The minor version number.
  38. * platform [in] - The optional platform identifier.
  39. * condition [in] - The test condition used to specifier whether we are
  40. * checking a version less then, equal to or greater than
  41. * what is specified in the major and minor version
  42. * numbers.
  43. *
  44. * Returns TRUE if matched; otherwise FALSE.
  45. */
  46. bool curlx_verify_windows_version(const unsigned int majorVersion,
  47. const unsigned int minorVersion,
  48. const PlatformIdentifier platform,
  49. const VersionCondition condition)
  50. {
  51. bool matched = FALSE;
  52. #if defined(CURL_WINDOWS_APP)
  53. /* We have no way to determine the Windows version from Windows apps,
  54. so let's assume we're running on the target Windows version. */
  55. const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
  56. const WORD targetVersion = (WORD)_WIN32_WINNT;
  57. switch(condition) {
  58. case VERSION_LESS_THAN:
  59. matched = targetVersion < fullVersion;
  60. break;
  61. case VERSION_LESS_THAN_EQUAL:
  62. matched = targetVersion <= fullVersion;
  63. break;
  64. case VERSION_EQUAL:
  65. matched = targetVersion == fullVersion;
  66. break;
  67. case VERSION_GREATER_THAN_EQUAL:
  68. matched = targetVersion >= fullVersion;
  69. break;
  70. case VERSION_GREATER_THAN:
  71. matched = targetVersion > fullVersion;
  72. break;
  73. }
  74. if(matched && (platform == PLATFORM_WINDOWS)) {
  75. /* we're always running on PLATFORM_WINNT */
  76. matched = FALSE;
  77. }
  78. #elif !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
  79. (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
  80. OSVERSIONINFO osver;
  81. memset(&osver, 0, sizeof(osver));
  82. osver.dwOSVersionInfoSize = sizeof(osver);
  83. /* Find out Windows version */
  84. if(GetVersionEx(&osver)) {
  85. /* Verify the Operating System version number */
  86. switch(condition) {
  87. case VERSION_LESS_THAN:
  88. if(osver.dwMajorVersion < majorVersion ||
  89. (osver.dwMajorVersion == majorVersion &&
  90. osver.dwMinorVersion < minorVersion))
  91. matched = TRUE;
  92. break;
  93. case VERSION_LESS_THAN_EQUAL:
  94. if(osver.dwMajorVersion < majorVersion ||
  95. (osver.dwMajorVersion == majorVersion &&
  96. osver.dwMinorVersion <= minorVersion))
  97. matched = TRUE;
  98. break;
  99. case VERSION_EQUAL:
  100. if(osver.dwMajorVersion == majorVersion &&
  101. osver.dwMinorVersion == minorVersion)
  102. matched = TRUE;
  103. break;
  104. case VERSION_GREATER_THAN_EQUAL:
  105. if(osver.dwMajorVersion > majorVersion ||
  106. (osver.dwMajorVersion == majorVersion &&
  107. osver.dwMinorVersion >= minorVersion))
  108. matched = TRUE;
  109. break;
  110. case VERSION_GREATER_THAN:
  111. if(osver.dwMajorVersion > majorVersion ||
  112. (osver.dwMajorVersion == majorVersion &&
  113. osver.dwMinorVersion > minorVersion))
  114. matched = TRUE;
  115. break;
  116. }
  117. /* Verify the platform identifier (if necessary) */
  118. if(matched) {
  119. switch(platform) {
  120. case PLATFORM_WINDOWS:
  121. if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  122. matched = FALSE;
  123. break;
  124. case PLATFORM_WINNT:
  125. if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
  126. matched = FALSE;
  127. default: /* like platform == PLATFORM_DONT_CARE */
  128. break;
  129. }
  130. }
  131. }
  132. #else
  133. ULONGLONG cm = 0;
  134. OSVERSIONINFOEX osver;
  135. BYTE majorCondition;
  136. BYTE minorCondition;
  137. BYTE spMajorCondition;
  138. BYTE spMinorCondition;
  139. switch(condition) {
  140. case VERSION_LESS_THAN:
  141. majorCondition = VER_LESS;
  142. minorCondition = VER_LESS;
  143. spMajorCondition = VER_LESS_EQUAL;
  144. spMinorCondition = VER_LESS_EQUAL;
  145. break;
  146. case VERSION_LESS_THAN_EQUAL:
  147. majorCondition = VER_LESS_EQUAL;
  148. minorCondition = VER_LESS_EQUAL;
  149. spMajorCondition = VER_LESS_EQUAL;
  150. spMinorCondition = VER_LESS_EQUAL;
  151. break;
  152. case VERSION_EQUAL:
  153. majorCondition = VER_EQUAL;
  154. minorCondition = VER_EQUAL;
  155. spMajorCondition = VER_GREATER_EQUAL;
  156. spMinorCondition = VER_GREATER_EQUAL;
  157. break;
  158. case VERSION_GREATER_THAN_EQUAL:
  159. majorCondition = VER_GREATER_EQUAL;
  160. minorCondition = VER_GREATER_EQUAL;
  161. spMajorCondition = VER_GREATER_EQUAL;
  162. spMinorCondition = VER_GREATER_EQUAL;
  163. break;
  164. case VERSION_GREATER_THAN:
  165. majorCondition = VER_GREATER;
  166. minorCondition = VER_GREATER;
  167. spMajorCondition = VER_GREATER_EQUAL;
  168. spMinorCondition = VER_GREATER_EQUAL;
  169. break;
  170. default:
  171. return FALSE;
  172. }
  173. memset(&osver, 0, sizeof(osver));
  174. osver.dwOSVersionInfoSize = sizeof(osver);
  175. osver.dwMajorVersion = majorVersion;
  176. osver.dwMinorVersion = minorVersion;
  177. if(platform == PLATFORM_WINDOWS)
  178. osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
  179. else if(platform == PLATFORM_WINNT)
  180. osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
  181. cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
  182. cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
  183. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
  184. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
  185. if(platform != PLATFORM_DONT_CARE)
  186. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  187. if(VerifyVersionInfo(&osver, (VER_MAJORVERSION | VER_MINORVERSION |
  188. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR),
  189. cm))
  190. matched = TRUE;
  191. #endif
  192. return matched;
  193. }
  194. #endif /* WIN32 */