Skip to content
Open
Show file tree
Hide file tree
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
82 changes: 70 additions & 12 deletions lib/pal/desktop/WindowsDesktopSystemInformationImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "WindowsEnvironmentInfo.hpp"

#include <cstdint>
#include <string>

// This define is only available for TH1+
Expand Down Expand Up @@ -139,17 +140,62 @@ namespace PAL_NS_BEGIN {
std::to_string(static_cast<int>(LOWORD(pffi->dwProductVersionLS)));
}

/**
* Get OS BuildLabEx string
*/
std::string getOsBuildLabEx()
const PCSTR c_currentVersion_Key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";

bool getCurrentVersionStringValue(PCSTR valueName, std::string& value)
{
char buff[MAX_PATH] = { 0 };
const PCSTR c_currentVersion_Key = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
const PCSTR c_buildLabEx_ValueName = "BuildLabEx";
DWORD size = sizeof(buff);
RegGetValueA(HKEY_LOCAL_MACHINE, c_currentVersion_Key, c_buildLabEx_ValueName, RRF_RT_REG_SZ | RRF_SUBKEY_WOW6464KEY, NULL, (char*)buff, &size);
return buff;
if (RegGetValueA(
HKEY_LOCAL_MACHINE,
c_currentVersion_Key,
valueName,
RRF_RT_REG_SZ | RRF_SUBKEY_WOW6464KEY,
NULL,
static_cast<char*>(buff),
&size) != ERROR_SUCCESS)
{
return false;
}

value = buff;
return !value.empty();
}

bool getCurrentVersionDwordValue(PCSTR valueName, uint32_t& value)
{
DWORD regValue = 0;
DWORD size = sizeof(regValue);
if (RegGetValueA(
HKEY_LOCAL_MACHINE,
c_currentVersion_Key,
valueName,
RRF_RT_REG_DWORD | RRF_SUBKEY_WOW6464KEY,
NULL,
&regValue,
&size) != ERROR_SUCCESS)
{
return false;
}

value = regValue;
return true;
}

std::string formatWindowsOsFullVersion(
unsigned long majorVersion,
unsigned long minorVersion,
std::string const& buildNumber,
uint32_t updateBuildRevision,
bool hasUpdateBuildRevision)
{
std::string version = std::to_string(majorVersion) + "." + std::to_string(minorVersion) + "." + buildNumber;
if (hasUpdateBuildRevision)
{
version += "." + std::to_string(updateBuildRevision);
}

return version;
}

/**
Expand All @@ -175,8 +221,6 @@ namespace PAL_NS_BEGIN {
*/
void getOsVersion(std::string& osMajorVersion, std::string& osFullVersion)
{
std::string buildLabEx = getOsBuildLabEx();

HMODULE hNtDll = ::GetModuleHandle(TEXT("ntdll.dll"));
typedef HRESULT NTSTATUS;
typedef NTSTATUS(__stdcall * RtlGetVersion_t)(PRTL_OSVERSIONINFOW);
Expand All @@ -186,8 +230,22 @@ namespace PAL_NS_BEGIN {
if (pRtlGetVersion && SUCCEEDED(pRtlGetVersion(&rtlOsvi)))
{
osMajorVersion = std::to_string((long long)rtlOsvi.dwMajorVersion) + "." + std::to_string((long long)rtlOsvi.dwMinorVersion);
osFullVersion = osMajorVersion + "." + std::to_string((long long)rtlOsvi.dwBuildNumber);
osFullVersion = osMajorVersion + "." + buildLabEx;

std::string buildNumber;
if (!getCurrentVersionStringValue("CurrentBuildNumber", buildNumber) &&
!getCurrentVersionStringValue("CurrentBuild", buildNumber))
{
buildNumber = std::to_string((long long)rtlOsvi.dwBuildNumber);
}

uint32_t updateBuildRevision = 0;
bool hasUpdateBuildRevision = getCurrentVersionDwordValue("UBR", updateBuildRevision);
osFullVersion = formatWindowsOsFullVersion(
rtlOsvi.dwMajorVersion,
rtlOsvi.dwMinorVersion,
buildNumber,
updateBuildRevision,
hasUpdateBuildRevision);
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/unittests/PalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "pal/PseudoRandomGenerator.hpp"
#include "Version.hpp"

#include <cstdint>
#include <string>

#ifdef HAVE_MAT_LOGGING
#include "pal/PAL.hpp"
#include <gtest/gtest.h>
Expand All @@ -24,6 +27,17 @@ using namespace PAL::detail;

using namespace testing;

#if defined(_WIN32) || defined(_WIN64)
namespace PAL_NS_BEGIN {
std::string formatWindowsOsFullVersion(
unsigned long majorVersion,
unsigned long minorVersion,
std::string const& buildNumber,
uint32_t updateBuildRevision,
bool hasUpdateBuildRevision);
} PAL_NS_END
#endif

class PalTests : public Test {};

TEST_F(PalTests, UuidGeneration)
Expand Down Expand Up @@ -95,6 +109,18 @@ TEST_F(PalTests, FormatUtcTimestampMsAsISO8601)
EXPECT_THAT(PAL::formatUtcTimestampMsAsISO8601(2147483647999ll), Eq("2038-01-19T03:14:07.999Z"));
}

#if defined(_WIN32) || defined(_WIN64)
TEST_F(PalTests, WindowsOsFullVersionUsesServicingBuildValues)
{
EXPECT_THAT(
PAL::formatWindowsOsFullVersion(10, 0, "26200", 1234, true),
Eq("10.0.26200.1234"));
EXPECT_THAT(
PAL::formatWindowsOsFullVersion(10, 0, "26200", 0, false),
Eq("10.0.26200"));
}
#endif

TEST_F(PalTests, MonotonicTime)
{
int64_t t0 = PAL::getMonotonicTimeMs();
Expand Down
Loading