检测Windows 10版本 [英] Detecting Windows 10 version

查看:331
本文介绍了检测Windows 10版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是检测Windows 10在我的代码,它必须工作跨平台以及跨不同版本的Windows(至少7& up)。 Windows提供 IsWindows10OrGreater()来解决这个问题,但还有另一个问题,此功能不存在于以前的Windows版本。

My objective is to detect Windows 10 in my code which has to work cross-platform as well as across different versions of Windows (atleast 7 & up). Windows provides IsWindows10OrGreater() to tackle this problem, but there's another issue with it, this function isn't present in previous windows versions.

你会发现无数的博客和SO的问题,以及清单疯狂的功能像这样getversion和其他人返回一些不同的版本,而不是正确的。

You'll find countless blogs and SO questions regarding this as well as the manifest madness where functions like this and getversion and others return some different version rather than the correct one.

例如在我的机器上 - 方法 IsWindows10OrGreater()不编译Win10 SDK)和 IsWindowsVersionOrGreater()报告 6 为主要版本。

For example on my machine - the method IsWindows10OrGreater() doesn't compile(I would've to install Win10 SDK), and IsWindowsVersionOrGreater() reports 6 as major version.

那么是否有一个合理的多版本方式可以解决这个问题?

So is there a sane multi-version way I could solve this problem?

推荐答案

检索真实操作系统版本的最直接方法是调用 RtlGetVersion 。它是 GetVersionEx VerifyVersionInfo 调用,但不使用兼容性垫片。

The most straight-forward way to retrieve the true OS version is to call RtlGetVersion. It is what GetVersionEx and VerifyVersionInfo call, but doesn't employ the compatibility shims.

您可以使用DDK(通过#including< ntddk.h>并链接到NtosKrnl.lib),或者使用运行时动态链接,如下面的代码片段:

You can either use the DDK (by #including <ntddk.h> and linking against NtosKrnl.lib), or use runtime dynamic linking as in the following snippet:

typedef LONG NTSTATUS, *PNTSTATUS;
#define STATUS_SUCCESS (0x00000000)

typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);

RTL_OSVERSIONINFOW GetRealOSVersion() {
    HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
    if (hMod) {
        RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");
        if (fxPtr != nullptr) {
            RTL_OSVERSIONINFOW rovi = { 0 };
            rovi.dwOSVersionInfoSize = sizeof(rovi);
            if ( STATUS_SUCCESS == fxPtr(&rovi) ) {
                return rovi;
            }
        }
    }
    RTL_OSVERSIONINFOW rovi = { 0 };
    return rovi;
}

如果您需要其他信息,可以传递 RTL_OSVERSIONINFOEXW 结构来代替 RTL_OSVERSIONINFOW 结构(正确分配 dwOSVersionInfoSize 成员)。

In case you need additional information you can pass an RTL_OSVERSIONINFOEXW structure in place of the RTL_OSVERSIONINFOW structure (properly assigning the dwOSVersionInfoSize member).

这将返回在Windows 10上的预期结果,即使没有附加清单。

This returns the expected result on Windows 10, even when there is no manifest attached.


另外,它被普遍接受为更好的解决方案提供基于可用功能的不同实现,而不是操作系统版本。


As an aside, it is commonly accepted as a better solution to provide different implementations based on available features rather than OS versions.

这篇关于检测Windows 10版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆