MSVC OSVersion returning incorrect version

2019-09-19 15:26发布

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

Running this in Powershell I get the correct result:

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

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

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

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

Why does it think my OS is Windows 8?

Specs:

  • Windows 10
  • Version 10.0.14393 Build 14393
  • Visual Studio 2015 (C++ project)
    • Target Platform 10.0.14393.0
  • Powershell version 5.1

1条回答
萌系小妹纸
2楼-- · 2019-09-19 15:51

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.

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>

See MSDN and Manifest Madness

查看更多
登录 后发表回答