win10 系统下获取系统版本号为6.2的问题

  近期赶时髦升级了win10,用着挺爽。但是某天在测试一个bug时发现要对win10做特殊处理,于是直接调用了GetVersionEx,并取出版本号进行判断,但是发现得到的版本竟然是6.2。当时就被雷到了,然后看了我们的其它产品中相关功能,皆获取的是6.2。

  在搜索一会儿之后,发现这是微软故意做的设定,GetVersionEx函数可能在Win8.1之后会取消,推荐程序员们使用Version Helper APIs ,所以在8.1之后的系统中此函数的行为改变了,如果程序没有加上正确的manifested以表明此程序兼容新系统,则只能得到6.2这个版本号。

  下面说说,需要如何添加一个正确的manifest,也可以去MSDN上直接看原始文档

   首先,如果原来程序就已经设置了附加一个额外的manifest文件,则直接在原来的manifest里的assembly根节点里加一段兼容指示:

 

 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
        <application> 
            <!-- Windows 10 --> 
            <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- 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}"/>
        </application> 
    </compatibility>

 


  如果原来没有一个manifest文件,则可以在工程的某目录创建一个 xxx.manifest文件,里面写上以下内容,当然内容中的工程名称可以随意,并没有强制,只要确保compatibility节点的值不变就可以了:

<?xml version="1.0" encoding="UTF-8"?> 
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
    <assemblyIdentity version="1.5.0.0" processorArchitecture="X86" name="Microsoft.Windows.工程名称" type="win32"/> 
    <description>工程名称</description>    
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
        <application> 
            <!-- Windows 10 --> 
            <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- 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}"/>
        </application> 
    </compatibility>
</assembly> 

 

  然后在vs工程属性里找到“清单工具->输入和输出->附加清单文件”里填入manifest文件的相对路径,然后重新链接,你的程序就可以正常的使用GetVersionEx了。但出了新系统之后,估计还是要重新改下manifest以兼容新的系统。

 

posted @ 2015-08-13 16:47  k1988  阅读(6161)  评论(1编辑  收藏  举报