【问题标题】:MSVC OSVersion returning incorrect versionMSVC OSVersion 返回不正确的版本
【发布时间】:2017-11-10 15:32:03
【问题描述】:

在我的开发机器上运行 Windows 10。试图让操作系统版本返回 10 的主要版本。Tried this 并通过System::Environment::OSVersion->VersionString 尝试了环境变量。两者都返回Microsoft Windows NT 6.2.9200。在 Windows 7 机器上测试可执行文件,我得到 6.1.*(正确)。

在 Powershell 中运行,我得到了正确的结果:

PS Z:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      14393  0

我在 Visual Studio 中使用相同的环境变量逻辑编写了一个测试 C++ 程序...

#include <Windows.h>
#include <iostream>

using namespace std; 

int main(int argc, char **argv, char** envp) {
    printf("Current compiler version is ... %d\n", _MSC_VER);
    printf("Current compiler full version is ... %d\n", _MSC_FULL_VER);
    printf("Current OSVersion is ... %s\n\n", System::Environment::OSVersion->VersionString);

    char** env;
    for (env = envp; *env != 0; env++)
    {
        char* thisEnv = *env;
        printf("%s\n", thisEnv);
    }

    cin.ignore();
    cin.ignore();

    return 0;
}

结果(我不会打印除一个以外的所有环境变量)...

Current compiler version is ... 1900
Current compiler full version is ... 190024215
Current OSVersion is ... Microsoft Windows NT 6.2.9200.0

...
OS=Windows_NT

以上测试程序配置目标平台为10.0.14393.0

为什么它认为我的操作系统是 Windows 8?

规格:

  • Windows 10
  • 版本 10.0.14393 内部版本 14393
  • Visual Studio 2015(C++ 项目)
    • 目标平台 10.0.14393.0
  • Powershell 5.1 版

【问题讨论】:

标签: c++ visual-studio powershell visual-c++ windows-10


【解决方案1】:

这是设计使然,因为 GetVersionExVerifyVersionInfo 函数在 Windows 8.1 或更高版本上为所有 Win32 桌面应用程序默认启用了“版本谎言”。

您可以通过向您的 EXE 添加包含适当兼容性 GUID 的嵌入式清单来控制“版本谎言”的应用:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
   <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
       <application> 
           <!-- Windows Vista -->
           <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
           <!-- Windows 7 -->
           <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
           <!-- Windows 8 -->
           <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
           <!-- Windows 8.1 -->
           <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
           <!-- Windows 10 -->
           <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
       </application> 
   </compatibility>
</assembly>

MSDNManifest Madness

【讨论】:

  • 这样做了,它有效,但我收到了一个警告(这似乎也是一个谎言) -- manifest authoring warning 81010002: Unrecognized Element "compatibility" in namespace "urn:schemas-microsoft-com:compatibility.v1" -- 我声称这是一个谎言,因为兼容性标签确实有效并且是能够返回正确的 Windows 版本。知道这是关于什么的,或者我该如何摆脱它?
  • 这取决于您使用的清单工具 (MT.EXE) 的版本。如果您使用的是较新的警告,则不会发生此警告。检查项目中的工具路径以确定您使用的是哪一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 2013-04-29
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多