MSVC OSVersion 返回不正确的版本 [英] MSVC OSVersion returning incorrect version

查看:33
本文介绍了MSVC OSVersion 返回不正确的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Running Windows 10 on my development machine. Trying to get the OS version to return a major version of 10. Tried this and also tried environment variables via System::Environment::OSVersion->VersionString. Both are returning Microsoft Windows NT 6.2.9200. Tested executable on a Windows 7 machine and I get 6.1.* (correct).

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

Running this in Powershell I get the correct result:

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

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

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

I wrote a test C++ program in Visual Studio using the same Environment variables logic ...

#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;
}

结果(除了一个,我不会打印所有环境变量)...

Results it (I won't print all environment variables except one)...

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.

The above test program is configured with the Target Platform as 10.0.14393.0.

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

Why does it think my OS is Windows 8?

规格:

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

    推荐答案

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

    This is by design as the GetVersionEx and VerifyVersionInfo functions have a 'version lie' enabled by default on Windows 8.1 or later for all Win32 desktop applications.

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

    You control the application of the 'version lie' by adding an embedded manifest to your EXE that includes the proper compatibility GUIDs:

    <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明显的疯狂

    这篇关于MSVC OSVersion 返回不正确的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆