1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_WindowsVersion_h
8 #define mozilla_WindowsVersion_h
9 
10 #include "mozilla/Attributes.h"
11 #include <stdint.h>
12 #include <windows.h>
13 
14 namespace mozilla {
15 
16 inline bool
IsWindowsVersionOrLater(uint32_t aVersion)17 IsWindowsVersionOrLater(uint32_t aVersion)
18 {
19   static uint32_t minVersion = 0;
20   static uint32_t maxVersion = UINT32_MAX;
21 
22   if (minVersion >= aVersion) {
23     return true;
24   }
25 
26   if (aVersion >= maxVersion) {
27     return false;
28   }
29 
30   OSVERSIONINFOEX info;
31   ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
32   info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
33   info.dwMajorVersion = aVersion >> 24;
34   info.dwMinorVersion = (aVersion >> 16) & 0xFF;
35   info.wServicePackMajor = (aVersion >> 8) & 0xFF;
36   info.wServicePackMinor = aVersion & 0xFF;
37 
38   DWORDLONG conditionMask = 0;
39   VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
40   VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
41   VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
42   VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);
43 
44   if (VerifyVersionInfo(&info,
45                         VER_MAJORVERSION | VER_MINORVERSION |
46                         VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
47                         conditionMask)) {
48     minVersion = aVersion;
49     return true;
50   }
51 
52   maxVersion = aVersion;
53   return false;
54 }
55 
56 inline bool
IsWindowsBuildOrLater(uint32_t aBuild)57 IsWindowsBuildOrLater(uint32_t aBuild)
58 {
59   static uint32_t minBuild = 0;
60   static uint32_t maxBuild = UINT32_MAX;
61 
62   if (minBuild >= aBuild) {
63     return true;
64   }
65 
66   if (aBuild >= maxBuild) {
67     return false;
68   }
69 
70   OSVERSIONINFOEX info;
71   ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
72   info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
73   info.dwBuildNumber = aBuild;
74 
75   DWORDLONG conditionMask = 0;
76   VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);
77 
78   if (VerifyVersionInfo(&info, VER_BUILDNUMBER, conditionMask)) {
79     minBuild = aBuild;
80     return true;
81   }
82 
83   maxBuild = aBuild;
84   return false;
85 }
86 
87 #if defined(_M_X64) || defined(_M_AMD64)
88 // We support only Win7 or later on Win64.
89 MOZ_ALWAYS_INLINE bool
IsXPSP3OrLater()90 IsXPSP3OrLater()
91 {
92   return true;
93 }
94 
95 MOZ_ALWAYS_INLINE bool
IsWin2003OrLater()96 IsWin2003OrLater()
97 {
98   return true;
99 }
100 
101 MOZ_ALWAYS_INLINE bool
IsWin2003SP2OrLater()102 IsWin2003SP2OrLater()
103 {
104   return true;
105 }
106 
107 MOZ_ALWAYS_INLINE bool
IsVistaOrLater()108 IsVistaOrLater()
109 {
110   return true;
111 }
112 
113 MOZ_ALWAYS_INLINE bool
IsVistaSP1OrLater()114 IsVistaSP1OrLater()
115 {
116   return true;
117 }
118 
119 MOZ_ALWAYS_INLINE bool
IsWin7OrLater()120 IsWin7OrLater()
121 {
122   return true;
123 }
124 #else
125 MOZ_ALWAYS_INLINE bool
IsXPSP3OrLater()126 IsXPSP3OrLater()
127 {
128   return IsWindowsVersionOrLater(0x05010300ul);
129 }
130 
131 MOZ_ALWAYS_INLINE bool
IsWin2003OrLater()132 IsWin2003OrLater()
133 {
134   return IsWindowsVersionOrLater(0x05020000ul);
135 }
136 
137 MOZ_ALWAYS_INLINE bool
IsWin2003SP2OrLater()138 IsWin2003SP2OrLater()
139 {
140   return IsWindowsVersionOrLater(0x05020200ul);
141 }
142 
143 MOZ_ALWAYS_INLINE bool
IsVistaOrLater()144 IsVistaOrLater()
145 {
146   return IsWindowsVersionOrLater(0x06000000ul);
147 }
148 
149 MOZ_ALWAYS_INLINE bool
IsVistaSP1OrLater()150 IsVistaSP1OrLater()
151 {
152   return IsWindowsVersionOrLater(0x06000100ul);
153 }
154 
155 MOZ_ALWAYS_INLINE bool
IsWin7OrLater()156 IsWin7OrLater()
157 {
158   return IsWindowsVersionOrLater(0x06010000ul);
159 }
160 #endif
161 
162 MOZ_ALWAYS_INLINE bool
IsWin7SP1OrLater()163 IsWin7SP1OrLater()
164 {
165   return IsWindowsVersionOrLater(0x06010100ul);
166 }
167 
168 MOZ_ALWAYS_INLINE bool
IsWin8OrLater()169 IsWin8OrLater()
170 {
171   return IsWindowsVersionOrLater(0x06020000ul);
172 }
173 
174 MOZ_ALWAYS_INLINE bool
IsWin10OrLater()175 IsWin10OrLater()
176 {
177   return IsWindowsVersionOrLater(0x0a000000ul);
178 }
179 
180 MOZ_ALWAYS_INLINE bool
IsNotWin7PreRTM()181 IsNotWin7PreRTM()
182 {
183   return IsWin7SP1OrLater() || !IsWin7OrLater() ||
184          IsWindowsBuildOrLater(7600);
185 }
186 
187 } // namespace mozilla
188 
189 #endif /* mozilla_WindowsVersion_h */
190