QtBase  v6.3.1
qoperatingsystemversion_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
41 
43 
44 #include <qt_windows.h>
45 #include <qbytearray.h>
46 
48 
49 static inline OSVERSIONINFOEX determineWinOsVersion()
50 {
51  OSVERSIONINFOEX result = { sizeof(OSVERSIONINFOEX), 0, 0, 0, 0, {'\0'}, 0, 0, 0, 0, 0};
52 
53  HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
54  if (Q_UNLIKELY(!ntdll))
55  return result;
56 
57  typedef NTSTATUS (NTAPI *RtlGetVersionFunction)(LPOSVERSIONINFO);
58 
59  // RtlGetVersion is documented public API but we must load it dynamically
60  // because linking to it at load time will not pass the Windows App Certification Kit
61  // https://msdn.microsoft.com/en-us/library/windows/hardware/ff561910.aspx
62  RtlGetVersionFunction pRtlGetVersion = reinterpret_cast<RtlGetVersionFunction>(
63  reinterpret_cast<QFunctionPointer>(GetProcAddress(ntdll, "RtlGetVersion")));
64  if (Q_UNLIKELY(!pRtlGetVersion))
65  return result;
66 
67  // GetVersionEx() has been deprecated in Windows 8.1 and will return
68  // only Windows 8 from that version on, so use the kernel API function.
69  pRtlGetVersion(reinterpret_cast<LPOSVERSIONINFO>(&result)); // always returns STATUS_SUCCESS
70  return result;
71 }
72 
73 OSVERSIONINFOEX qWindowsVersionInfo()
74 {
75  OSVERSIONINFOEX realResult = determineWinOsVersion();
76 #ifdef QT_DEBUG
77  {
78  if (Q_UNLIKELY(qEnvironmentVariableIsSet("QT_WINVER_OVERRIDE"))) {
79  OSVERSIONINFOEX result = realResult;
80  result.dwMajorVersion = 0;
81  result.dwMinorVersion = 0;
82 
83  // Erase any build number and service pack information
84  result.dwBuildNumber = 0;
85  result.szCSDVersion[0] = L'\0';
86  result.wServicePackMajor = 0;
87  result.wServicePackMinor = 0;
88 
89  const QByteArray winVerOverride = qgetenv("QT_WINVER_OVERRIDE");
90  if (winVerOverride == "WINDOWS10" || winVerOverride == "2016") {
91  result.dwMajorVersion = 10;
92  } else {
93  return realResult;
94  }
95 
96  if (winVerOverride == "2016") {
97  // If the current host OS is a domain controller and the override OS
98  // is also a server type OS, preserve that information
99  if (result.wProductType == VER_NT_WORKSTATION)
100  result.wProductType = VER_NT_SERVER;
101  } else {
102  // Any other OS must be a workstation OS type
103  result.wProductType = VER_NT_WORKSTATION;
104  }
105  }
106  }
107 #endif
108  return realResult;
109 }
110 
112 {
113  static QOperatingSystemVersionBase v = [](){
115  v.m_os = currentType();
116  const OSVERSIONINFOEX osv = qWindowsVersionInfo();
117  v.m_major = osv.dwMajorVersion;
118  v.m_minor = osv.dwMinorVersion;
119  v.m_micro = osv.dwBuildNumber;
120  return v;
121  }();
122  return v;
123 }
124 
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
static Q_CORE_EXPORT QOperatingSystemVersionBase current()
static constexpr OSType currentType()
#define Q_UNLIKELY(x)
GLsizei const GLfloat * v
[13]
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
OSVERSIONINFOEX qWindowsVersionInfo()
HINSTANCE HMODULE