win10 系統下獲取系統版本號爲6.2的問題 win10 系統下獲取系統版本號爲6.2的問題

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以兼容新的系統。

 

 

 

同時可以修正ua獲取問題:

when use UrlMkGetSessionOption to acquire IE default UserAgent, I get the string "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)".But my IE Browsertype is IE11 and not in the compatible mode.

 

You are probably testing on a Webbrowser and your feature control keys are not set so it is setting ie7 as default browser. Check this key and change the value to "11001" for IE11:

-HKEY_LOCAL_MACHINE (or HKEY_CURRENT_USER)
   SOFTWARE
      Microsoft
         Internet Explorer
            Main
               FeatureControl
                  FEATURE_BROWSER_EMULATION
                     yourApplication.exe = (DWORD) 00009000

https://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx#browser_emulation

Note: if your os is 64 bits and your app is 32 bits, you have to take this path instead: SOFTWARE/WOW6432NODE/MICROSOFT...

 

參考IE開發文檔:

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/ms775114(v=vs.85)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章