c++ - 检查 Windows 版本

标签 c++ windows version

如果计算机上安装的 Windows 版本是 Windows Vista 及更高版本 (Windows 7),我如何检查 C++?

最佳答案

此线程中的所有答案都指向您使用 GetVersionGetVersionEx 进行此测试,这是不正确。它似乎有效,但它是有风险的。 Windows 操作系统升级的 appcompat 问题的主要来源是基于 GetVersion 结果的编写不佳的测试,带有错误的假设或错误的比较。

进行此测试的正确方法是使用 VerifyVersionInfo,而不是 GetVersionGetVersionEx

如果您使用的是 VS 2013 编译器工具集和 Windows 8.1 SDK,则可以使用 VersionHelpers.h 并只需调用 IsWindowsVistaOrGreater

If you are using the VS 2013 v120_xp platform toolset to target Windows XP, you are actually using the Windows 7.1A SDK, so you need to use VeriyVersionInfo directly.

否则,使用:

bool IsWindowsVistaOrGreater()
{
OSVERSIONINFOEXW osvi = {};
osvi.dwOSVersionInfoSize = sizeof(osvi);
DWORDLONG const dwlConditionMask = VerSetConditionMask(
    VerSetConditionMask(
    VerSetConditionMask(
            0, VER_MAJORVERSION, VER_GREATER_EQUAL),
               VER_MINORVERSION, VER_GREATER_EQUAL),
               VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_VISTA);
osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_VISTA);
osvi.wServicePackMajor = 0;

return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
}

此代码可在 Windows 2000 或更高版本上运行,并为您提供可靠的结果。如果您确实需要此测试在 Windows 98 或 Windows ME 上运行 - 并且 - 您正在使用足够老的编译器工具集以在该平台上实际运行,您将执行相同的测试,但使用显式链接而不是隐式链接。 What's in a version number?

此外,在 Windows 8.1 和 Windows 10 上使用 GetVersionGetVersionEx 默认会得到错误的版本。见 Manifest Madness .

Note that with Windows 10 VerifyVersionInfo is also subject to the same manifest-based behavior (i.e. without the GUID element for Windows 10, VVI acts as if the OS version number is 6.2 rather than 10.0. That said, most real-world tests like IsWindowsVistaOrGreater, IsWindows7OrGreater, IsWindows7SP1OrGreater, IsWindows8OrGreater are all going to work just fine even without the manifest. It's only if you are using IsWindows8Point1OrGreater or IsWindows10OrGreater that the manifest-based behavior even matters.

另见 this堆栈溢出线程。

关于c++ - 检查 Windows 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1963992/

相关文章:

c# - Windows Mobile 中的 openCV

c++ - 使用 Qt 通过 GPIB 编程电源

windows - Google Cloud SDK 安装程序在 Windows 7 上失败(gcloud 崩溃(UnicodeDecodeError))

hbase - 我使用的是哪个版本的 hbase?

android - 将 APK 上传到 Android Market 时出现 minSdkVersion 错误

c++ - 排序列表 - 最好的方法

c - 数据类型大小是否因计算机而异?

windows - 一段时间后 TToolbar 停止显示按钮标题

数据库版本控制

c++ - 在 C++ 中从现有数组创建子数组的最佳方法是什么?