Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 65 additions & 21 deletions lib/base/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include "base/atomic-file.hpp"
#include "base/defer.hpp"
#include "base/utility.hpp"
#include "base/convert.hpp"
#include "base/application.hpp"
Expand Down Expand Up @@ -55,6 +56,7 @@
#ifdef _WIN32
# include <VersionHelpers.h>
# include <windows.h>
# include <winreg.h>
# include <io.h>
# include <msi.h>
# include <shlobj.h>
Expand Down Expand Up @@ -1625,28 +1627,56 @@ static String UnameHelper(char type)
}
}
#endif /* _WIN32 */

#ifdef _WIN32
struct RegistryString
{
char Data[512] = {0};
DWORD Size = sizeof(Data);

BYTE* AsBytes() { return reinterpret_cast<BYTE*>(&Data); }
};

static const char * const l_RegCurrentVersion = R"EOF(SOFTWARE\Microsoft\Windows NT\CurrentVersion)EOF";
#endif /* _WIN32 */

static bool ReleaseHelper(String *platformName, String *platformVersion)
{
#ifdef _WIN32
if (platformName)
*platformName = "Windows";

if (platformVersion) {
*platformVersion = "Vista";
if (IsWindowsVistaSP1OrGreater())
*platformVersion = "Vista SP1";
if (IsWindowsVistaSP2OrGreater())
*platformVersion = "Vista SP2";
if (IsWindows7OrGreater())
*platformVersion = "7";
if (IsWindows7SP1OrGreater())
*platformVersion = "7 SP1";
if (IsWindows8OrGreater())
*platformVersion = "8";
if (IsWindows8Point1OrGreater())
*platformVersion = "8.1 or greater";
if (IsWindowsServer())
*platformVersion += " (Server)";
HKEY hKey;
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));

if (err == ERROR_SUCCESS) {
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
RegistryString productName;
auto err (RegQueryValueExA(hKey, "ProductName", nullptr, nullptr, productName.AsBytes(), &productName.Size));

if (err == ERROR_SUCCESS) {
*platformVersion = productName.Data;

RegistryString displayVersion;

if (RegQueryValueExA(hKey, "DisplayVersion", nullptr, nullptr, displayVersion.AsBytes(), &displayVersion.Size) == ERROR_SUCCESS) {
*platformVersion += " ";
*platformVersion += displayVersion.Data;
} else {
RegistryString releaseId;

if (RegQueryValueExA(hKey, "ReleaseId", nullptr, nullptr, releaseId.AsBytes(), &releaseId.Size) == ERROR_SUCCESS) {
*platformVersion += " ";
*platformVersion += releaseId.Data;
}
}
} else {
*platformVersion = "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\ProductName: " + Utility::FormatErrorNumber(err))) + ")";
}
} else {
*platformVersion = "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
}
}

return true;
Expand Down Expand Up @@ -1744,14 +1774,28 @@ String Utility::GetPlatformKernel()
String Utility::GetPlatformKernelVersion()
{
#ifdef _WIN32
OSVERSIONINFO info;
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&info);
HKEY hKey;
auto err (RegOpenKeyExA(HKEY_LOCAL_MACHINE, l_RegCurrentVersion, 0, KEY_READ, &hKey));

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

return msgbuf.str();
if (err == ERROR_SUCCESS) {
RegistryString currentBuildNumber;

if (RegQueryValueExA(hKey, "CurrentBuildNumber", nullptr, nullptr, currentBuildNumber.AsBytes(), &currentBuildNumber.Size) == ERROR_SUCCESS) {
return String(currentVersion.Data) + "." + currentBuildNumber.Data;
} else {
return currentVersion.Data;
}
} else {
return "Unknown (Can't query HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + ("\\CurrentVersion: " + Utility::FormatErrorNumber(err))) + ")";
}
} else {
return "Unknown (Can't open HKEY_LOCAL_MACHINE\\" + (l_RegCurrentVersion + (": " + Utility::FormatErrorNumber(err))) + ")";
}
#else /* _WIN32 */
return UnameHelper('r');
#endif /* _WIN32 */
Expand Down
Loading