获取操作系统的详细信息---基础

自己开始在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、微软操作系统版本的信息

系统版本号dwMajorVersiondwMinorVersion其他
Windows 1010.0 *100OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 201610.0 *100OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows 8.16.3 *63OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2012 R26.3 *63OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows 86.262OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 20126.262OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows 7的6.161OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2008 R26.161OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows Server 2008660OSVERSIONINFOEX.wProductType!= VER_NT_WORKSTATION
Windows Vista660OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
Windows Server 2003 R25.22GetSystemMetrics(SM_SERVERR2)!= 0
Windows Server 20035.22GetSystemMetrics(SM_SERVERR2)== 0
Windows XP5.11不适用
Windows 200050不适用

可以看到,每个或者连个操作系统使用相同的主版本可次版本号,但最后又使用另一参数(wProductType )进行区分。

来一波参考资料:

WindowsAPI获取主机操作系统信息

如何判断操作系统是64位还是32位

Windows系统版本判定那些事儿

银河军团(其实只看这个就可以了,还是牛皮)

VCmainfies作用


版权声明:本文为Ma_Hong_Kai原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。