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 
40 #include "qoperatingsystemversion_win_p.h"
41 
42 #include "qoperatingsystemversion_p.h"
43 
44 #include <qt_windows.h>
45 #include <qbytearray.h>
46 
47 QT_BEGIN_NAMESPACE
48 
49 #ifdef Q_OS_WINRT
moduleHandleForFunction(LPCVOID address)50 static inline HMODULE moduleHandleForFunction(LPCVOID address)
51 {
52     // This is a widely used, decades-old technique for retrieving the handle
53     // of a module and is effectively equivalent to GetModuleHandleEx
54     // (which is unavailable on WinRT)
55     MEMORY_BASIC_INFORMATION mbi = { 0, 0, 0, 0, 0, 0, 0 };
56     if (VirtualQuery(address, &mbi, sizeof(mbi)) == 0)
57         return 0;
58     return reinterpret_cast<HMODULE>(mbi.AllocationBase);
59 }
60 #endif
61 
determineWinOsVersion()62 static inline OSVERSIONINFOEX determineWinOsVersion()
63 {
64     OSVERSIONINFOEX result = { sizeof(OSVERSIONINFOEX), 0, 0, 0, 0, {'\0'}, 0, 0, 0, 0, 0};
65 
66 #define GetProcAddressA GetProcAddress
67 
68     // GetModuleHandle is not supported in WinRT and linking to it at load time
69     // will not pass the Windows App Certification Kit... but it exists and is functional,
70     // so use some unusual but widely used techniques to get a pointer to it
71 #ifdef Q_OS_WINRT
72     // 1. Get HMODULE of kernel32.dll, using the address of some function exported by that DLL
73     HMODULE kernelModule = moduleHandleForFunction(reinterpret_cast<LPCVOID>(VirtualQuery));
74     if (Q_UNLIKELY(!kernelModule))
75         return result;
76 
77     // 2. Get pointer to GetModuleHandle so we can then load other arbitrary modules (DLLs)
78     typedef HMODULE(WINAPI *GetModuleHandleFunction)(LPCWSTR);
79     GetModuleHandleFunction pGetModuleHandle = reinterpret_cast<GetModuleHandleFunction>(
80         GetProcAddressA(kernelModule, "GetModuleHandleW"));
81     if (Q_UNLIKELY(!pGetModuleHandle))
82         return result;
83 #else
84 #define pGetModuleHandle GetModuleHandleW
85 #endif
86 
87     HMODULE ntdll = pGetModuleHandle(L"ntdll.dll");
88     if (Q_UNLIKELY(!ntdll))
89         return result;
90 
91     // NTSTATUS is not defined on WinRT
92     typedef LONG NTSTATUS;
93     typedef NTSTATUS (NTAPI *RtlGetVersionFunction)(LPOSVERSIONINFO);
94 
95     // RtlGetVersion is documented public API but we must load it dynamically
96     // because linking to it at load time will not pass the Windows App Certification Kit
97     // https://msdn.microsoft.com/en-us/library/windows/hardware/ff561910.aspx
98     RtlGetVersionFunction pRtlGetVersion = reinterpret_cast<RtlGetVersionFunction>(
99         reinterpret_cast<QFunctionPointer>(GetProcAddressA(ntdll, "RtlGetVersion")));
100     if (Q_UNLIKELY(!pRtlGetVersion))
101         return result;
102 
103     // GetVersionEx() has been deprecated in Windows 8.1 and will return
104     // only Windows 8 from that version on, so use the kernel API function.
105     pRtlGetVersion(reinterpret_cast<LPOSVERSIONINFO>(&result)); // always returns STATUS_SUCCESS
106     return result;
107 }
108 
qWindowsVersionInfo()109 OSVERSIONINFOEX qWindowsVersionInfo()
110 {
111     OSVERSIONINFOEX realResult = determineWinOsVersion();
112 #ifdef QT_DEBUG
113     {
114         if (Q_UNLIKELY(qEnvironmentVariableIsSet("QT_WINVER_OVERRIDE"))) {
115             OSVERSIONINFOEX result = realResult;
116             result.dwMajorVersion = 0;
117             result.dwMinorVersion = 0;
118 
119             // Erase any build number and service pack information
120             result.dwBuildNumber = 0;
121             result.szCSDVersion[0] = L'\0';
122             result.wServicePackMajor = 0;
123             result.wServicePackMinor = 0;
124 
125             const QByteArray winVerOverride = qgetenv("QT_WINVER_OVERRIDE");
126             if (winVerOverride == "WINDOWS7" || winVerOverride == "2008_R2") {
127                 result.dwMajorVersion = 6;
128                 result.dwMinorVersion = 1;
129             } else if (winVerOverride == "WINDOWS8" || winVerOverride == "2012") {
130                 result.dwMajorVersion = 6;
131                 result.dwMinorVersion = 2;
132             } else if (winVerOverride == "WINDOWS8_1" || winVerOverride == "2012_R2") {
133                 result.dwMajorVersion = 6;
134                 result.dwMinorVersion = 3;
135             } else if (winVerOverride == "WINDOWS10" || winVerOverride == "2016") {
136                 result.dwMajorVersion = 10;
137             } else {
138                 return realResult;
139             }
140 
141             if (winVerOverride == "2008_R2"
142                 || winVerOverride == "2012"
143                 || winVerOverride == "2012_R2"
144                 || winVerOverride == "2016") {
145                 // If the current host OS is a domain controller and the override OS
146                 // is also a server type OS, preserve that information
147                 if (result.wProductType == VER_NT_WORKSTATION)
148                     result.wProductType = VER_NT_SERVER;
149             } else {
150                 // Any other OS must be a workstation OS type
151                 result.wProductType = VER_NT_WORKSTATION;
152             }
153         }
154     }
155 #endif
156     return realResult;
157 }
158 
current()159 QOperatingSystemVersion QOperatingSystemVersion::current()
160 {
161     QOperatingSystemVersion v;
162     v.m_os = currentType();
163     const OSVERSIONINFOEX osv = qWindowsVersionInfo();
164     v.m_major = osv.dwMajorVersion;
165     v.m_minor = osv.dwMinorVersion;
166     v.m_micro = osv.dwBuildNumber;
167     return v;
168 }
169 
170 QT_END_NAMESPACE
171