You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/win/windows_version.h"
  11. #include <windows.h>
  12. #include <memory>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/string_utils.h"
  15. #if !defined(__clang__) && _MSC_FULL_VER < 191125507
  16. #error VS 2017 Update 3.2 or higher is required
  17. #endif
  18. #if !defined(WINUWP)
  19. namespace {
  20. typedef BOOL(WINAPI* GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD);
  21. // Mask to pull WOW64 access flags out of REGSAM access.
  22. const REGSAM kWow64AccessMask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
  23. // Utility class to read, write and manipulate the Windows Registry.
  24. // Registry vocabulary primer: a "key" is like a folder, in which there
  25. // are "values", which are <name, data> pairs, with an associated data type.
  26. // Based on base::win::RegKey but only implements a small fraction of it.
  27. class RegKey {
  28. public:
  29. RegKey() : key_(nullptr), wow64access_(0) {}
  30. RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access)
  31. : key_(nullptr), wow64access_(0) {
  32. if (rootkey) {
  33. if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
  34. Create(rootkey, subkey, access);
  35. else
  36. Open(rootkey, subkey, access);
  37. } else {
  38. RTC_DCHECK(!subkey);
  39. wow64access_ = access & kWow64AccessMask;
  40. }
  41. }
  42. ~RegKey() { Close(); }
  43. LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
  44. DWORD disposition_value;
  45. return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
  46. }
  47. LONG CreateWithDisposition(HKEY rootkey,
  48. const wchar_t* subkey,
  49. DWORD* disposition,
  50. REGSAM access) {
  51. RTC_DCHECK(rootkey && subkey && access && disposition);
  52. HKEY subhkey = NULL;
  53. LONG result =
  54. ::RegCreateKeyExW(rootkey, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
  55. access, NULL, &subhkey, disposition);
  56. if (result == ERROR_SUCCESS) {
  57. Close();
  58. key_ = subhkey;
  59. wow64access_ = access & kWow64AccessMask;
  60. }
  61. return result;
  62. }
  63. // Opens an existing reg key.
  64. LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
  65. RTC_DCHECK(rootkey && subkey && access);
  66. HKEY subhkey = NULL;
  67. LONG result = ::RegOpenKeyExW(rootkey, subkey, 0, access, &subhkey);
  68. if (result == ERROR_SUCCESS) {
  69. Close();
  70. key_ = subhkey;
  71. wow64access_ = access & kWow64AccessMask;
  72. }
  73. return result;
  74. }
  75. // Closes this reg key.
  76. void Close() {
  77. if (key_) {
  78. ::RegCloseKey(key_);
  79. key_ = nullptr;
  80. }
  81. }
  82. // Reads a REG_DWORD (uint32_t) into |out_value|. If |name| is null or empty,
  83. // reads the key's default value, if any.
  84. LONG ReadValueDW(const wchar_t* name, DWORD* out_value) const {
  85. RTC_DCHECK(out_value);
  86. DWORD type = REG_DWORD;
  87. DWORD size = sizeof(DWORD);
  88. DWORD local_value = 0;
  89. LONG result = ReadValue(name, &local_value, &size, &type);
  90. if (result == ERROR_SUCCESS) {
  91. if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD))
  92. *out_value = local_value;
  93. else
  94. result = ERROR_CANTREAD;
  95. }
  96. return result;
  97. }
  98. // Reads a string into |out_value|. If |name| is null or empty, reads
  99. // the key's default value, if any.
  100. LONG ReadValue(const wchar_t* name, std::wstring* out_value) const {
  101. RTC_DCHECK(out_value);
  102. const size_t kMaxStringLength = 1024; // This is after expansion.
  103. // Use the one of the other forms of ReadValue if 1024 is too small for you.
  104. wchar_t raw_value[kMaxStringLength];
  105. DWORD type = REG_SZ, size = sizeof(raw_value);
  106. LONG result = ReadValue(name, raw_value, &size, &type);
  107. if (result == ERROR_SUCCESS) {
  108. if (type == REG_SZ) {
  109. *out_value = raw_value;
  110. } else if (type == REG_EXPAND_SZ) {
  111. wchar_t expanded[kMaxStringLength];
  112. size =
  113. ::ExpandEnvironmentStringsW(raw_value, expanded, kMaxStringLength);
  114. // Success: returns the number of wchar_t's copied
  115. // Fail: buffer too small, returns the size required
  116. // Fail: other, returns 0
  117. if (size == 0 || size > kMaxStringLength) {
  118. result = ERROR_MORE_DATA;
  119. } else {
  120. *out_value = expanded;
  121. }
  122. } else {
  123. // Not a string. Oops.
  124. result = ERROR_CANTREAD;
  125. }
  126. }
  127. return result;
  128. }
  129. LONG ReadValue(const wchar_t* name,
  130. void* data,
  131. DWORD* dsize,
  132. DWORD* dtype) const {
  133. LONG result = RegQueryValueExW(key_, name, 0, dtype,
  134. reinterpret_cast<LPBYTE>(data), dsize);
  135. return result;
  136. }
  137. private:
  138. HKEY key_;
  139. REGSAM wow64access_;
  140. };
  141. } // namespace
  142. #endif // !defined(WINUWP)
  143. namespace rtc {
  144. namespace rtc_win {
  145. namespace {
  146. // Helper to map a major.minor.x.build version (e.g. 6.1) to a Windows release.
  147. Version MajorMinorBuildToVersion(int major, int minor, int build) {
  148. if ((major == 5) && (minor > 0)) {
  149. // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003.
  150. return (minor == 1) ? VERSION_XP : VERSION_SERVER_2003;
  151. } else if (major == 6) {
  152. switch (minor) {
  153. case 0:
  154. // Treat Windows Server 2008 the same as Windows Vista.
  155. return VERSION_VISTA;
  156. case 1:
  157. // Treat Windows Server 2008 R2 the same as Windows 7.
  158. return VERSION_WIN7;
  159. case 2:
  160. // Treat Windows Server 2012 the same as Windows 8.
  161. return VERSION_WIN8;
  162. default:
  163. RTC_DCHECK_EQ(minor, 3);
  164. return VERSION_WIN8_1;
  165. }
  166. } else if (major == 10) {
  167. if (build < 10586) {
  168. return VERSION_WIN10;
  169. } else if (build < 14393) {
  170. return VERSION_WIN10_TH2;
  171. } else if (build < 15063) {
  172. return VERSION_WIN10_RS1;
  173. } else if (build < 16299) {
  174. return VERSION_WIN10_RS2;
  175. } else if (build < 17134) {
  176. return VERSION_WIN10_RS3;
  177. } else {
  178. return VERSION_WIN10_RS4;
  179. }
  180. } else if (major > 6) {
  181. RTC_NOTREACHED();
  182. return VERSION_WIN_LAST;
  183. }
  184. return VERSION_PRE_XP;
  185. }
  186. // Returns the the "UBR" value from the registry. Introduced in Windows 10,
  187. // this undocumented value appears to be similar to a patch number.
  188. // Returns 0 if the value does not exist or it could not be read.
  189. int GetUBR() {
  190. #if defined(WINUWP)
  191. // The registry is not accessible for WinUWP sandboxed store applications.
  192. return 0;
  193. #else
  194. // The values under the CurrentVersion registry hive are mirrored under
  195. // the corresponding Wow6432 hive.
  196. static constexpr wchar_t kRegKeyWindowsNTCurrentVersion[] =
  197. L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
  198. RegKey key;
  199. if (key.Open(HKEY_LOCAL_MACHINE, kRegKeyWindowsNTCurrentVersion,
  200. KEY_QUERY_VALUE) != ERROR_SUCCESS) {
  201. return 0;
  202. }
  203. DWORD ubr = 0;
  204. key.ReadValueDW(L"UBR", &ubr);
  205. return static_cast<int>(ubr);
  206. #endif // defined(WINUWP)
  207. }
  208. } // namespace
  209. // static
  210. OSInfo* OSInfo::GetInstance() {
  211. // Note: we don't use the Singleton class because it depends on AtExitManager,
  212. // and it's convenient for other modules to use this class without it. This
  213. // pattern is copied from gurl.cc.
  214. static OSInfo* info;
  215. if (!info) {
  216. OSInfo* new_info = new OSInfo();
  217. if (InterlockedCompareExchangePointer(reinterpret_cast<PVOID*>(&info),
  218. new_info, NULL)) {
  219. delete new_info;
  220. }
  221. }
  222. return info;
  223. }
  224. OSInfo::OSInfo()
  225. : version_(VERSION_PRE_XP),
  226. architecture_(OTHER_ARCHITECTURE),
  227. wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) {
  228. OSVERSIONINFOEXW version_info = {sizeof version_info};
  229. // Applications not manifested for Windows 8.1 or Windows 10 will return the
  230. // Windows 8 OS version value (6.2). Once an application is manifested for a
  231. // given operating system version, GetVersionEx() will always return the
  232. // version that the application is manifested for in future releases.
  233. // https://docs.microsoft.com/en-us/windows/desktop/SysInfo/targeting-your-application-at-windows-8-1.
  234. // https://www.codeproject.com/Articles/678606/Part-Overcoming-Windows-s-deprecation-of-GetVe.
  235. ::GetVersionExW(reinterpret_cast<OSVERSIONINFOW*>(&version_info));
  236. version_number_.major = version_info.dwMajorVersion;
  237. version_number_.minor = version_info.dwMinorVersion;
  238. version_number_.build = version_info.dwBuildNumber;
  239. version_number_.patch = GetUBR();
  240. version_ = MajorMinorBuildToVersion(
  241. version_number_.major, version_number_.minor, version_number_.build);
  242. service_pack_.major = version_info.wServicePackMajor;
  243. service_pack_.minor = version_info.wServicePackMinor;
  244. service_pack_str_ = rtc::ToUtf8(version_info.szCSDVersion);
  245. SYSTEM_INFO system_info = {};
  246. ::GetNativeSystemInfo(&system_info);
  247. switch (system_info.wProcessorArchitecture) {
  248. case PROCESSOR_ARCHITECTURE_INTEL:
  249. architecture_ = X86_ARCHITECTURE;
  250. break;
  251. case PROCESSOR_ARCHITECTURE_AMD64:
  252. architecture_ = X64_ARCHITECTURE;
  253. break;
  254. case PROCESSOR_ARCHITECTURE_IA64:
  255. architecture_ = IA64_ARCHITECTURE;
  256. break;
  257. }
  258. processors_ = system_info.dwNumberOfProcessors;
  259. allocation_granularity_ = system_info.dwAllocationGranularity;
  260. #if !defined(WINUWP)
  261. GetProductInfoPtr get_product_info;
  262. DWORD os_type;
  263. if (version_info.dwMajorVersion == 6 || version_info.dwMajorVersion == 10) {
  264. // Only present on Vista+.
  265. get_product_info = reinterpret_cast<GetProductInfoPtr>(::GetProcAddress(
  266. ::GetModuleHandleW(L"kernel32.dll"), "GetProductInfo"));
  267. get_product_info(version_info.dwMajorVersion, version_info.dwMinorVersion,
  268. 0, 0, &os_type);
  269. switch (os_type) {
  270. case PRODUCT_CLUSTER_SERVER:
  271. case PRODUCT_DATACENTER_SERVER:
  272. case PRODUCT_DATACENTER_SERVER_CORE:
  273. case PRODUCT_ENTERPRISE_SERVER:
  274. case PRODUCT_ENTERPRISE_SERVER_CORE:
  275. case PRODUCT_ENTERPRISE_SERVER_IA64:
  276. case PRODUCT_SMALLBUSINESS_SERVER:
  277. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  278. case PRODUCT_STANDARD_SERVER:
  279. case PRODUCT_STANDARD_SERVER_CORE:
  280. case PRODUCT_WEB_SERVER:
  281. version_type_ = SUITE_SERVER;
  282. break;
  283. case PRODUCT_PROFESSIONAL:
  284. case PRODUCT_ULTIMATE:
  285. version_type_ = SUITE_PROFESSIONAL;
  286. break;
  287. case PRODUCT_ENTERPRISE:
  288. case PRODUCT_ENTERPRISE_E:
  289. case PRODUCT_ENTERPRISE_EVALUATION:
  290. case PRODUCT_ENTERPRISE_N:
  291. case PRODUCT_ENTERPRISE_N_EVALUATION:
  292. case PRODUCT_ENTERPRISE_S:
  293. case PRODUCT_ENTERPRISE_S_EVALUATION:
  294. case PRODUCT_ENTERPRISE_S_N:
  295. case PRODUCT_ENTERPRISE_S_N_EVALUATION:
  296. case PRODUCT_BUSINESS:
  297. case PRODUCT_BUSINESS_N:
  298. version_type_ = SUITE_ENTERPRISE;
  299. break;
  300. case PRODUCT_EDUCATION:
  301. case PRODUCT_EDUCATION_N:
  302. version_type_ = SUITE_EDUCATION;
  303. break;
  304. case PRODUCT_HOME_BASIC:
  305. case PRODUCT_HOME_PREMIUM:
  306. case PRODUCT_STARTER:
  307. default:
  308. version_type_ = SUITE_HOME;
  309. break;
  310. }
  311. } else if (version_info.dwMajorVersion == 5 &&
  312. version_info.dwMinorVersion == 2) {
  313. if (version_info.wProductType == VER_NT_WORKSTATION &&
  314. system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
  315. version_type_ = SUITE_PROFESSIONAL;
  316. } else if (version_info.wSuiteMask & VER_SUITE_WH_SERVER) {
  317. version_type_ = SUITE_HOME;
  318. } else {
  319. version_type_ = SUITE_SERVER;
  320. }
  321. } else if (version_info.dwMajorVersion == 5 &&
  322. version_info.dwMinorVersion == 1) {
  323. if (version_info.wSuiteMask & VER_SUITE_PERSONAL)
  324. version_type_ = SUITE_HOME;
  325. else
  326. version_type_ = SUITE_PROFESSIONAL;
  327. } else {
  328. // Windows is pre XP so we don't care but pick a safe default.
  329. version_type_ = SUITE_HOME;
  330. }
  331. #else
  332. // WinUWP sandboxed store apps do not have a mechanism to determine
  333. // product suite thus the most restricted suite is chosen.
  334. version_type_ = SUITE_HOME;
  335. #endif // !defined(WINUWP)
  336. }
  337. OSInfo::~OSInfo() {}
  338. std::string OSInfo::processor_model_name() {
  339. #if defined(WINUWP)
  340. // WinUWP sandboxed store apps do not have the ability to
  341. // probe the name of the current processor.
  342. return "Unknown Processor (UWP)";
  343. #else
  344. if (processor_model_name_.empty()) {
  345. const wchar_t kProcessorNameString[] =
  346. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
  347. RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ);
  348. std::wstring value;
  349. key.ReadValue(L"ProcessorNameString", &value);
  350. processor_model_name_ = rtc::ToUtf8(value);
  351. }
  352. return processor_model_name_;
  353. #endif // defined(WINUWP)
  354. }
  355. // static
  356. OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) {
  357. BOOL is_wow64;
  358. #if defined(WINUWP)
  359. if (!IsWow64Process(process_handle, &is_wow64))
  360. return WOW64_UNKNOWN;
  361. #else
  362. typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE, PBOOL);
  363. IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
  364. GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "IsWow64Process"));
  365. if (!is_wow64_process)
  366. return WOW64_DISABLED;
  367. if (!(*is_wow64_process)(process_handle, &is_wow64))
  368. return WOW64_UNKNOWN;
  369. #endif // defined(WINUWP)
  370. return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
  371. }
  372. Version GetVersion() {
  373. return OSInfo::GetInstance()->version();
  374. }
  375. } // namespace rtc_win
  376. } // namespace rtc