Windows 系统版本 总结

0、导引

详细介绍 了 Windows 系统 相关 API 的使用 等内容。

1、操作系统版本

Operating system Version number
Windows 10 10.0*
Windows Server 2019 10.0*
Windows Server 2016 10.0*
Windows 8.1 6.3*
Windows Server 2012 R2 6.3*
Windows 8 6.2
Windows Server 2012 6.2
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64-Bit Edition 5.2
Windows XP 5.1
Windows 2000 5.0

* For applications that have been manifested for Windows 8.1 or Windows 10. Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows.

2、应用层

2.1、通过 GetVersionEx

2.1.1、API 介绍

[GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead, use the Version Helper functions]

With the release of Windows 8.1, the behavior of the GetVersionEx API has changed in the value it will return for the operating system version. The value returned by the GetVersionEx function now depends on how the application is manifested.

Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows.

语法:

NOT_BUILD_WINDOWS_DEPRECATE BOOL GetVersionExA(
LPOSVERSIONINFOA lpVersionInformation
);

参数:

lpVersionInformation

An OSVERSIONINFO or OSVERSIONINFOEX structure that receives the operating system information.

Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of the structure as appropriate to indicate which data structure is being passed to this function.

补充:

To check for specific operating systems or operating system features, use the IsOS function. The GetProductInfo function retrieves the product type.

To retrieve information for the operating system on a remote computer, use the NetWkstaGetInfo function, the Win32_OperatingSystem WMI class, or the OperatingSystem property of the IADsComputer interface.

To compare the current system version to a required version, use the VerifyVersionInfo function instead of using GetVersionEx to perform the comparison yourself.

If compatibility mode is in effect, the GetVersionEx function reports the operating system as it identifies itself, which may not be the operating system that is installed. For example, if compatibility mode is in effect, GetVersionEx reports the operating system that is selected for application compatibility.

2.1.2、相关实验

2.1.2.1、检查当前系统的版本

代码:

#include <windows.h>
#include <stdio.h>
#include <iostream>

#pragma warning(disable: 4996)

using namespace std;

void main()
{
 OSVERSIONINFO osvi;
 BOOL bIsWindowsXPorLater;

 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

 GetVersionEx(&osvi);

 // 检查系统版本
 bIsWindowsXPorLater =
  ((osvi.dwMajorVersion > 5) ||
  ((osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1)));

 if (bIsWindowsXPorLater)
  cout << "The system meets the requirements." << endl;
 else
  cout << "The system does not meet the requirements." << endl;

 // 打印当前系统版本
 cout << osvi.dwMajorVersion << endl;
 cout << osvi.dwMinorVersion << endl;
}

如下情况测试:

  1. 在 Win10 环境下,直接运行,打印当前版本为:

    6.2

  2. 在 Win10 环境下,以兼容模式来运行(XP Sp3),则打印当前的版本为:

    5.1

  3. 在 Win7 x64 环境下,直接运行,则为:

    6.1

  4. Targeting your application for Windows

    待补充

2.2、通过 VerifyVersionInfoW

2.2.1、API 介绍

Compares a set of operating system version requirements to the corresponding values for the currently running version of the system.This function is subject to manifest-based behavior. For more information, see the Remarks section.

语法:

BOOL VerifyVersionInfoA(
  LPOSVERSIONINFOEXA lpVersionInformation,
  DWORD dwTypeMask,
  DWORDLONG dwlConditionMask
);

补充:

It is preferable to use VerifyVersionInfo rather than calling the GetVersionEx function to perform your own comparisons.

To verify whether the current operating system is either the Media Center or Tablet PC version of Windows, call GetSystemMetrics.

Windows 10: VerifyVersionInfo returns false when called by applications that do not have a compatibility manifest for Windows 8.1 or Windows 10 if the lpVersionInfo parameter is set so that it specifies Windows 8.1 or Windows 10, even when the current operating system version is Windows 8.1 or Windows 10. Specifically, VerifyVersionInfo has the following behavior:

  • If the application has no manifest, VerifyVersionInfo behaves as if the operation system version is Windows 8 (6.2).
  • If the application has a manifest that contains the GUID that corresponds to Windows 8.1, VerifyVersionInfo behaves as if the operation system version is Windows 8.1 (6.3).
  • If the application has a manifest that contains the GUID that corresponds to Windows 10, VerifyVersionInfo behaves as if the operation system version is Windows 10 (10.0).

The Version Helper functions use the VerifyVersionInfo function, so the behavior IsWindows8Point1OrGreater and IsWindows10OrGreater are similarly affected by the presence and content of the manifest.

2.2.2、相关实验

[ Use of the VerifyVersionInfo function to verify the currently running operating system is not recommended. Instead, use the Version Helper APIs]

2.2.2.1、例1

The following example determines whether the application is running on Windows XP with Service Pack 2 (SP2) or a later version of Windows, such as Windows Server 2003 or Windows Vista.

代码:

#include <windows.h>
#include <stdio.h>

BOOL Is_WinXP_SP2_or_Later ()
{
   OSVERSIONINFOEX osvi;
   DWORDLONG dwlConditionMask = 0;
   int op=VER_GREATER_EQUAL;

   // Initialize the OSVERSIONINFOEX structure.

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
   osvi.dwMajorVersion = 5;
   osvi.dwMinorVersion = 1;
   osvi.wServicePackMajor = 2;
   osvi.wServicePackMinor = 0;

   // Initialize the condition mask.

   VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
   VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
   VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
   VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );

   // Perform the test.

   return VerifyVersionInfo(
      &osvi,
      VER_MAJORVERSION | VER_MINORVERSION |
      VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
      dwlConditionMask);
}

void main()
{
    if(Is_WinXP_SP2_or_Later())
        printf("The system meets the requirements.\n");
    else printf("The system does not meet the requirements.\n");
}
2.2.2.2、例2

The following code verifies that the application is running on Windows 2000 Server or a later server, such as Windows Server 2003 or Windows Server 2008.

#include <windows.h>
#include <stdio.h>

BOOL Is_Win_Server()
{
   OSVERSIONINFOEX osvi;
   DWORDLONG dwlConditionMask = 0;
   int op=VER_GREATER_EQUAL;

   // Initialize the OSVERSIONINFOEX structure.

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
   osvi.dwMajorVersion = 5;
   osvi.dwMinorVersion = 0;
   osvi.wServicePackMajor = 0;
   osvi.wServicePackMinor = 0;
   osvi.wProductType = VER_NT_SERVER;

   // Initialize the condition mask.

   VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
   VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
   VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
   VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
   VER_SET_CONDITION( dwlConditionMask, VER_PRODUCT_TYPE, VER_EQUAL );

   // Perform the test.

   return VerifyVersionInfo(
      &osvi,
      VER_MAJORVERSION | VER_MINORVERSION |
      VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR |
      VER_PRODUCT_TYPE,
      dwlConditionMask);
}

void main()
{
    if(Is_Win_Server())
        printf("The system meets the requirements.\n");
    else printf("The system does not meet the requirements.\n");
}

2.3、通过 Version Helper functions

2.3.1、API 介绍

The following functions can be used to determine the current operating system version or identify whether it is a Windows or Windows Server release.

IsWinodwsXXXX 系列函数

2.3.2、相关实验

2.3.2.1、例1

The following example uses the Version API Helper functions to determine the version of the current operating system, if it is a Server or Client release, and then displays this information to the console. If compatibility mode is in effect, the example displays the operating system selected for application compatibility.

代码:

#include <windows.h>
#include <stdio.h>
#include <VersionHelpers.h>

int
__cdecl
wmain(
 __in int argc,
 __in_ecount(argc) PCWSTR argv[]
)
{
 UNREFERENCED_PARAMETER(argc);
 UNREFERENCED_PARAMETER(argv);

 if (IsWindowsXPOrGreater())
 {
  printf("XPOrGreater\n");
 }

 if (IsWindowsXPSP1OrGreater())
 {
  printf("XPSP1OrGreater\n");
 }

 if (IsWindowsXPSP2OrGreater())
 {
  printf("XPSP2OrGreater\n");
 }

 if (IsWindowsXPSP3OrGreater())
 {
  printf("XPSP3OrGreater\n");
 }

 if (IsWindowsVistaOrGreater())
 {
  printf("VistaOrGreater\n");
 }

 if (IsWindowsVistaSP1OrGreater())
 {
  printf("VistaSP1OrGreater\n");
 }

 if (IsWindowsVistaSP2OrGreater())
 {
  printf("VistaSP2OrGreater\n");
 }

 if (IsWindows7OrGreater())
 {
  printf("Windows7OrGreater\n");
 }

 if (IsWindows7SP1OrGreater())
 {
  printf("Windows7SP1OrGreater\n");
 }

 if (IsWindows8OrGreater())
 {
  printf("Windows8OrGreater\n");
 }

 if (IsWindows8Point1OrGreater())
 {
  printf("Windows8Point1OrGreater\n");
 }

 if (IsWindows10OrGreater())
 {
  printf("Windows10OrGreater\n");
 }

 if (IsWindowsServer())
 {
  printf("Server\n");
 }
 else
 {
  printf("Client\n");
 }

 getchar();
}

2.4、嵌入 Mainifeast 文件

方法如下:

a)、在工程目录下创建以清单文件,且输入如下文件内容。

<?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>

如文件名为,ConsoleTest.manifest。

b)、然后在vs工程属性里找到 “清单工具->输入和输出->附加清单文件” 里填入manifest文件的相对路径,然后重新链接,即可。

代码如 3.2,还是调用 GetVersionEx。

实验结果如下:

  1. 在 Win10 环境下,直接运行,打印当前版本为:

    10.0

  2. 在 Win10 环境下,以兼容模式来运行(XP Sp3),则打印当前的版本为:

    5.1

2.5、通过 RtlGetVersion

2.5.1、API 介绍

GetVersionEx 调用的 RtlGetVersion,但是 该函数 能获得正确的 版本信息。

2.5.2、代码

#include "pch.h"
#include <windows.h>
#include <iostream>

#define DECLARE_DLL_FUNCTION(fn, type, dll) \
    auto fn = reinterpret_cast<type>(GetProcAddress(GetModuleHandleW(L##dll), #fn))

struct VersionNumber {
    unsigned long major_version;
    unsigned long minor_version;
};

void GetSystemVersion(VersionNumber& version_number) noexcept
{
    constexpr NTSTATUS kStatusSuccess = 0L;
    DECLARE_DLL_FUNCTION(RtlGetVersion, NTSTATUS(WINAPI*)(PRTL_OSVERSIONINFOW), "ntdll.dll");
    if (!RtlGetVersion) {
        return;
    }

    RTL_OSVERSIONINFOW ovi{ sizeof(ovi) };
    if (RtlGetVersion(&ovi) != kStatusSuccess) {
        return;
    }

    version_number.major_version = ovi.dwMajorVersion;
    version_number.minor_version = ovi.dwMinorVersion;
}

int main()
{
    VersionNumber stNum{};
    GetSystemVersion(stNum);
    std::cout << stNum.major_version << std::endl;
    std::cout << stNum.minor_version << std::endl;

    getchar();

    return 0;
}

在 Win10 x64, 1903 下获得的版本号是 10.0。

即使在兼容模式下也正常。

2.6、通过 RtlGetNtVersionNumbers

2.6.1、代码

using RtlGetNtVersionNumbersProc = void(__stdcall*)(DWORD *, DWORD *, DWORD *);
HINSTANCE hinst = LoadLibraryW(L"ntdll.dll");   
RtlGetNtVersionNumbersProc pFunc = (RtlGetNtVersionNumbersProc)GetProcAddress(hinst, "RtlGetNtVersionNumbers");

DWORD dwBuildNumber = 0;
DWORD dwMajor = 0;
DWORD dwMinor = 0;
pFunc(&dwMajor, &dwMinor, &dwBuildNumber);
std::cout << dwMajor << " " << dwMinor << " " << dwBuildNumber << std::endl;

在 Win10 x64, 1903 下获得的版本号是 10.0。

同时在兼容模式下,获得也正常。

2.7、通过读取 系统文件信息 来获取

To obtain the full version number for the operating system, call the GetFileVersionInfo function on one of the system DLLs, such as Kernel32.dll, then call VerQueryValue to obtain the \\StringFileInfo\\\\ProductVersion subblock of the file version information.

https://docs.microsoft.com/zh-cn/windows/win32/sysinfo/getting-the-system-version?redirectedfrom=MSDN

2.8、通过 PEB 来获取

#include<Windows.h>
#include<stdio.h>
 
int main()
{
    uintptr_t PEB;
    int OsBuildNumber;
#ifdef _WIN64
    PEB = __readgsqword(0x60);
    OsBuildNumber = *((INT *)(PEB + 0x120));
#else
    PEB = __readfsdword(0x30);
    OsBuildNumber = *((INT *)(PEB + 0xAC));
#endif // _WIN64
    switch (OsBuildNumber)
    {
    case 17134:
        printf("Win10 1803\n");
        break;
    case 17763:
        printf("Win10 1809\n");
        break;
    case 18362:
        printf("Win10 1903\n");
        break;
    case 15063:
        printf("Win10 1703\n");
        break;
    case 16299:
        printf("Win10 1709\n");
        break;
    // ........Other Windows OS Version
    default:
        break;
    }
    printf("OsBuildNumber: %d", OsBuildNumber);
    getchar();
}

说明:

Wow64 PEB

+0x118 OSMajorVersion : 0xa
+0x11c OSMinorVersion : 0
+0x120 OSBuildNumber : 0x47ba
+0x122 OSCSDVersion : 0
+0x124 OSPlatformId : 2

2.8、其他 相关 API 介绍

2.8.1、GetProductInfo

2.8.1.1、API 介绍

Retrieves the product type for the operating system on the local computer, and maps the type to the product types supported by the specified operating system.

To retrieve product type information on versions of Windows prior to the minimum supported operating systems specified in the Requirements section, use the GetVersionEx function.

语法:

BOOL GetProductInfo(
  DWORD  dwOSMajorVersion,
  DWORD  dwOSMinorVersion,
  DWORD  dwSpMajorVersion,
  DWORD  dwSpMinorVersion,
  PDWORD pdwReturnedProductType
);

要求:

Minimum supported client Windows Vista [desktop apps only]
Minimum supported server Windows Server 2008 [desktop apps only]

具体使用略。

3、内核层

3.1、通过 RtlGetVersion

3.1.1、API 介绍

The RtlGetVersion routine returns version information about the currently running operating system.

语法:

NTSYSAPI NTSTATUS RtlGetVersion(
  PRTL_OSVERSIONINFOW lpVersionInformation
);

补充:

RtlGetVersion is the kernel-mode equivalent of the user-mode GetVersionEx function in the Windows SDK. See the example in the Windows SDK that shows how to get the system version.

3.1.2、代码

RTL_OSVERSIONINFOEXW stVerInfo = { 0 };
::RtlZeroMemory(&stVerInfo, sizeof(RTL_OSVERSIONINFOEXW));
stVerInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);

if (STATUS_SUCCESS == ::RtlGetVersion((PRTL_OSVERSIONINFOW)&stVerInfo))
{
    KdPrint(("dwMajorVersion: 0x%08x\n", stVerInfo.dwMajorVersion));
    KdPrint(("dwMinorVersion: 0x%08x\n", stVerInfo.dwMinorVersion));
    KdPrint(("dwBuildNumber: 0x%08x\n", stVerInfo.dwBuildNumber));
}

Win10 环境(1803)下 打印结果:

dwMajorVersion: 0x0000000a
dwMinorVersion: 0x00000000
dwBuildNumber: 0x000042ee

3.2、通过 PsGetVersion

3.2.1、API 介绍

This function is obsolete in Windows XP and later versions of the Windows operating system. Use RtlGetVersion instead.
PsGetVersion returns caller-selected information about the current version of the NT-based operating system.

其他略:

3.2.2、代码

ULONG dwMajorVersion = 0;
ULONG dwMinorVersion = 0;
ULONG dwBuildNumber = 0;
BOOLEAN bChecked = PsGetVersion(&dwMajorVersion, &dwMinorVersion, &dwBuildNumber, nullptr);
KdPrint(("Check Build: 0x%08x\n", bChecked)); // returns whether the system is a checked or free build.
KdPrint(("dwMajorVersion: 0x%08x\n", dwMajorVersion));
KdPrint(("dwMinorVersion: 0x%08x\n", dwMinorVersion));
KdPrint(("dwBuildNumber: 0x%08x\n", dwBuildNumber));

Win10 环境(1803)下 打印结果:

Check Build: 0x00000000
dwMajorVersion: 0x0000000a
dwMinorVersion: 0x00000000
dwBuildNumber: 0x000042ee

3.3、通过 RtlVerifyVersionInfo

3.3.1、API 介绍

The RtlVerifyVersionInfo routine compares a specified set of operating system version requirements to the corresponding attributes of the currently running version of the operating system.

其他参考 VerifyVersionInfo。

发布了7 篇原创文章 · 获赞 0 · 访问量 33

猜你喜欢

转载自blog.csdn.net/songbei6/article/details/105233898