Home >Backend Development >C++ >How to Reliably Detect Windows 10 Version Across Platforms?

How to Reliably Detect Windows 10 Version Across Platforms?

DDD
DDDOriginal
2024-11-05 18:32:02214browse

How to Reliably Detect Windows 10 Version Across Platforms?

Cross-Platform Windows 10 Detection

Determining the Windows 10 version is often problematic due to the limitations of built-in functions like IsWindows10OrGreater(). This issue becomes even more pressing when cross-platform compatibility is desired.

A Universal Solution

To overcome these obstacles, it's suggested to employ the RtlGetVersion function. This function bypasses the compatibility shims and grants access to the true OS version. Although it's available in the DDK, it can also be invoked through runtime dynamic linking using the following code 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;
}

This snippet returns the actual OS version on Windows 10, even in the absence of a manifest.

An Alternative Approach

An alternative solution focuses on utilizing features rather than OS versions. This allows for a more flexible approach where different implementations are provided based on available capabilities.

The above is the detailed content of How to Reliably Detect Windows 10 Version Across Platforms?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn