Skip to content

Commit 88fccc5

Browse files
committed
Utility::GetPlatform*(): on Windows query the registry for version info
not to have to adjust the code (see removed lines) for every new version.
1 parent 50b4bc5 commit 88fccc5

1 file changed

Lines changed: 65 additions & 21 deletions

File tree

lib/base/utility.cpp

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: GPL-2.0-or-later
33

44
#include "base/atomic-file.hpp"
5+
#include "base/defer.hpp"
56
#include "base/utility.hpp"
67
#include "base/convert.hpp"
78
#include "base/application.hpp"
@@ -55,6 +56,7 @@
5556
#ifdef _WIN32
5657
# include <VersionHelpers.h>
5758
# include <windows.h>
59+
# include <winreg.h>
5860
# include <io.h>
5961
# include <msi.h>
6062
# include <shlobj.h>
@@ -1625,28 +1627,56 @@ static String UnameHelper(char type)
16251627
}
16261628
}
16271629
#endif /* _WIN32 */
1630+
1631+
#ifdef _WIN32
1632+
struct RegistryString
1633+
{
1634+
char Data[512] = {0};
1635+
DWORD Size = sizeof(Data);
1636+
1637+
BYTE* AsBytes() { return reinterpret_cast<BYTE*>(&Data); }
1638+
};
1639+
1640+
static const char * const l_RegCurrentVersion = R"EOF(SOFTWARE\Microsoft\Windows NT\CurrentVersion)EOF";
1641+
#endif /* _WIN32 */
1642+
16281643
static bool ReleaseHelper(String *platformName, String *platformVersion)
16291644
{
16301645
#ifdef _WIN32
16311646
if (platformName)
16321647
*platformName = "Windows";
16331648

16341649
if (platformVersion) {
1635-
*platformVersion = "Vista";
1636-
if (IsWindowsVistaSP1OrGreater())
1637-
*platformVersion = "Vista SP1";
1638-
if (IsWindowsVistaSP2OrGreater())
1639-
*platformVersion = "Vista SP2";
1640-
if (IsWindows7OrGreater())
1641-
*platformVersion = "7";
1642-
if (IsWindows7SP1OrGreater())
1643-
*platformVersion = "7 SP1";
1644-
if (IsWindows8OrGreater())
1645-
*platformVersion = "8";
1646-
if (IsWindows8Point1OrGreater())
1647-
*platformVersion = "8.1 or greater";
1648-
if (IsWindowsServer())
1649-
*platformVersion += " (Server)";
1650+
HKEY hKey;
1651+
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));
1652+
1653+
if (err == ERROR_SUCCESS) {
1654+
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
1655+
RegistryString productName;
1656+
auto err (RegQueryValueExA(hKey, "ProductName", nullptr, nullptr, productName.AsBytes(), &productName.Size));
1657+
1658+
if (err == ERROR_SUCCESS) {
1659+
*platformVersion = productName.Data;
1660+
1661+
RegistryString displayVersion;
1662+
1663+
if (RegQueryValueExA(hKey, "DisplayVersion", nullptr, nullptr, displayVersion.AsBytes(), &displayVersion.Size) == ERROR_SUCCESS) {
1664+
*platformVersion += " ";
1665+
*platformVersion += displayVersion.Data;
1666+
} else {
1667+
RegistryString releaseId;
1668+
1669+
if (RegQueryValueExA(hKey, "ReleaseId", nullptr, nullptr, releaseId.AsBytes(), &releaseId.Size) == ERROR_SUCCESS) {
1670+
*platformVersion += " ";
1671+
*platformVersion += releaseId.Data;
1672+
}
1673+
}
1674+
} else {
1675+
*platformVersion = "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\ProductName: " + Utility::FormatErrorNumber(err))) + ")";
1676+
}
1677+
} else {
1678+
*platformVersion = "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
1679+
}
16501680
}
16511681

16521682
return true;
@@ -1744,14 +1774,28 @@ String Utility::GetPlatformKernel()
17441774
String Utility::GetPlatformKernelVersion()
17451775
{
17461776
#ifdef _WIN32
1747-
OSVERSIONINFO info;
1748-
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1749-
GetVersionEx(&info);
1777+
HKEY hKey;
1778+
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));
17501779

1751-
std::ostringstream msgbuf;
1752-
msgbuf << info.dwMajorVersion << "." << info.dwMinorVersion;
1780+
if (err == ERROR_SUCCESS) {
1781+
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
1782+
RegistryString currentVersion;
1783+
auto err (RegQueryValueExA(hKey, "CurrentVersion", nullptr, nullptr, currentVersion.AsBytes(), &currentVersion.Size));
17531784

1754-
return msgbuf.str();
1785+
if (err == ERROR_SUCCESS) {
1786+
RegistryString currentBuildNumber;
1787+
1788+
if (RegQueryValueExA(hKey, "CurrentBuildNumber", nullptr, nullptr, currentBuildNumber.AsBytes(), &currentBuildNumber.Size) == ERROR_SUCCESS) {
1789+
return String(currentVersion.Data) + "." + currentBuildNumber.Data;
1790+
} else {
1791+
return currentVersion.Data;
1792+
}
1793+
} else {
1794+
return "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\CurrentVersion: " + Utility::FormatErrorNumber(err))) + ")";
1795+
}
1796+
} else {
1797+
return "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
1798+
}
17551799
#else /* _WIN32 */
17561800
return UnameHelper('r');
17571801
#endif /* _WIN32 */

0 commit comments

Comments
 (0)