Home  >  Article  >  Backend Development  >  How to Reliably Detect Windows 10 Version Across Platforms and Versions?

How to Reliably Detect Windows 10 Version Across Platforms and Versions?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-05 12:24:02329browse

How to Reliably Detect Windows 10 Version Across Platforms and Versions?

Detecting Windows 10 Version Cross-Platform and Multi-Version

Context

Detecting Windows 10 for cross-platform compatibility can be challenging due to inaccuracies in available functions like IsWindows10OrGreater(). This article explores a cross-platform and multi-version approach to solve this problem.

Solution

The most reliable method for retrieving the true OS version is to call RtlGetVersion. This function avoids the compatibility shims used by GetVersionEx and VerifyVersionInfo. It can be accessed through dynamic linking as shown in the following code snippet:

<code class="c++">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(&amp;rovi) ) {
                return rovi;
            }
        }
    }
    RTL_OSVERSIONINFOW rovi = { 0 };
    return rovi;
}</code>

This method returns the expected results on Windows 10, even in the absence of a manifest.

Alternative Approach

Instead of relying on OS versions, it is recommended to implement feature-based solutions. This provides a more flexible and future-proof approach.

The above is the detailed content of How to Reliably Detect Windows 10 Version Across Platforms and Versions?. 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