logging: Improve Windows OS version detection (#1949)

This commit is contained in:
Hr. Vedel 2026-06-08 17:02:51 +02:00 committed by GitHub
parent 326933b248
commit e4fa470140
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -499,21 +499,43 @@ namespace CafeSystem
std::string GetWindowsNamedVersion(uint32& buildNumber)
{
char productName[256];
char buildNumberStr[32];
char featureVersion[32];
HKEY hKey;
DWORD dwType = REG_SZ;
DWORD dwSize = sizeof(productName);
buildNumber = 0;
featureVersion[0] = '\0';
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
if (RegQueryValueExA(hKey, "ProductName", NULL, &dwType, (LPBYTE)productName, &dwSize) != ERROR_SUCCESS)
strcpy(productName, "Windows");
dwType = REG_SZ;
dwSize = sizeof(buildNumberStr);
if (RegQueryValueExA(hKey, "CurrentBuildNumber", NULL, &dwType, (LPBYTE)buildNumberStr, &dwSize) == ERROR_SUCCESS)
buildNumber = (uint32)atoi(buildNumberStr);
dwType = REG_SZ;
dwSize = sizeof(featureVersion);
if (RegQueryValueExA(hKey, "DisplayVersion", NULL, &dwType, (LPBYTE)featureVersion, &dwSize) != ERROR_SUCCESS)
{
dwType = REG_SZ;
dwSize = sizeof(featureVersion);
if (RegQueryValueExA(hKey, "ReleaseId", NULL, &dwType, (LPBYTE)featureVersion, &dwSize) != ERROR_SUCCESS)
featureVersion[0] = '\0';
}
RegCloseKey(hKey);
}
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
buildNumber = osvi.dwBuildNumber;
return std::string(productName);
std::string result(productName);
// ProductName still reads as "Windows 10" on Windows 11. Find and replace with "Windows 11" based on build number.
if (buildNumber >= 22000)
{
size_t pos = result.find("Windows 10");
if (pos != std::string::npos)
result.replace(pos, 10, "Windows 11");
}
if (featureVersion[0] != '\0')
result += fmt::format(" {}", featureVersion);
return result;
}
#endif