C++ 检查 Windows 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1963992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check Windows version
提问by Matej
How I can check in C++ if Windows version installed on computer is Windows Vista and higher (Windows 7)?
如果计算机上安装的 Windows 版本是 Windows Vista 及更高版本(Windows 7),我如何检查 C++?
采纳答案by wallyk
Similar to other tests for checking the version of Windows NT:
与检查 Windows NT 版本的其他测试类似:
OSVERSIONINFO vi;
memset (&vi, 0, sizeof vi);
vi .dwOSVersionInfoSize = sizeof vi;
GetVersionEx (&vi);
if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT && vi.dwMajorVersion >= 6)
回答by Chuck Walbourn
All the answers in this thread point you to using GetVersion
or GetVersionEx
for this test, which is incorrect. It seems to work, but it is risky. The primary source of appcompat problems for Windows OS upgrades comes from poorly written tests based on GetVersion
results with bad assumptions or buggy comparisons.
此线程中的所有答案都指向您使用GetVersion
或GetVersionEx
用于此测试,这是不正确的。这似乎有效,但风险很大。Windows 操作系统升级的 appcompat 问题的主要来源来自基于GetVersion
错误假设或错误比较的结果编写的糟糕测试。
The correct way to do this test is to use VerifyVersionInfo
, not GetVersion
or GetVersionEx
.
进行此测试的正确方法是使用VerifyVersionInfo
、 不GetVersion
或GetVersionEx
。
If you are using the VS 2013 compiler toolset and the Windows 8.1 SDK, you can use the VersionHelpers.h
and just call IsWindowsVistaOrGreater
.
如果您使用的是 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.
如果你使用VS 2013
v120_xp
平台工具集来定位Windows XP,你实际上是在使用Windows 7.1A SDK,所以需要VeriyVersionInfo
直接使用。
Otherwise, use:
否则,请使用:
bool IsWindowsVistaOrGreater()
{
OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0 };
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;
}
This code will work on Windows 2000 or later and give you a robust result. If you really needed this test to run on Windows 98 or Windows ME -and- you are using a compiler toolset old enough to actually run on that platform, you'd do the same test but with explicit rather than implicit linking. What's in a version number?
此代码适用于 Windows 2000 或更高版本,并为您提供可靠的结果。如果您确实需要在 Windows 98 或 Windows ME 上运行此测试——并且您使用的编译器工具集足够老,可以在该平台上实际运行,那么您将执行相同的测试,但使用显式而不是隐式链接。版本号中有什么?
Furthermore, using GetVersion
or GetVersionEx
will by default get the wrong versionon Windows 8.1 and Windows 10. See Manifest Madness.
此外,在 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.
请注意,与Windows 10
VerifyVersionInfo
也受到相同的基于清单行为(即没有为Windows 10的GUID元素,VVI行为,如果操作系统的版本号是6.2,而不是10.0。这就是说,最现实世界的测试,如IsWindowsVistaOrGreater
,IsWindows7OrGreater
,IsWindows7SP1OrGreater
,IsWindows8OrGreater
即使没有清单也能正常工作。只有当你正在使用IsWindows8Point1OrGreater
或者IsWindows10OrGreater
基于清单的行为甚至很重要时。
See also thisstack overflow thread.
另请参阅此堆栈溢出线程。
回答by Mehrdad Afshari
Use GetVersionExAPI function defined in kernel32.dll
:
使用中定义的GetVersionExAPI 函数kernel32.dll
:
bool IsWindowsVistaOrHigher() {
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
return osvi.dwMajorVersion >= 6;
}
回答by Hiale
In Visual Studio 2013 or higher, you can also use the new Version Helper functions.
在 Visual Studio 2013 或更高版本中,您还可以使用新的 Version Helper 函数。
There are methods for many different Windows versions. Example:
有许多不同的 Windows 版本的方法。例子:
#include <VersionHelpers.h>
if (!IsWindowsVistaOrGreater())
{
MessageBox(NULL, "You need at least Windows Vista", "Version Not Supported", MB_OK);
}
More information here
更多信息在这里
回答by Adam Maras
I think you're looking for the GetVersionExfunction.
我认为您正在寻找GetVersionEx函数。
回答by ChrisF
This Microsoft support pagegives you details for older versions.
此Microsoft 支持页面为您提供旧版本的详细信息。
To determine the operating system that is running on a given system, the following data is needed:
95 98 ME NT 4 2000 XP PlatformID 1 1 1 2 2 2 Major version 4 4 4 4 5 5 Minor version 0 10 90 0 0 1
要确定在给定系统上运行的操作系统,需要以下数据:
95 98 ME NT 4 2000 XP PlatformID 1 1 1 2 2 2 Major version 4 4 4 4 5 5 Minor version 0 10 90 0 0 1
You could implement the code and run it on a Vista and Windows-7 machine to check the values returned.
您可以实现代码并在 Vista 和 Windows-7 机器上运行它以检查返回的值。
To get the operating system version information make the following call:
要获取操作系统版本信息,请进行以下调用:
System::OperatingSystem *osInfo = System::Environment::OSVersion;
回答by jpyllman
You could use the GetVersion() or GetVersionEx() function in the kernel32.dll. This two functions are only available on Windows 2000 or later.
您可以使用 kernel32.dll 中的 GetVersion() 或 GetVersionEx() 函数。这两个功能仅在 Windows 2000 或更高版本上可用。
To read more about this look at http://msdn.microsoft.com/en-us/library/ms724451%28VS.85%29.aspx.
要阅读有关此内容的更多信息,请访问http://msdn.microsoft.com/en-us/library/ms724451%28VS.85%29.aspx。