获取操作系统版本,其实是一件很简单的事,我们只需知道常用的win7,win8,win10等等的一些信息,再从SYSTEM_INFO结构体中匹配就可以了。但要想获得完整的,各个版本兼容适用的,还是得花时间查找资料来完成。如果已经花时间查过了,最好记录下来,下次就可以少花时间。
// WindowsOS.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <strsafe.h>
#include <tchar.h>
BOOL GetOSDisplayString(LPTSTR pszOS);
int main()
{
TCHAR szOS[MAX_PATH] = _T("");
if (GetOSDisplayString(szOS))
{
printf("OS: %ls
", szOS);
}
return 0;
}
BOOL GetOSDisplayString(LPTSTR pszOS)
{
typedef void (WINAPI *LPFN_SYSTEM_INFO)(LPSYSTEM_INFO); //SYSTEM_INFO
typedef BOOL(WINAPI *LPFN_GETNATIVE)(DWORD, DWORD, DWORD, DWORD, PDWORD);
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
LPFN_SYSTEM_INFO psi;
LPFN_GETNATIVE GetNative;
BOOL bOsVersionInfoEx;
DWORD dwType;
SecureZeroMemory(&si, sizeof(SYSTEM_INFO));
SecureZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
typedef LONG(WINAPI* LPFN_RTLGETVERSION)(RTL_OSVERSIONINFOEXW*);
HMODULE h_NtDll = GetModuleHandleW(L"ntdll.dll");
LPFN_RTLGETVERSION f_RtlGetVersion = (LPFN_RTLGETVERSION)GetProcAddress(h_NtDll, "RtlGetVersion");
bOsVersionInfoEx = f_RtlGetVersion((RTL_OSVERSIONINFOEXW*)&osvi);
psi = (LPFN_SYSTEM_INFO)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
if (NULL != psi)
{
psi(&si);
}
else GetSystemInfo(&si);
if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
{
StringCchCopy(pszOS, MAX_PATH, TEXT("Microsoft "));
if (osvi.dwMajorVersion == 10)
{
if (osvi.dwMinorVersion == 0)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
StringCchCat(pszOS, MAX_PATH, TEXT("Windows 10 "));
else
StringCchCat(pszOS, MAX_PATH, TEXT("Windows Server 2016 Technical Preview "));
}
}
else if (osvi.dwMajorVersion == 6)
{
if (osvi.dwMinorVersion == 0)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
StringCchCat(pszOS, MAX_PATH, TEXT("Windows Vista "));
else StringCchCat(pszOS, MAX_PATH, TEXT("Windows Server 2008 "));
}
if (osvi.dwMinorVersion == 1)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
StringCchCat(pszOS, MAX_PATH, TEXT("Windows 7 "));
else StringCchCat(pszOS, MAX_PATH, TEXT("Windows Server 2008 R2 "));
}
if (osvi.dwMinorVersion == 2)
{
if (osvi.wProductType == VER_NT_WORKSTATION)
StringCchCat(pszOS, MAX_PATH, TEXT("Windows 8 "));
else
StringCchCat(pszOS, MAX_PATH, TEXT("Windows Server 2012"));
}
GetNative = (LPFN_GETNATIVE)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")),
"GetProductInfo");
GetNative(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
switch (dwType)
{
case PRODUCT_ULTIMATE:
StringCchCat(pszOS, MAX_PATH, TEXT("Ultimate Edition"));
break;
case PRODUCT_PROFESSIONAL:
StringCchCat(pszOS, MAX_PATH, TEXT("Professional"));
break;
case PRODUCT_HOME_PREMIUM:
StringCchCat(pszOS, MAX_PATH, TEXT("Home Premium Edition"));
break;
case PRODUCT_HOME_BASIC:
StringCchCat(pszOS, MAX_PATH, TEXT("Home Basic Edition"));
break;
case PRODUCT_ENTERPRISE:
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise Edition"));
break;
case PRODUCT_BUSINESS:
StringCchCat(pszOS, MAX_PATH, TEXT("Business Edition"));
break;
case PRODUCT_STARTER:
StringCchCat(pszOS, MAX_PATH, TEXT("Starter Edition"));
break;
case PRODUCT_CLUSTER_SERVER:
StringCchCat(pszOS, MAX_PATH, TEXT("Cluster Server Edition"));
break;
case PRODUCT_DATACENTER_SERVER:
StringCchCat(pszOS, MAX_PATH, TEXT("Datacenter Edition"));
break;
case PRODUCT_DATACENTER_SERVER_CORE:
StringCchCat(pszOS, MAX_PATH, TEXT("Datacenter Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER:
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise Edition"));
break;
case PRODUCT_ENTERPRISE_SERVER_CORE:
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise Edition (core installation)"));
break;
case PRODUCT_ENTERPRISE_SERVER_IA64:
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise Edition for Itanium-based Systems"));
break;
case PRODUCT_SMALLBUSINESS_SERVER:
StringCchCat(pszOS, MAX_PATH, TEXT("Small Business Server"));
break;
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
StringCchCat(pszOS, MAX_PATH, TEXT("Small Business Server Premium Edition"));
break;
case PRODUCT_STANDARD_SERVER:
StringCchCat(pszOS, MAX_PATH, TEXT("Standard Edition"));
break;
case PRODUCT_STANDARD_SERVER_CORE:
StringCchCat(pszOS, MAX_PATH, TEXT("Standard Edition (core installation)"));
break;
case PRODUCT_WEB_SERVER:
StringCchCat(pszOS, MAX_PATH, TEXT("Web Server Edition"));
break;
}
}
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
{
if (GetSystemMetrics(SM_SERVERR2))
StringCchCat(pszOS, MAX_PATH, TEXT("Windows Server 2003 R2, "));
else if (osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER)
StringCchCat(pszOS, MAX_PATH, TEXT("Windows Storage Server 2003"));
else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
StringCchCat(pszOS, MAX_PATH, TEXT("Windows Home Server"));
else if (osvi.wProductType == VER_NT_WORKSTATION &&
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
StringCchCat(pszOS, MAX_PATH, TEXT("Windows XP Professional x64 Edition"));
}
else StringCchCat(pszOS, MAX_PATH, TEXT("Windows Server 2003, "));
if (osvi.wProductType != VER_NT_WORKSTATION)
{
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
{
if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
StringCchCat(pszOS, MAX_PATH, TEXT("Datacenter Edition for Itanium-based Systems"));
else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise Edition for Itanium-based Systems"));
}
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
StringCchCat(pszOS, MAX_PATH, TEXT("Datacenter x64 Edition"));
else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise x64 Edition"));
else StringCchCat(pszOS, MAX_PATH, TEXT("Standard x64 Edition"));
}
else
{
if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
StringCchCat(pszOS, MAX_PATH, TEXT("Compute Cluster Edition"));
else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
StringCchCat(pszOS, MAX_PATH, TEXT("Datacenter Edition"));
else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
StringCchCat(pszOS, MAX_PATH, TEXT("Enterprise Edition"));
else if (osvi.wSuiteMask & VER_SUITE_BLADE)
StringCchCat(pszOS, MAX_PATH, TEXT("Web Edition"));
else StringCchCat(pszOS, MAX_PATH, TEXT("Standard Edition"));
}
}
}
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
{
StringCchCat(pszOS, MAX_PATH, TEXT("Windows XP "));
if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
StringCchCat(pszOS, MAX_PATH, TEXT("Home Edition"));
else StringCchCat(pszOS, MAX_PATH, TEXT("Professional"));
}
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
{
StringCchCat(pszOS, MAX_PATH, TEXT("Windows 2000 "));
if (osvi.wProductType == VER_NT_WORKSTATION)
{
StringCchCat(pszOS, MAX_PATH, TEXT("Professional"));
}
else
{
if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
StringCchCat(pszOS, MAX_PATH, TEXT("Datacenter Server"));
else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
StringCchCat(pszOS, MAX_PATH, TEXT("Advanced Server"));
else StringCchCat(pszOS, MAX_PATH, TEXT("Server"));
}
}
size_t targetSize;
StringCchLength(osvi.szCSDVersion, MAX_PATH, &targetSize);
if (targetSize > 0)
{
StringCchCat(pszOS, MAX_PATH, TEXT(" "));
StringCchCat(pszOS, MAX_PATH, osvi.szCSDVersion);
}
TCHAR buf[80];
StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
StringCchCat(pszOS, MAX_PATH, buf);
if (osvi.dwMajorVersion >= 6)
{
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
StringCchCat(pszOS, MAX_PATH, TEXT(" 64-bit"));
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
StringCchCat(pszOS, MAX_PATH, TEXT(" 32-bit"));
}
return TRUE;
}
else
{
return FALSE;
}
}