This commit is contained in:
Birunthan Mohanathas 2014-02-13 19:21:38 +02:00
parent c71585a818
commit 7a1c0f6e78
2 changed files with 23 additions and 72 deletions

View File

@ -23,15 +23,15 @@ namespace Platform {
LPCWSTR GetPlatformName()
{
bool isServer = IsWindowsServer();
const bool isServer = IsWindowsServer();
// Note: Place newer versions at the top.
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";
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";
}
@ -39,38 +39,30 @@ LPCWSTR GetPlatformName()
std::wstring GetPlatformFriendlyName()
{
std::wstring name;
WCHAR* buffer = new WCHAR[MAX_LINE_LENGTH];
DWORD size = MAX_LINE_LENGTH;
WCHAR buffer[256];
DWORD size = _countof(buffer);
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
if (RegQueryValueEx(hKey, L"ProductName", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
bool is64Bit = false;
bool has64Bit = GetPlatformBit(is64Bit);
name += buffer;
name += L' ';
name += (has64Bit) ? (is64Bit) ? L" 64" : L" 32" : L" ???";
name += Is64BitWindows() ? L"64" : L"32";
name += L"-bit";
size = MAX_LINE_LENGTH;
if (RegQueryValueEx(hKey, L"CurrentBuildNumber", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
name += L" (build ";
name += buffer;
name += L')';
}
else if (RegQueryValueEx(hKey, L"CurrentBuild", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
size = _countof(buffer);
if (RegQueryValueEx(hKey, L"CurrentBuildNumber", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS ||
RegQueryValueEx(hKey, L"CurrentBuild", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
name += L" (build ";
name += buffer;
name += L')';
}
size = MAX_LINE_LENGTH;
size = _countof(buffer);
if (RegQueryValueEx(hKey, L"CSDVersion", nullptr, nullptr, (LPBYTE)buffer, (LPDWORD)&size) == ERROR_SUCCESS)
{
name += L' ';
@ -85,59 +77,27 @@ std::wstring GetPlatformFriendlyName()
name = L"Windows version unknown";
}
delete [] buffer;
return name;
}
/*
** Returns |true| if the OS architecture can be found (either 32-bit or 64-bit),
** or |false| if it cannot be determined.
**
** Note: IsWow64Process was introduced with Windows XP SP2.
** Returns |true| if running on 64-bit Windows.
*/
bool GetPlatformBit(bool& is64Bit)
bool Is64BitWindows()
{
#if _WIN64
is64Bit = true;
return true;
#endif
#elif _WIN32
BOOL isWow64 = FALSE;
LPFN_ISWOW64PROCESS fnIsWow64Process =
(LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process");
if (fnIsWow64Process)
typedef BOOL(WINAPI * IsWow64ProcessFunc)(HANDLE hProcess, PBOOL Wow64Process);
auto isWow64Process = (IsWow64ProcessFunc)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process");
if (isWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(), &isWow64))
{
return false;
}
if (isWow64)
{
is64Bit = true;
}
else
{
is64Bit = false;
}
return true;
BOOL isWow64 = FALSE;
return isWow64Process(GetCurrentProcess(), &isWow64) && isWow64;
}
else
{
return false;
}
#else
return false;
#endif
}
} // namespace Platform

View File

@ -21,20 +21,11 @@
#include <string>
#define MAX_LINE_LENGTH 4096
namespace Platform {
typedef BOOL(WINAPI * LPFN_ISWOW64PROCESS)(HANDLE hProcess, PBOOL Wow64Process);
typedef BOOL(WINAPI * PGETPRODUCTINFO)(DWORD dwOSMajorVersion,
DWORD dwOSMinorVersion,
DWORD dwSpMajorVersion,
DWORD dwSpMinorVersion,
PDWORD pdwReturnedProductType);
LPCWSTR GetPlatformName();
std::wstring GetPlatformFriendlyName();
bool GetPlatformBit(bool& is64Bit);
bool Is64BitWindows();
} // namespace Platform