AuroraRuntime/Source/SWInfo/SWInfo.NT.cpp
Reece e665714341 [*] Bug fix: NT registry read function conflated array length with count
[*] Locale convert wchar -> utf count was not respected under non-nt targets
2022-05-26 15:02:23 +01:00

137 lines
4.4 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SWInfo.NT.cpp
Date: 2022-2-1
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "SWInfo.hpp"
#include "SWInfo.NT.hpp"
#if defined(AURORA_PLATFORM_WIN32)
#include <VersionHelpers.h>
#include <winternl.h>
#endif
namespace Aurora::SWInfo
{
static AuString gKernelString;
static AuString gUserlandBrand;
static AuString gUserlandDesktopEnv;
static AuString gBuildString;
#if defined(AURORA_PLATFORM_WIN32)
static bool IsWindowsEnterpriseBranch()
{
OSVERSIONINFOEXW osvi = {sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0, VER_SUITE_ENTERPRISE, 0, 0};
DWORDLONG const dwlConditionMask = VerSetConditionMask(0, VER_SUITENAME, VER_EQUAL);
return !VerifyVersionInfoW(&osvi, VER_SUITENAME, dwlConditionMask);
}
auline bool Win32ReadRegistry(HKEY hKey, const wchar_t *key, AuString &strValue)
{
DWORD dwBufferSize {};
if (RegQueryValueExW(hKey, key, 0, NULL, NULL, &dwBufferSize) != ERROR_SUCCESS)
{
SysPushErrorUnavailableError("Couldn't access registery key");
return false;
}
std::wstring in;
if (!AuTryResize(in, dwBufferSize / sizeof(wchar_t)))
{
SysPushErrorMem();
return false;
}
if (RegQueryValueExW(hKey, key, 0, NULL, reinterpret_cast<LPBYTE>(in.data()), &dwBufferSize) != ERROR_SUCCESS)
{
SysPushErrorUnavailableError("Couldn't access registery key");
return false;
}
auto c = dwBufferSize / sizeof(wchar_t);
if (c) c--;
strValue = Locale::ConvertFromWChar(in.data(), c);
return strValue.size();
}
#endif
void InitNTInfo(OSInformation &osInfo)
{
OSVERSIONINFOEX info {};
info.dwOSVersionInfoSize = sizeof(info);
osInfo = AuMove(OSInformation(&gKernelString, &gUserlandBrand, &gUserlandDesktopEnv, &gBuildString, Aurora::Build::EPlatform::eEnumInvalid));
#if defined(AURORA_PLATFORM_WIN32)
osInfo.bIsServer = IsWindowsServer();
osInfo.bIsEnterprise = osInfo.bIsServer;//IsWindowsEnterpriseBranch();
#else
osInfo.bIsServer = false;
osInfo.bIsEnterprise = false;
#endif
gUserlandDesktopEnv = "Desktop Window Manager";
if (GetVersionExA(reinterpret_cast<LPOSVERSIONINFOA>(&info)))
{
osInfo.uKernelPatch = info.dwBuildNumber;
osInfo.uKernelMinor = info.dwMinorVersion;
osInfo.uKernelMajor = info.dwMajorVersion;
osInfo.uUserlandMajor = osInfo.uKernelMajor;
osInfo.uUserlandMinor = info.wServicePackMajor;
osInfo.uUserlandPatch = info.wServicePackMinor;
}
#if defined(AURORA_PLATFORM_WIN32)
RTL_OSVERSIONINFOEXW ovi {};
ovi.dwOSVersionInfoSize = sizeof(ovi);
NTSTATUS(CALLBACK *pRtlGetVersion) (PRTL_OSVERSIONINFOW lpVersionInformation);
pRtlGetVersion = reinterpret_cast<decltype(pRtlGetVersion)>(GetProcAddress(LoadLibraryW(L"Ntdll.dll"), "RtlGetVersion"));
if (pRtlGetVersion && pRtlGetVersion(reinterpret_cast<PRTL_OSVERSIONINFOW>(&ovi)) == 0)
{
osInfo.uKernelPatch = ovi.dwBuildNumber;
osInfo.uKernelMinor = ovi.dwMinorVersion;
osInfo.uKernelMajor = ovi.dwMajorVersion;
osInfo.uUserlandMajor = osInfo.uKernelMajor;
osInfo.uUserlandMinor = ovi.wServicePackMajor;
osInfo.uUserlandPatch = ovi.wServicePackMinor;
}
HKEY hKey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
if (!Win32ReadRegistry(hKey, L"BuildLabEx", gBuildString))
{
Win32ReadRegistry(hKey, L"BuildLab", gBuildString);
}
Win32ReadRegistry(hKey, L"ProductName", gUserlandBrand);
RegCloseKey(hKey);
}
#endif
osInfo.bIsEnterprise |= AuStringContains(gUserlandBrand, "nterprise");
if (gKernelString.empty())
{
gKernelString = fmt::format("Microsoft NT {}.{}.{}", osInfo.uKernelMajor, osInfo.uKernelMinor, osInfo.uKernelPatch);
}
if (gUserlandBrand.empty())
{
gUserlandBrand = fmt::format("NT Userland {}.{}.{}", osInfo.uUserlandMajor, osInfo.uUserlandMinor, osInfo.uUserlandPatch);
}
}
}