WindowsVersion.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_WindowsVersion_h
  6. #define mozilla_WindowsVersion_h
  7. #include "mozilla/Attributes.h"
  8. #include <stdint.h>
  9. #include <windows.h>
  10. namespace mozilla {
  11. inline bool
  12. IsWindowsVersionOrLater(uint32_t aVersion)
  13. {
  14. static uint32_t minVersion = 0;
  15. static uint32_t maxVersion = UINT32_MAX;
  16. if (minVersion >= aVersion) {
  17. return true;
  18. }
  19. if (aVersion >= maxVersion) {
  20. return false;
  21. }
  22. OSVERSIONINFOEX info;
  23. ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
  24. info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  25. info.dwMajorVersion = aVersion >> 24;
  26. info.dwMinorVersion = (aVersion >> 16) & 0xFF;
  27. info.wServicePackMajor = (aVersion >> 8) & 0xFF;
  28. info.wServicePackMinor = aVersion & 0xFF;
  29. DWORDLONG conditionMask = 0;
  30. VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
  31. VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
  32. VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  33. VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);
  34. if (VerifyVersionInfo(&info,
  35. VER_MAJORVERSION | VER_MINORVERSION |
  36. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
  37. conditionMask)) {
  38. minVersion = aVersion;
  39. return true;
  40. }
  41. maxVersion = aVersion;
  42. return false;
  43. }
  44. inline bool
  45. IsWindowsBuildOrLater(uint32_t aBuild)
  46. {
  47. static uint32_t minBuild = 0;
  48. static uint32_t maxBuild = UINT32_MAX;
  49. if (minBuild >= aBuild) {
  50. return true;
  51. }
  52. if (aBuild >= maxBuild) {
  53. return false;
  54. }
  55. OSVERSIONINFOEX info;
  56. ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
  57. info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  58. info.dwBuildNumber = aBuild;
  59. DWORDLONG conditionMask = 0;
  60. VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);
  61. if (VerifyVersionInfo(&info, VER_BUILDNUMBER, conditionMask)) {
  62. minBuild = aBuild;
  63. return true;
  64. }
  65. maxBuild = aBuild;
  66. return false;
  67. }
  68. // Although many of the older versions are not supported, we should keep
  69. // these entries for completeness (since they don't take any resources of
  70. // note), and to cater to corner cases for applications running e.g. in
  71. // Windows' compatibility mode.
  72. MOZ_ALWAYS_INLINE bool
  73. IsXPSP3OrLater()
  74. {
  75. return IsWindowsVersionOrLater(0x05010300ul);
  76. }
  77. MOZ_ALWAYS_INLINE bool
  78. IsWin2003OrLater()
  79. {
  80. return IsWindowsVersionOrLater(0x05020000ul);
  81. }
  82. MOZ_ALWAYS_INLINE bool
  83. IsWin2003SP2OrLater()
  84. {
  85. return IsWindowsVersionOrLater(0x05020200ul);
  86. }
  87. MOZ_ALWAYS_INLINE bool
  88. IsVistaOrLater()
  89. {
  90. return IsWindowsVersionOrLater(0x06000000ul);
  91. }
  92. MOZ_ALWAYS_INLINE bool
  93. IsVistaSP1OrLater()
  94. {
  95. return IsWindowsVersionOrLater(0x06000100ul);
  96. }
  97. MOZ_ALWAYS_INLINE bool
  98. IsWin7OrLater()
  99. {
  100. return IsWindowsVersionOrLater(0x06010000ul);
  101. }
  102. MOZ_ALWAYS_INLINE bool
  103. IsWin7SP1OrLater()
  104. {
  105. return IsWindowsVersionOrLater(0x06010100ul);
  106. }
  107. MOZ_ALWAYS_INLINE bool
  108. IsWin8OrLater()
  109. {
  110. return IsWindowsVersionOrLater(0x06020000ul);
  111. }
  112. MOZ_ALWAYS_INLINE bool
  113. IsWin8Point1OrLater()
  114. {
  115. return IsWindowsVersionOrLater(0x06030000ul);
  116. }
  117. MOZ_ALWAYS_INLINE bool
  118. IsWin10OrLater()
  119. {
  120. return IsWindowsVersionOrLater(0x0a000000ul);
  121. }
  122. MOZ_ALWAYS_INLINE bool
  123. IsNotWin7PreRTM()
  124. {
  125. return IsWin7SP1OrLater() || !IsWin7OrLater() ||
  126. IsWindowsBuildOrLater(7600);
  127. }
  128. } // namespace mozilla
  129. #endif /* mozilla_WindowsVersion_h */