[toc]

接口说明

在windows操作系统上,判断系统版本号一般都是使用 GetVersionEx 函数,但是该函数 windows8.1 以后被启用了,尽管可以通过添加宏来忽略 4996 的错误。在新的API中提供了新的接口以供判断操作系统版本:

  1. Versionhelpers.h 标头 - Win32 apps | Microsoft Learn
  2. 声明的宏

本文介绍如何使用 IsWindowsVersionOrGreater verifyVersionInfoW 函数。

关于如何查询windows版本号可以看这个文档: 更新 WINVER 和 _WIN32_WINNT | Microsoft Learn

Windows各个版本的宏

所需的最低系统 NTDDI_VERSION的值
Windows 10 1903 “19H1” NTDDI_WIN10_19H1 (0x0A000007)
Windows 10 1809 “红石 5” NTDDI_WIN10_RS5 (0x0A000006)
Windows 10 1803 “红石 4” NTDDI_WIN10_RS4 (0x0A000005)
Windows 10 1709 “红石 3” NTDDI_WIN10_RS3 (0x0A000004)
Windows 10 1703 “红石 2” NTDDI_WIN10_RS2 (0x0A000003)
Windows 10 1607 “红石 1” NTDDI_WIN10_RS1 (0x0A000002)
Windows 10 1511“阈值 2” NTDDI_WIN10_TH2 (0x0A000001)
Windows 10 1507“阈值” NTDDI_WIN10 (0x0A000000)
Windows 8.1 NTDDI_WINBLUE (0x06030000)
Windows 8 NTDDI_WIN8 (0x06020000)
Windows 7 NTDDI_WIN7 (0x06010000)
Windows Server 2008 NTDDI_WS08 (0x06000100)
Windows Vista Service Pack 1 (SP1) NTDDI_VISTASP1 (0x06000100)
Windows Vista NTDDI_VISTA (0x06000000)
Windows Server 2003 Service Pack 2 (SP2) NTDDI_WS03SP2 (0x05020200)
带有 Service Pack 1 (SP1) 的 Windows Server 2003 NTDDI_WS03SP1 (0x05020100)
Windows Server 2003 NTDDI_WS03 (0x05020000)
Windows XP Service Pack 3 (SP3) NTDDI_WINXPSP3 (0x05010300)
Windows XP Service Pack 2 (SP2) NTDDI_WINXPSP2 (0x05010200)
Windows XP Service Pack 1 (SP1) NTDDI_WINXPSP1 (0x05010100)
Windows XP NTDDI_WINXP (0x05010000)

补充

关于 函数的三个参数的输入可以参考文档中定义的宏:

1
2
3
#define OSVER(Version)  ((Version) & OSVERSION_MASK)
#define SPVER(Version) (((Version) & SPVERSION_MASK) >> 8)
#define SUBVER(Version) (((Version) & SUBVERSION_MASK) )

除此之外,还可以使用以下宏获取三个版本号:

1
2
3
4
5
6
7
8
9
10
11
/*
LOWORD()得到一个32bit数的低16bit
HIWORD()得到一个32bit数的高16bit
LOBYTE()得到一个16bit数最低(最右边)那个字节
HIBYTE()得到一个16bit数最高(最左边)那个字节
*/

#define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff))
#define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
#define LOBYTE(w) ((BYTE)(((DWORD_PTR)(w)) & 0xff))
#define HIBYTE(w) ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))

示例

IsWindowsVersionOrGreater 函数是 Windows API 中的一个函数,用于检查当前运行的操作系统版本是否满足指定的条件。以下是该函数的一些常见用法:

  1. 检查特定版本及以上的 Windows 版本:

1
2
3
4
5
6
7
#include <Windows.h>

// 示例:检查 Windows 7 及以上版本
if (IsWindowsVersionOrGreater(6, 1, 0))
{
// 在 Windows 7 或更高版本上执行某些操作
}
  1. 检查特定版本和 Service Pack 的 Windows 版本:

1
2
3
4
5
6
7
#include <Windows.h>

// 示例:检查 Windows 10 版本 1809(秋季创意者更新)及以上版本
if (IsWindowsVersionOrGreater(10, 0, 17763))
{
// 在 Windows 10 版本 1809(秋季创意者更新)或更高版本上执行某些操作
}
  1. 检查是否为特定 Windows Server 版本:

1
2
3
4
5
6
7
#include <Windows.h>

// 示例:检查是否为 Windows Server 2016 版本
if (IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN10), LOBYTE(_WIN32_WINNT_WIN10), 0))
{
// 在 Windows Server 2016 或更高版本上执行某些操作
}
  1. 检查特定版本、Service Pack 和产品类型的 Windows 版本:

1
2
3
4
5
6
7
#include <Windows.h>

// 示例:检查 Windows 8.1 Update 版本且为工作站类型
if (IsWindowsVersionOrGreater(6, 3, 0, VER_GREATER_EQUAL, VER_NT_WORKSTATION))
{
// 在 Windows 8.1 Update 版本且为工作站类型上执行某些操作
}

这些示例可以帮助你理解 IsWindowsVersionOrGreater 函数的各种用法,你可以根据实际需求在代码中适当调整参数和条件。

请注意,IsWindowsVersionOrGreater 是一个动态链接库函数,在使用时需要包含 <Windows.h> 头文件,且需要链接到 Version.lib 库。

判断 dwPlatformId

dwPlatformId 字段说明

在 Windows 系统中,常用的平台标识(Platform ID)定义如下:

  • VER_PLATFORM_WIN32s:表示 Win32s 子系统,它是为 Windows 3.1 提供的一个扩展运行环境,使得部分 32 位应用程序可以在 Windows 3.1 上运行。
  • VER_PLATFORM_WIN32_WINDOWS:表示 Windows 9x 系列操作系统,包括 Windows 95、Windows 98 和 Windows ME。这些操作系统都是基于 MS-DOS 的。
  • VER_PLATFORM_WIN32_NT:表示 Windows NT 系列操作系统,包括 Windows NT、Windows 2000、Windows XP、Windows Server 2003、Windows Vista、Windows 7、Windows 8、Windows 10 等版本。这些操作系统是基于全新的内核设计的,并具有更强大的功能和稳定性。

每个平台标识对应不同的操作系统系列,因此在开发应用程序时,根据需要选择正确的平台标识是很重要的。

判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool IsWindowsNT()
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
// osvi.dwPlatformId = VER_PLATFORM_WIN32s;
// osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
osvi.dwPlatformId = VER_PLATFORM_WIN32_NT;

DWORDLONG conditionMask = 0;
VER_SET_CONDITION(conditionMask, VER_PLATFORMID, VER_EQUAL);

return VerifyVersionInfo(&osvi, VER_PLATFORMID, conditionMask);
}