【问题标题】:Detecting Windows 11 properly正确检测 Windows 11
【发布时间】:2021-12-18 13:01:26
【问题描述】:

我是 FOSS 游戏 0 AD (https://play0ad.com) 的开发人员,我最近意识到我们对 Windows 11 的 Windows 版本的检测失败,就像在 Windows 8.1 和 Windows 10 之间一样。

它报告的 Windows 10.0.22000 在技术上是正确的版本,但我们希望它报告 Windows 11。

这是我们当前使用的代码

出于兼容性原因,我们坚持使用 Windows 7 SDK。

简单的解决办法是更换

    if (osInfo.dwMajorVersion >= 10)
    {
        stream << "Win" << osInfo.dwMajorVersion;
    }

通过

    if (osInfo.dwMajorVersion >= 10)
    {
        if (osInfo.dwMinorVersion > 22000)
            stream << "Win" << 11;
        else
            stream << "Win" << osInfo.dwMajorVersion;
    }

是否有更强大/面向未来的解决方案。

提前致谢!

【问题讨论】:

  • 如果(satya.nadella.v == 2.1)
  • This post 可能会有所帮助。
  • 谢谢@LouisGo 看来我们必须再次解决它:/

标签: c++ windows open-source


【解决方案1】:

我不确定这是否可行,但你可以试试这个代码吗?它使用 sysinfoapi.h 文件。根据文档,它只适用于 Windows。

#include <iostream>
#include <sysinfoapi.h>

void print_os_info()
{
    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

    GetVersionEx((LPOSVERSIONINFO)&info);

    printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);
}

int main()
{
    print_os_info();
}

编辑:显然,未针对 Windows 8.1 或 Windows 10 显示的应用程序将根据 sysinfoapi.h docs\

返回 Windows 8 操作系统版本值 (6.2)

再次编辑:我尝试使用带有内容的 $(Filename).exe.manifest 文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <assemblyIdentity
        type="win32"
        name="Contoso.ExampleApplication.ExampleBinary"
        version="1.2.3.4"
        processorArchitecture="x86"
    />
    <description>Contoso Example Application</description>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <!-- Windows 10 and Windows 11 -->
            <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- Windows 8 -->
            <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
            <!-- Windows 7 -->
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
            <!-- Windows Vista -->
            <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
        </application>
    </compatibility>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <!--
                  UAC settings:
                  - app should run at same integrity level as calling process
                  - app does not need to manipulate windows belonging to
                    higher-integrity-level processes
                  -->
                <requestedExecutionLevel
                    level="asInvoker"
                    uiAccess="false"
                />   
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

哪个对我有用并打印出 Windows 10.0 而不是 6.2
来源:Manifest Filesysinfoapi.h

【讨论】:

  • 感谢您的回答。我们的原始代码看起来像您的示例,由于 8.1 的问题,我们不得不更改它。该应用程序当前没有清单,因此显示为 6.2。
【解决方案2】:

除了考虑 Windows 10 构建大于 22000 的非常弱的解决方案(至少对我而言),例如 Windows 11,我发现唯一真正有效的解决方案是 WMIs Win32_OperatingSystem class - Caption 属性。

在我的开发 Windows 10 机器上,它提供以下字符串:Microsoft Windows 10 Pro

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

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

C# 和 C++ 运行良好。

【讨论】:

  • 这个答案是duplicate answerthis question。不一定是坏事,但应该注意,尤其是当 Windows 11 开始出现在行业中时。
猜你喜欢
  • 2022-01-02
  • 2021-11-01
  • 2021-10-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 2019-11-20
相关资源
最近更新 更多