如何使用 Delphi 10.3.3 检测 Windows 11

How to detect Windows 11 using Delphi 10.3.3

我使用 TOSVersion.ToString 函数(使用 SysUtils)来检测 Windows 版本。然而,这就是我在 Windows11:

中得到的

Windows 10 (Version 10.0, Build 21996, 64-bit Edition)

有没有可靠的方法检测Windows 11?我正在使用 Delphi 10.3.3.

更新:Windows11 正式发布,我再次尝试。 这是我得到的:

Windows 10 (Version 10.0, Build 22000, 64-bit Edition)

正如 Remy 所指出的:使用 WinAPI 你可能会陷入某些 compatibility mode, resulting in getting a version reported that is lower than the actual.

  1. 一种替代方法是检查预期文件的文件版本,即

    • %windir%\system32\ntoskrnl.exe
    • %windir%\explorer.exe

    using GetFileVersionInfo() and VerQueryValue() - HiWord(dwFileVersionLS) 应该是 22000 或更高(根据 Windows NT build/release number)。

  2. 另一种是在 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ 下的 Registry 中查找文本 values CurrentBuild and CurrentBuildNumber,检查两者的最高值是否为 22000 或更高。

  3. David 已经在 Checking Windows version on W10 with even more alternatives, although concentrating on the high/low version numbers, not the build. But WMI might help 中写了详细的答案。

  4. (这仅在确认知识的回顾中有效。) 检查哪些 API 导出 可用:这个想法是特定的功能是通过特定的 Windows releases/versions 引入的,因此如果导入失败,您就知道您使用的是以下版本。 outdated example and an outdated list of minimum versions per function 会给你一个想法。现在你“只需”找出 Windows 11.

    引入了哪些新功能

这些都不是万无一失的,但你可以把它们结合起来,然后得出结论。毕竟你仍然可以尝试你的方法来解析文本而不是仅仅依赖数字。它还展示了您可以多么轻松地操纵您的系统,根据使用的方法报告不同的版本。

Windows11 的官方主要版本号是 10。

The official build number for the public preview of Windows 11 is 10.0.22000.168

Earlier builds:

  • 10.0.22000.71
  • 10.0.22000.65
  • 10.0.22000.51

Microsoft Windows 11 FAQ

如果要检测预览版,最早的内部版本号是 10.0.22000.51 Windows 11 version history

TOSVersion 依赖于一些硬编码名称和 return OS 名称的逻辑。您将必须实施自己的检测、复制和修改 TOSVersion 记录或对其进行包装,您可以在其中使用旧版本的现有逻辑并根据 Windows 11 内部版本号实施检查以检测 Windows11.

检测OS版本的其他常见问题和方法可以参考

除了非常弱的,至少对我来说,考虑 Windows 10 builds 大于 22000 的解决方案,例如 Windows 11,我发现唯一有效的解决方案是 WMIs Win32_OperatingSystem class - Caption 属性.

在我的开发 Win10 机器上,它给出了以下字符串:Microsoft Windows 10 Pro.

在我的另一台安装了 Win11 的开发机器上,同样的功能给出:Microsoft Windows 11 Pro

区别在于字符串值——“10”与“11”——但这至少比“构建大于”解决方案要好得多。

C# 和 C++ 运行良好。

最简单的方法是获取 Kernel32.dll 的版本,如果 Major Version 是 10 并且 Build Version >= 22000 那么你有 Windows 11.

在这里查看我的代码:How can I find the Windows product name in Windows 11?