Platform: Use registry to retrieve OS name information

This commit is contained in:
Brian Ferguson 2014-02-13 09:19:54 -07:00
parent 0b34c06974
commit 57e6787b71
3 changed files with 62 additions and 194 deletions

View File

@ -18,212 +18,77 @@
#include "StdAfx.h"
#include "Platform.h"
#include <wchar.h>
#include <string>
namespace Platform {
LPCWSTR GetPlatformName(bool getExtendedInfo)
LPCWSTR GetPlatformName()
{
static WCHAR buffer[256];
wcscpy_s(buffer, L"");
bool isServer = IsWindowsServer();
OSVERSIONINFOEX osvi = { sizeof(OSVERSIONINFOEX) };
if (GetVersionEx((OSVERSIONINFO*)&osvi))
{
// Note: Place newer versions at the top.
if (IsWindows8Point1OrGreater())
{
wcscat_s(buffer, isServer ? L"Windows Server 2012 R2" : L"Windows 8.1");
}
else if (IsWindows8OrGreater())
{
wcscat_s(buffer, isServer ? L"Windows Server 2012" : L"Windows 8");
}
else if (IsWindows7OrGreater())
{
wcscat_s(buffer, isServer ? L"Windows Server 2008 R2" : L"Windows 7");
}
else if (IsWindowsVistaOrGreater())
{
wcscat_s(buffer, isServer ? L"Windows Server 2008" : L"Windows Vista");
}
else if (IsWindowsXPOrGreater())
{
if (GetSystemMetrics(SM_SERVERR2) != 0)
{
wcscat_s(buffer, L"Windows Server 2003 R2");
}
else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
{
wcscat_s(buffer, L"Windows Home Server");
}
else
{
wcscat_s(buffer, isServer ? L"Windows Server 2003" : L"Windows XP");
}
}
else
{
wcscat_s(buffer, L"Unknown");
if (IsWindows8Point1OrGreater()) return isServer ? L"Windows Server 2012 R2" : L"Windows 8.1";
if (IsWindows8OrGreater()) return isServer ? L"Windows Server 2012" : L"Windows 8";
if (IsWindows7OrGreater()) return isServer ? L"Windows Server 2008 R2" : L"Windows 7";
if (IsWindowsVistaOrGreater()) return isServer ? L"Windows Server 2008" : L"Windows Vista";
if (IsWindowsXPOrGreater()) return isServer ? L"Windows Server 2003" : L"Windows XP";
return L"Unknown";
}
if (getExtendedInfo && _wcsicmp(buffer, L"Unknown") != 0)
LPCWSTR GetPlatformFriendlyName()
{
if (IsWindowsVistaOrGreater())
{
PGETPRODUCTINFO pGetProductInfo = (PGETPRODUCTINFO)
GetProcAddress(GetModuleHandle(L"kernel32"), "GetProductInfo");
if (pGetProductInfo)
{
DWORD dwType;
if (pGetProductInfo(osvi.dwMajorVersion, osvi.dwMinorVersion,
osvi.wServicePackMajor, osvi.wServicePackMinor, &dwType))
{
// Only use most common versions.
// Full list at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724358%28v=vs.85%29.aspx
switch (dwType)
{
case PRODUCT_ULTIMATE:
case PRODUCT_ULTIMATE_N:
wcscat_s(buffer, L" Ultimate"); break;
static std::wstring name;
case PRODUCT_PROFESSIONAL:
case PRODUCT_PROFESSIONAL_N:
wcscat_s(buffer, L" Professional"); break;
WCHAR* buffer = new WCHAR[MAX_LINE_LENGTH];
DWORD size = MAX_LINE_LENGTH;
case PRODUCT_PROFESSIONAL_WMC:
wcscat_s(buffer, L" Professional Media Center"); break;
case PRODUCT_HOME_PREMIUM:
case PRODUCT_HOME_PREMIUM_N:
wcscat_s(buffer, L" Home Premium"); break;
case PRODUCT_HOME_BASIC:
case PRODUCT_HOME_BASIC_N:
wcscat_s(buffer, L" Home Basic"); break;
case PRODUCT_STARTER:
case PRODUCT_STARTER_N:
wcscat_s(buffer, L" Starter"); break;
case PRODUCT_ENTERPRISE:
case PRODUCT_ENTERPRISE_EVALUATION:
case PRODUCT_ENTERPRISE_N:
case PRODUCT_ENTERPRISE_N_EVALUATION:
wcscat_s(buffer, L" Enterprise"); break;
case PRODUCT_BUSINESS:
case PRODUCT_BUSINESS_N:
wcscat_s(buffer, L" Business"); break;
// Server Editions
case PRODUCT_CLUSTER_SERVER:
case PRODUCT_CLUSTER_SERVER_V:
wcscat_s(buffer, L" Cluster Edition"); break;
case PRODUCT_DATACENTER_SERVER:
case PRODUCT_DATACENTER_SERVER_V:
case PRODUCT_DATACENTER_SERVER_CORE:
case PRODUCT_DATACENTER_SERVER_CORE_V:
case PRODUCT_DATACENTER_EVALUATION_SERVER:
wcscat_s(buffer, L" Datacenter Edition"); break;
case PRODUCT_ENTERPRISE_SERVER:
case PRODUCT_ENTERPRISE_SERVER_V:
case PRODUCT_ENTERPRISE_SERVER_CORE:
case PRODUCT_ENTERPRISE_SERVER_CORE_V:
case PRODUCT_ENTERPRISE_SERVER_IA64:
wcscat_s(buffer, L" Enterprise Edition"); break;
case PRODUCT_SMALLBUSINESS_SERVER:
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE:
wcscat_s(buffer, L" Small Business Server"); break;
case PRODUCT_STANDARD_SERVER:
case PRODUCT_STANDARD_SERVER_V:
case PRODUCT_STANDARD_SERVER_CORE:
case PRODUCT_STANDARD_SERVER_CORE_V:
wcscat_s(buffer, L" Standard Edition"); break;
case PRODUCT_WEB_SERVER:
case PRODUCT_WEB_SERVER_CORE:
wcscat_s(buffer, L" Web Server"); break;
}
}
}
}
else if (IsWindowsXPOrGreater())
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
if (isServer)
if (RegQueryValueEx(hKey, L"ProductName", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
{
wcscat_s(buffer, L" Compute Cluster Edition");
}
else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
{
wcscat_s(buffer, L" Datacenter Edition");
}
else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
{
wcscat_s(buffer, L" Enterprise Edition");
}
else if (osvi.wSuiteMask & VER_SUITE_BLADE)
{
wcscat_s(buffer, L" Web Edition");
}
else
{
wcscat_s(buffer, L" Standard Edition");
}
}
else
{
if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
{
wcscat_s(buffer, L" Home Edition");
}
else if (GetSystemMetrics(SM_MEDIACENTER))
{
wcscat_s(buffer, L" Media Center Edition");
}
else if (GetSystemMetrics(SM_STARTER))
{
wcscat_s(buffer, L" Starter Edition");
}
else if (GetSystemMetrics(SM_TABLETPC))
{
wcscat_s(buffer, L" Tablet PC Edition");
}
else
{
return L" Professional";
}
}
}
// Build number and 32/64 bit
bool is64Bit = false;
bool has64Bit = Platform::GetPlatformBit(is64Bit);
_snwprintf_s(buffer, _TRUNCATE, L"%s %s-bit (build %d)",
buffer,
(has64Bit) ? (is64Bit) ? L"64" : L"32" : L"???",
osvi.dwBuildNumber);
bool has64Bit = GetPlatformBit(is64Bit);
// Service Pack
if (osvi.szCSDVersion)
name += buffer;
name += L' ';
name += (has64Bit) ? (is64Bit) ? L" 64" : L" 32" : L" ???";
name += L"-bit";
size = MAX_LINE_LENGTH;
if (RegQueryValueEx(hKey, L"CurrentBuildNumber", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
wcscat_s(buffer, L", ");
wcscat_s(buffer, osvi.szCSDVersion);
name += L" (build ";
name += buffer;
name += L')';
}
else if (RegQueryValueEx(hKey, L"CurrentBuild", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
name += L" (build ";
name += buffer;
name += L')';
}
size = MAX_LINE_LENGTH;
if (RegQueryValueEx(hKey, L"CSDVersion", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
name += L' ';
name += buffer;
}
}
return buffer;
RegCloseKey(hKey);
}
else
{
name = L"Windows version unknown";
}
delete [] buffer;
return name.c_str();
}
/*

View File

@ -19,6 +19,8 @@
#ifndef RM_COMMON_PLATFORM_H_
#define RM_COMMON_PLATFORM_H_
#define MAX_LINE_LENGTH 4096
namespace Platform {
typedef BOOL(WINAPI * LPFN_ISWOW64PROCESS)(HANDLE hProcess, PBOOL Wow64Process);
@ -28,7 +30,8 @@ typedef BOOL(WINAPI * PGETPRODUCTINFO)(DWORD dwOSMajorVersion,
DWORD dwSpMinorVersion,
PDWORD pdwReturnedProductType);
LPCWSTR GetPlatformName(bool getExtendedInfo = false);
LPCWSTR GetPlatformName();
LPCWSTR GetPlatformFriendlyName();
bool GetPlatformBit(bool& is64Bit);
} // namespace Platform

View File

@ -1239,7 +1239,7 @@ void DialogAbout::TabVersion::Initialize()
SetWindowText(item, tmpSz);
item = GetControl(Id_WinVerLabel);
SetWindowText(item, Platform::GetPlatformName(true));
SetWindowText(item, Platform::GetPlatformFriendlyName());
item = GetControl(Id_PathLabel);
std::wstring text = L"Path: " + GetRainmeter().GetPath();
@ -1289,7 +1289,7 @@ INT_PTR DialogAbout::TabVersion::OnCommand(WPARAM wParam, LPARAM lParam)
int len = _snwprintf_s(tmpSz, _TRUNCATE, L"%s%s r%i %s (%s)", APPVERSION, revision_beta ? L" beta" : L"", revision_number, APPBITS, APPDATE);
std::wstring text(tmpSz, len);
text += L'\n';
text += Platform::GetPlatformName(true);
text += Platform::GetPlatformFriendlyName();
text += L"\nPath: ";
text += GetRainmeter().GetPath();
text += L"\nIniFile: ";