Skip to content

Commit 1e4aab1

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 a65f2d6 commit 1e4aab1

1 file changed

Lines changed: 67 additions & 21 deletions

File tree

lib/base/utility.cpp

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
22

33
#include "base/atomic-file.hpp"
4+
#include "base/defer.hpp"
45
#include "base/utility.hpp"
56
#include "base/convert.hpp"
67
#include "base/application.hpp"
@@ -54,6 +55,7 @@
5455
#ifdef _WIN32
5556
# include <VersionHelpers.h>
5657
# include <windows.h>
58+
# include <winreg.h>
5759
# include <io.h>
5860
# include <msi.h>
5961
# include <shlobj.h>
@@ -1584,28 +1586,58 @@ static String UnameHelper(char type)
15841586
}
15851587
}
15861588
#endif /* _WIN32 */
1589+
1590+
#ifdef _WIN32
1591+
struct RegistryString
1592+
{
1593+
union {
1594+
BYTE AsBytes[512];
1595+
char AsChars[1] = {0};
1596+
} Data;
1597+
1598+
DWORD Size = sizeof(Data);
1599+
};
1600+
1601+
static const char * const l_RegCurrentVersion = R"EOF(SOFTWARE\Microsoft\Windows NT\CurrentVersion)EOF";
1602+
#endif /* _WIN32 */
1603+
15871604
static bool ReleaseHelper(String *platformName, String *platformVersion)
15881605
{
15891606
#ifdef _WIN32
15901607
if (platformName)
15911608
*platformName = "Windows";
15921609

15931610
if (platformVersion) {
1594-
*platformVersion = "Vista";
1595-
if (IsWindowsVistaSP1OrGreater())
1596-
*platformVersion = "Vista SP1";
1597-
if (IsWindowsVistaSP2OrGreater())
1598-
*platformVersion = "Vista SP2";
1599-
if (IsWindows7OrGreater())
1600-
*platformVersion = "7";
1601-
if (IsWindows7SP1OrGreater())
1602-
*platformVersion = "7 SP1";
1603-
if (IsWindows8OrGreater())
1604-
*platformVersion = "8";
1605-
if (IsWindows8Point1OrGreater())
1606-
*platformVersion = "8.1 or greater";
1607-
if (IsWindowsServer())
1608-
*platformVersion += " (Server)";
1611+
HKEY hKey;
1612+
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));
1613+
1614+
if (err == ERROR_SUCCESS) {
1615+
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
1616+
RegistryString productName;
1617+
auto err (RegQueryValueExA(hKey, "ProductName", nullptr, nullptr, productName.Data.AsBytes, &productName.Size));
1618+
1619+
if (err == ERROR_SUCCESS) {
1620+
*platformVersion = productName.Data.AsChars;
1621+
1622+
RegistryString displayVersion;
1623+
1624+
if (RegQueryValueExA(hKey, "DisplayVersion", nullptr, nullptr, displayVersion.Data.AsBytes, &displayVersion.Size) == ERROR_SUCCESS) {
1625+
*platformVersion += " ";
1626+
*platformVersion += displayVersion.Data.AsChars;
1627+
} else {
1628+
RegistryString releaseId;
1629+
1630+
if (RegQueryValueExA(hKey, "ReleaseId", nullptr, nullptr, releaseId.Data.AsBytes, &releaseId.Size) == ERROR_SUCCESS) {
1631+
*platformVersion += " ";
1632+
*platformVersion += releaseId.Data.AsChars;
1633+
}
1634+
}
1635+
} else {
1636+
*platformVersion = "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\ProductName: " + Utility::FormatErrorNumber(err))) + ")";
1637+
}
1638+
} else {
1639+
*platformVersion = "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
1640+
}
16091641
}
16101642

16111643
return true;
@@ -1703,14 +1735,28 @@ String Utility::GetPlatformKernel()
17031735
String Utility::GetPlatformKernelVersion()
17041736
{
17051737
#ifdef _WIN32
1706-
OSVERSIONINFO info;
1707-
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1708-
GetVersionEx(&info);
1738+
HKEY hKey;
1739+
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));
17091740

1710-
std::ostringstream msgbuf;
1711-
msgbuf << info.dwMajorVersion << "." << info.dwMinorVersion;
1741+
if (err == ERROR_SUCCESS) {
1742+
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
1743+
RegistryString currentVersion;
1744+
auto err (RegQueryValueExA(hKey, "CurrentVersion", nullptr, nullptr, currentVersion.Data.AsBytes, &currentVersion.Size));
17121745

1713-
return msgbuf.str();
1746+
if (err == ERROR_SUCCESS) {
1747+
RegistryString currentBuildNumber;
1748+
1749+
if (RegQueryValueExA(hKey, "CurrentBuildNumber", nullptr, nullptr, currentBuildNumber.Data.AsBytes, &currentBuildNumber.Size) == ERROR_SUCCESS) {
1750+
return String(currentVersion.Data.AsChars) + "." + currentBuildNumber.Data.AsChars;
1751+
} else {
1752+
return currentVersion.Data.AsChars;
1753+
}
1754+
} else {
1755+
return "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\CurrentVersion: " + Utility::FormatErrorNumber(err))) + ")";
1756+
}
1757+
} else {
1758+
return "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
1759+
}
17141760
#else /* _WIN32 */
17151761
return UnameHelper('r');
17161762
#endif /* _WIN32 */

0 commit comments

Comments
 (0)