Windows 8 之前最常用获取操作系统版本的方法是调用 GetVersionEx 函数。而从 Windows8.1 之后,GetVersionEx 这个API被微软明文废弃了,Windows8.1开始之后(包括Windows10),这个API常规情况下返回值都是 6.2。微软MSDN官方给出的说法是 Windows 8 以后可以使用 VersionHelpers 中的 IsWindowsXPOrGreater、IsWindowsXPSP1OrGreater、IsWindowsVistaOrGreater、IsWindows7OrGreater、IsWindows8Point1OrGreater、IsWindows10OrGreater、IsWindowsServer等系列函数,但这些函数在 XP 和 Win7 中并不兼容。
下面给出一种方法获取 Windows XP ~ Windows 10 全系列版本方法,该方法只能获取内核版本号,不能确定详细版本,若要获取详细系统版本,可参考获取Windows操作系统版本(1)
原理:
默认情况下,系统中进程环境块(PEB)的0xA8偏移指向操作系统次版本号(MinorVersion),0xA4偏移指向操作系统主版本号(MajorVersion),通过主版本号和此版本我们便可盘点操作系统的大概版本。
代码:
void Z_GetSimpleSystemVersion(__out char* pszOSVersion, __in const int nOSSize)
{
if (pszOSVersion != NULL)
{
memset(pszOSVersion, 0, nOSSize);
}
DWORD dwMajor, dwMinor;
char *pszOS = "*";
_asm
{
pushad
mov ebx, fs:[0x18];
mov eax, fs:[0x30];
mov ebx, [eax + 0A8h];
mov eax, [eax + 0A4h];
mov dwMinor, ebx
mov dwMajor, eax
popad
}
if (dwMajor <= 4)
pszOS = "Windows NT";
if (dwMajor == 5 && dwMinor == 0)
pszOS = "Windows 2000";
if (dwMajor == 5 && dwMinor == 1)
pszOS = "Windows XP";
if (dwMajor == 5 && dwMinor == 2)
pszOS = "Windows Server 2003";
if (dwMajor == 6 && dwMinor == 0)
pszOS = "Windows Vista | Windows Server 2008";
if (dwMajor == 6 && dwMinor == 0)
pszOS = "Windows Server 2008";
if (dwMajor == 6 && dwMinor == 1)
pszOS = "Windows Server 2008R2";
if (dwMajor == 6 && dwMinor == 1)
pszOS = "Windows 7";
if (dwMajor == 6 && dwMinor == 2)
pszOS = "Windows 8 | Windows Server 2012";
if (dwMajor == 6 && dwMinor == 3)
pszOS = "Windows Server 2012R2";
if (dwMajor == 6 && dwMinor == 3)
pszOS = "Windows 8.1";
if (dwMajor == 10)
pszOS = "Windows 10";
else
pszOS = "*";
memcpy(pszOSVersion, pszOS, strlen(pszOS));
}