MSVC OSVersion 返回不正确的版本

MSVC OSVersion returning incorrect version

本文关键字:版本 不正确 返回 OSVersion MSVC      更新时间:2023-10-16

在我的开发机器上运行Windows 10。尝试获取操作系统版本以返回主要版本 10。尝试了这个,也通过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 ... %dn", _MSC_VER);
    printf("Current compiler full version is ... %dn", _MSC_FULL_VER);
    printf("Current OSVersion is ... %snn", System::Environment::OSVersion->VersionString);
    char** env;
    for (env = envp; *env != 0; env++)
    {
        char* thisEnv = *env;
        printf("%sn", 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?

规格:

  • 视窗 10
  • 版本
  • 10.0.14393 内部版本 14393
  • Visual Studio 2015 (C++ 项目(
    • 目标平台 10.0.14393.0
  • Powershell 版本 5.1
这是

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

您可以通过向包含

适当兼容性 GUID 的 EXE 添加嵌入式清单来控制"版本谎言"的应用:

<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>

请参阅 MSDN 和 Manifest Madness