C++ 如何检测 Windows 10

2022-01-15 00:00:00 windows windows-10 c++

我多年前编写了一个 PC 审计工具,并且一直在更新它.基本功能之一是报告正在审核的 PC 上运行的 Windows 版本,我一直使用 GetVersionEx 调用.

I have written a PC auditing tool many years ago and have been keeping it up to date. One of basic functions is reporting the version of Windows running on the PC being audited for which I have always used the GetVersionEx call.

这适用于 Windows 8(包括 Windows 8),但在 Windows 10 下不受支持,实际上 Windows 10 与 Windows 8 一样返回 8.2.Microsoft 似乎没有引入任何东西作为直接替代,而是建议您检查所需的特定功能而不是查看操作系统,但出于审核目的,我实际上想要操作系统名称.

This works up to and including Windows 8 but is not supported under Windows 10 and indeed Windows 10 returns 8.2 just as windows 8 does. Microsoft do not seem to have introduced anything as a direct replacement suggesting instead that you check for specific features required rather than looking at the OS but for the purpose of the audit I actually want the OS name.

扫描仪"是一个必须在非特权帐户下运行的 C++ 程序,因此我认为我没有阅读过其他建议 - 选择系统 DLL 的版本(例如 kernel32.dll)将像这些文件夹一样工作通常用户无法访问.

The 'scanner' is a C++ program which must run under non-privileged accounts so I don't think another suggestion I have read - picking up the version of a system DLL such as kernel32.dll will work as these folders are typically not accessible to users.

欢迎任何其他建议/想法!

Any other suggestions/thoughts are most welcome!

推荐答案

从 Windows 8.1 开始,GetVersion()GetVersionEx() 以应用表现为准:

Starting in Windows 8.1, GetVersion() and GetVersionEx() are subject to application manifestation:

随着 Windows 8.1 的发布,GetVersionEx API 的行为发生了变化,它将为操作系统版本返回的值.GetVersionEx 函数返回的值现在取决于应用程序的显示方式.

With the release of Windows 8.1, the behavior of the GetVersionEx API has changed in the value it will return for the operating system version. The value returned by the GetVersionEx function now depends on how the application is manifested.

未针对 Windows 8.1 或 Windows 10 显示的应用程序将返回 Windows 8 操作系统版本值 (6.2).一旦针对给定的操作系统版本显示应用程序,GetVersionEx 将始终返回应用程序在未来版本中显示的版本.要为 Windows 8.1 或 Windows 10 显示您的应用程序,请参阅 定位您的Windows 应用程序.

Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows.

较新的 Version Helper 函数只是简单的包装器对于 VerifyVersionInfo().从 Windows 10 开始,它现在也受到表现形式的影响:

The newer Version Helper functions are simply wrappers for VerifyVersionInfo(). Starting in Windows 10, it is now subject to manifestation as well:

Windows 10:如果 lpVersionInfo 参数设置为指定Windows 8.1 或 Windows 10,即使当前操作系统版本是 Windows 8.1 或 Windows 10.具体而言,VerifyVersionInfo 具有以下行为:

Windows 10: VerifyVersionInfo returns false when called by applications that do not have a compatibility manifest for Windows 8.1 or Windows 10 if the lpVersionInfo parameter is set so that it specifies Windows 8.1 or Windows 10, even when the current operating system version is Windows 8.1 or Windows 10. Specifically, VerifyVersionInfo has the following behavior:

  • 如果应用程序没有清单,VerifyVersionInfo 的行为就像操作系统版本是 Windows 8 (6.2).
  • 如果应用程序的清单包含与 Windows 8.1 对应的 GUID,则 VerifyVersionInfo 的行为就像操作系统版本是 Windows 8.1 (6.3).
  • 如果应用程序的清单包含与 Windows 10 对应的 GUID,则 VerifyVersionInfo 的行为就像操作系统版本是 Windows 10 (10.0).
  • If the application has no manifest, VerifyVersionInfo behaves as if the operation system version is Windows 8 (6.2).
  • If the application has a manifest that contains the GUID that corresponds to Windows 8.1, VerifyVersionInfo behaves as if the operation system version is Windows 8.1 (6.3).
  • If the application has a manifest that contains the GUID that corresponds to Windows 10, VerifyVersionInfo behaves as if the operation system version is Windows 10 (10.0).

Version Helper 函数使用 VerifyVersionInfo 函数,所以行为 IsWindows8Point1OrGreaterIsWindows10OrGreater 同样受清单的存在和内容的影响.

The Version Helper functions use the VerifyVersionInfo function, so the behavior IsWindows8Point1OrGreater and IsWindows10OrGreater are similarly affected by the presence and content of the manifest.

要为 Windows 8.1 或 Windows 10 显示您的应用程序,请参阅 针对 Windows 的应用程序.

To manifest your applications for Windows 8.1 or Windows 10, see Targeting your application for Windows.

为了获得 true 操作系统版本(无论表现形式如何),微软建议查询系统 DLL 的文件版本:

To get the true OS version regardless of manifestation, Microsoft suggests querying the file version of a system DLL:

获取系统版本

要获取操作系统的完整版本号,请在系统 DLL 之一上调用 GetFileVersionInfo 函数,例如 Kernel32.dll,然后调用 VerQueryValue获取文件版本信息的\StringFileInfo\<lang><codepage>\ProductVersion子块.

To obtain the full version number for the operating system, call the GetFileVersionInfo function on one of the system DLLs, such as Kernel32.dll, then call VerQueryValue to obtain the \StringFileInfo\<lang><codepage>\ProductVersion subblock of the file version information.

另一种方法是使用 RtlGetVersion(), NetServerGetInfo(),或 NetWkstaGetInfo() 代替.它们都报告了准确的操作系统版本,并且不受表现形式的影响(还没有?).

Another way is to use RtlGetVersion(), NetServerGetInfo(), or NetWkstaGetInfo() instead. They all report an accurate OS version and are not subject to manifestation (yet?).

相关文章