| 1 | #include "Common/WindowsRegistry.h" |
| 2 | |
| 3 | #include <Windows.h> |
| 4 | #include <string> |
| 5 | #include <type_traits> |
| 6 | #include "Common/StringUtil.h" |
| 7 | |
| 8 | namespace WindowsRegistry |
| 9 | { |
| 10 | template <typename T> |
| 11 | bool ReadValue(T* value, const std::string& subkey, const std::string& name) |
| 12 | { |
| 13 | DWORD flags = 0; |
| 14 | static_assert(std::is_integral_v<T> && (sizeof(T) == sizeof(u32) || sizeof(T) == sizeof(u64)), |
| 15 | "Unsupported type"); |
| 16 | if constexpr (sizeof(T) == sizeof(u32)) |
| 17 | flags = RRF_RT_REG_DWORD; |
| 18 | else if constexpr (sizeof(T) == sizeof(u64)) |
| 19 | flags = RRF_RT_REG_QWORD; |
| 20 | |
| 21 | DWORD value_len = sizeof(*value); |
| 22 | return RegGetValueA(HKEY_LOCAL_MACHINE, subkey.c_str(), name.c_str(), flags, nullptr, value, |
| 23 | &value_len) == ERROR_SUCCESS; |
| 24 | } |
| 25 | |
| 26 | template <> |
| 27 | bool ReadValue(std::string* value, const std::string& subkey, const std::string& name) |
| 28 | { |
| 29 | const DWORD flags = RRF_RT_REG_SZ | RRF_NOEXPAND; |
| 30 | DWORD value_len = 0; |
| 31 | auto status = RegGetValueA(HKEY_LOCAL_MACHINE, subkey.c_str(), name.c_str(), flags, nullptr, |
| 32 | nullptr, &value_len); |
| 33 | if (status != ERROR_SUCCESS && status != ERROR_MORE_DATA) |
| 34 | return false; |
| 35 | |
| 36 | value->resize(value_len); |
| 37 | status = RegGetValueA(HKEY_LOCAL_MACHINE, subkey.c_str(), name.c_str(), flags, nullptr, |
| 38 | value->data(), &value_len); |
| 39 | if (status != ERROR_SUCCESS) |
| 40 | { |
| 41 | value->clear(); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | TruncateToCString(value); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | // These explicit instantiations are needed to prevent linker errors when calling |
| 50 | // ReadValue in WinUpdater/Platform.cpp (for u32) and ArmCPUDetect.cpp (for u64) |
| 51 | template bool ReadValue(u32* value, const std::string& subkey, const std::string& name); |
| 52 | template bool ReadValue(u64* value, const std::string& subkey, const std::string& name); |
| 53 | |
| 54 | OSVERSIONINFOW GetOSVersion() |
| 55 | { |
| 56 | // PEB may have faked data if the binary is launched with "compatibility mode" enabled. |
| 57 | // Try to read real OS version from registry. |
| 58 | const char* subkey = R"(SOFTWARE\Microsoft\Windows NT\CurrentVersion)"; |
| 59 | OSVERSIONINFOW info{.dwOSVersionInfoSize = sizeof(info)}; |
| 60 | std::string build_str; |
| 61 | if (!ReadValue(&info.dwMajorVersion, subkey, "CurrentMajorVersionNumber") || |
| 62 | !ReadValue(&info.dwMinorVersion, subkey, "CurrentMinorVersionNumber") || |
| 63 | !ReadValue(&build_str, subkey, "CurrentBuildNumber") || |
| 64 | !TryParse(build_str, &info.dwBuildNumber)) |
| 65 | { |
| 66 | // Fallback to version from PEB |
| 67 | typedef DWORD(WINAPI * RtlGetVersion_t)(PRTL_OSVERSIONINFOW); |
| 68 | auto RtlGetVersion = |
| 69 | (RtlGetVersion_t)GetProcAddress(GetModuleHandle(TEXT("ntdll")), "RtlGetVersion"); |
| 70 | RtlGetVersion(&info); |
| 71 | // Clear fields which would not be filled in by registry query |
| 72 | info.dwPlatformId = 0; |
| 73 | info.szCSDVersion[0] = L'\0'; |
| 74 | } |
| 75 | return info; |
| 76 | } |
| 77 | } // namespace WindowsRegistry |