Skip to content

Commit fe63342

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 b4381c0 commit fe63342

File tree

1 file changed

+64
-21
lines changed

1 file changed

+64
-21
lines changed

lib/base/utility.cpp

+64-21
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"
@@ -52,6 +53,7 @@
5253
#ifdef _WIN32
5354
# include <VersionHelpers.h>
5455
# include <windows.h>
56+
# include <winreg.h>
5557
# include <io.h>
5658
# include <msi.h>
5759
# include <shlobj.h>
@@ -1533,28 +1535,55 @@ static String UnameHelper(char type)
15331535
}
15341536
}
15351537
#endif /* _WIN32 */
1538+
struct RegistryString
1539+
{
1540+
union {
1541+
BYTE AsBytes[512];
1542+
char AsChars[1] = {0};
1543+
} Data;
1544+
1545+
DWORD Size = sizeof(Data);
1546+
};
1547+
1548+
static const char * const l_RegCurrentVersion = R"EOF(SOFTWARE\Microsoft\Windows NT\CurrentVersion)EOF";
1549+
15361550
static bool ReleaseHelper(String *platformName, String *platformVersion)
15371551
{
15381552
#ifdef _WIN32
15391553
if (platformName)
15401554
*platformName = "Windows";
15411555

15421556
if (platformVersion) {
1543-
*platformVersion = "Vista";
1544-
if (IsWindowsVistaSP1OrGreater())
1545-
*platformVersion = "Vista SP1";
1546-
if (IsWindowsVistaSP2OrGreater())
1547-
*platformVersion = "Vista SP2";
1548-
if (IsWindows7OrGreater())
1549-
*platformVersion = "7";
1550-
if (IsWindows7SP1OrGreater())
1551-
*platformVersion = "7 SP1";
1552-
if (IsWindows8OrGreater())
1553-
*platformVersion = "8";
1554-
if (IsWindows8Point1OrGreater())
1555-
*platformVersion = "8.1 or greater";
1556-
if (IsWindowsServer())
1557-
*platformVersion += " (Server)";
1557+
HKEY hKey;
1558+
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));
1559+
1560+
if (err == ERROR_SUCCESS) {
1561+
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
1562+
RegistryString productName;
1563+
auto err (RegQueryValueExA(hKey, "ProductName", nullptr, nullptr, productName.Data.AsBytes, &productName.Size));
1564+
1565+
if (err == ERROR_SUCCESS) {
1566+
*platformVersion = productName.Data.AsChars;
1567+
1568+
RegistryString displayVersion;
1569+
1570+
if (RegQueryValueExA(hKey, "DisplayVersion", nullptr, nullptr, displayVersion.Data.AsBytes, &displayVersion.Size) == ERROR_SUCCESS) {
1571+
*platformVersion += " ";
1572+
*platformVersion += displayVersion.Data.AsChars;
1573+
} else {
1574+
RegistryString releaseId;
1575+
1576+
if (RegQueryValueExA(hKey, "ReleaseId", nullptr, nullptr, releaseId.Data.AsBytes, &releaseId.Size) == ERROR_SUCCESS) {
1577+
*platformVersion += " ";
1578+
*platformVersion += releaseId.Data.AsChars;
1579+
}
1580+
}
1581+
} else {
1582+
*platformVersion = "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\ProductName: " + Utility::FormatErrorNumber(err))) + ")";
1583+
}
1584+
} else {
1585+
*platformVersion = "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
1586+
}
15581587
}
15591588

15601589
return true;
@@ -1718,14 +1747,28 @@ String Utility::GetPlatformKernel()
17181747
String Utility::GetPlatformKernelVersion()
17191748
{
17201749
#ifdef _WIN32
1721-
OSVERSIONINFO info;
1722-
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1723-
GetVersionEx(&info);
1750+
HKEY hKey;
1751+
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));
17241752

1725-
std::ostringstream msgbuf;
1726-
msgbuf << info.dwMajorVersion << "." << info.dwMinorVersion;
1753+
if (err == ERROR_SUCCESS) {
1754+
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
1755+
RegistryString currentVersion;
1756+
auto err (RegQueryValueExA(hKey, "CurrentVersion", nullptr, nullptr, currentVersion.Data.AsBytes, &currentVersion.Size));
17271757

1728-
return msgbuf.str();
1758+
if (err == ERROR_SUCCESS) {
1759+
RegistryString currentBuildNumber;
1760+
1761+
if (RegQueryValueExA(hKey, "CurrentBuildNumber", nullptr, nullptr, currentBuildNumber.Data.AsBytes, &currentBuildNumber.Size) == ERROR_SUCCESS) {
1762+
return String(currentVersion.Data.AsChars) + "." + currentBuildNumber.Data.AsChars;
1763+
} else {
1764+
return currentVersion.Data.AsChars;
1765+
}
1766+
} else {
1767+
return "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\CurrentVersion: " + Utility::FormatErrorNumber(err))) + ")";
1768+
}
1769+
} else {
1770+
return "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
1771+
}
17291772
#else /* _WIN32 */
17301773
return UnameHelper('r');
17311774
#endif /* _WIN32 */

0 commit comments

Comments
 (0)