1. 程式人生 > >獲取作業系統的詳細資訊---基礎

獲取作業系統的詳細資訊---基礎

自己開始在CSDN上寫,就是因為總結這個的時候,由於意外新浪退出,所以gg了。

不知道記得多少了。

整理這個是因為有個問題是,獲取win10版本資訊的時獲取的結果是win8。

微軟給出兩個解決方案:

1、修改manifes針對windows的應用程式(我根據網友的提示自己試了,沒有什麼鳥用,大家可以試下微軟的)

2、win8.1以後使用GetVersionExA函式(參考下,Version Helper functions,可以嘗試使用 IsWindows10OrGreater

一、微軟的manifes

<exe>.manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <assemblyIdentity 
        type="win32" 
        name=SXS_ASSEMBLY_NAME
        version=SXS_ASSEMBLY_VERSION
        processorArchitecture=SXS_PROCESSOR_ARCHITECTURE
    />
    <description> my exe </description>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel
                    level="asInvoker"
                    uiAccess="false"
                />   
            </requestedPrivileges>
        </security>
    </trustInfo>
    <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>

接下來,將以下內容新增到您的來源:

SXS_MANIFEST_RESOURCE_ID=1
SXS_MANIFEST=foo.manifest
SXS_ASSEMBLY_NAME=Microsoft.Windows.Foo
SXS_ASSEMBLY_VERSION=1.0    
SXS_ASSEMBLY_LANGUAGE_INDEPENDENT=1
SXS_MANIFEST_IN_RESOURCES=1

在以前的作業系統上執行時,顯示Windows 8.1或Windows 10 的.exe不會產生任何影響。如果已經定義了.rc檔案,也可以將其新增到.rc檔案中。

(可能是我不知道怎麼用)

二、_GetVersionEx (還有一個函式GetVersion,有興趣可以看看

1、GetVersionExA(GetVersionExW,其實一個是寬字元,一個就是一般的,原來自己傻呵呵的,都不知道)

NOT_BUILD_WINDOWS_DEPRECATE BOOL GetVersionExA( LPOSVERSIONINFOA lpVersionInformation );

其實接下來主要理解就是:

LPOSVERSIONINFOA (看下面)

是什麼,怎麼用?

LPOSVERSIONINFOA 就需要看 _OSVERSIONINFOA

這個結構體

typedef struct _OSVERSIONINFOA {
  DWORD dwOSVersionInfoSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  DWORD dwBuildNumber;
  DWORD dwPlatformId;
  CHAR  szCSDVersion[128];
} OSVERSIONINFOA, *POSVERSIONINFOA, *LPOSVERSIONINFOA;

要使用函式,感覺這個是重點,要明白微軟定義的6.1中的6和1都是啥。

2、_OSVERSIONINFOA 引數詳解

引數詳解:

dwOSVersionInfoSize

The size of this data structure, in bytes. Set this member to sizeof(OSVERSIONINFO).(資料結構的大小)

dwMajorVersion

The major version number of the operating system. For more information, see Remarks.(主版本號,比如6.1中的6)

dwMinorVersion

The minor version number of the operating system. For more information, see Remarks.(次版本號,比如6.1中的1)

dwBuildNumber

The build number of the operating system.(內部版本號)

dwPlatformId

The operating system platform. This member can be the following value.作業系統平臺

szCSDVersion

A null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty.(以null結尾的字串,例如“Service Pack 3”,表示系統上安裝的最新Service Pack。如果尚未安裝Service Pack,則字串為空)

3、微軟作業系統版本的資訊

系統 版本號 dwMajorVersion dwMinorVersion 其他
Windows 10 10.0 * 10 0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2016 10.0 * 10 0 OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows 8.1 6.3 * 6 3 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2012 R2 6.3 * 6 3 OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows 8 6.2 6 2 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2012 6.2 6 2 OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows 7的 6.1 6 1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2008 R2 6.1 6 1 OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows Server 2008 6 6 0 OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows Vista 6 6 0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2003 R2 5.2 2 GetSystemMetrics(SM_SERVERR2)!= 0
Windows Server 2003 5.2 2 GetSystemMetrics(SM_SERVERR2)== 0
Windows XP 5.1 1 不適用
Windows 2000 5 0 不適用

可以看到,每個或者連個作業系統使用相同的主版本可次版本號,但最後又使用另一引數(wProductType )進行區分。

來一波參考資料:

WindowsAPI獲取主機作業系統資訊

如何判斷作業系統是64位還是32位

Windows系統版本判定那些事兒

銀河軍團(其實只看這個就可以了,還是牛皮)

VCmainfies作用