如果计算机上安装的 Windows 版本是 Windows Vista 及更高版本 (Windows 7),我如何检查 C++?
最佳答案
此线程中的所有答案都指向您使用 GetVersion
或 GetVersionEx
进行此测试,这是不正确。它似乎有效,但它是有风险的。 Windows 操作系统升级的 appcompat 问题的主要来源是基于 GetVersion
结果的编写不佳的测试,带有错误的假设或错误的比较。
进行此测试的正确方法是使用 VerifyVersionInfo
,而不是 GetVersion
或 GetVersionEx
。
如果您使用的是 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 useVeriyVersionInfo
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 上使用 GetVersion
或 GetVersionEx
默认会得到错误的版本。见 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 likeIsWindowsVistaOrGreater
,IsWindows7OrGreater
,IsWindows7SP1OrGreater
,IsWindows8OrGreater
are all going to work just fine even without the manifest. It's only if you are usingIsWindows8Point1OrGreater
orIsWindows10OrGreater
that the manifest-based behavior even matters.
另见 this堆栈溢出线程。
关于c++ - 检查 Windows 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1963992/