5

I use TOSVersion.ToString function (uses SysUtils) to detect Windows version. However this is what I get in Windows11:

Windows 10 (Version 10.0, Build 21996, 64-bit Edition)

Is there any reliable way to detect Windows 11? I'm using Delphi 10.3.3.

UPDATE: Windows 11 is officially released and I tried again. Here is what I get:

Windows 10 (Version 10.0, Build 22000, 64-bit Edition)

Xel Naga
  • 826
  • 11
  • 28
  • 2
    I suppose if the build number is greater than 20000 – misha130 Jul 24 '21 at 14:01
  • 2
    "I use TOSVersion.ToString function (uses SysUtils) to detect Windows version." That sounds like a bad approach to me. – Andreas Rejbrand Jul 24 '21 at 15:01
  • Is there a particular reason why you need to detect the Windows version? It is generally better to detect the presence of features you need at runtime, not rely on specific version numbers. Especially since in recent years, Microsoft is really going out of its way to obscure the version numbers, and deprecate APIs that are able to retrieve them. – Remy Lebeau Jul 24 '21 at 15:49
  • @RemyLebeau My code has some if statements for Windows XP and 7. I don't have any Windows 11 related issue for now. However it would be good to be able to detect Windows 11. – Xel Naga Jul 24 '21 at 21:29
  • 2
    @Andrzej but WHY? Aside from displaying it, what kind of logic do you control with this information? In any case, one of the best ways to get the OS version number, that is not (yet?) affected by manifest virtualization, is to use `RtlGetVersion()` in `Ntdll.dl`. Windows 11 hasn't been publicly released yet, only in previews. It doesn't even have a proper `supportedOS` guid defined yet for app manifests. – Remy Lebeau Jul 24 '21 at 23:23
  • Solve the problem when you actually have a problem – David Heffernan Jul 25 '21 at 05:01
  • *"Is there any reliable way to detect Windows 11?"* You can't reliably detect something that still doesn't exist. – Olivier Jul 25 '21 at 07:19
  • 1
    The build number cutoff isn't 20000 but instead 22000. Or if you want to catch the leaked beta 21996, though doing so might cause compatibility troubles due to it being incomplete. – William Oct 02 '21 at 23:01
  • 1
    https://www.techthoughts.info/windows-version-numbers/ – Gabriel Feb 02 '22 at 20:22
  • 1
    @Remy: My particular reason why to check for Windows 11 is to be able to circumvent following bug: https://techcommunity.microsoft.com/t5/report-an-issue/combobox-ghost-after-windows-11-upgrade/m-p/3120717. The workaround I found has some side effects, therefore I want to use it only if my software will be running on Windows 11. – truthseeker Mar 17 '22 at 11:35

4 Answers4

4

As Remy pointed out: using the WinAPI you risk of being in some compatibility mode, resulting in getting a version reported that is lower than the actual.

  1. One alternative is to check the file version of expected files, i.e.

    • %windir%\system32\ntoskrnl.exe or
    • %windir%\explorer.exe

    using GetFileVersionInfo() and VerQueryValue() - the HiWord(dwFileVersionLS) should be 22000 or higher (according to Windows NT build/release number).

  2. Another is to look in the Registry under HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ at the text values CurrentBuild and CurrentBuildNumber, checking if the highest of both is 22000 or higher.

  3. David already wrote a detailled answer in Checking Windows version on W10 with even more alternatives, although concentrating on the high/low version numbers, not the build. But WMI might help.

  4. (This only works in retrospective with confirmed knowledge.) Check which API exports are available: the idea is that specific functions were introduced with specific Windows releases/versions, so if importing fails for one you know you're on a version below. An outdated example and an outdated list of minimum versions per function will give you an idea. Now you "only" have to find out which new functions are introduced with Windows 11.

Those are all not bulletproof, but you could combine them and then draw conclusions. And after all that you can still try your approach to parse texts instead of relying on numbers only. It also shows how easily you can manipulate your system into reporting different versions as per which method is used.

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
4

Official major version number for Windows 11 is 10.

The official build number for the public preview of Windows 11 is 10.0.22000.168

Earlier builds:

  • 10.0.22000.71
  • 10.0.22000.65
  • 10.0.22000.51

Microsoft Windows 11 FAQ

If you want to detect Preview versions, earliest build number was 10.0.22000.51 Windows 11 version history

TOSVersion relies on some hard coded names and logic to return OS name. You will have to implement your own detection, copy and modify TOSVersion record or make wrapper around it, where you can use existing logic for older versions and implement check based on Windows 11 build number to detect Windows 11.

For other general issues and approaches in detecting OS version you can refer to AmigoJack's answer

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
1

Except the very weak, atleast for me, solution of considering Windows 10 builds greater than 22000, such as Windows 11, the only solution I found which is actually working is the WMIs Win32_OperatingSystem class - Caption property.

On my dev Win10 machine, it gives the following string: Microsoft Windows 10 Pro.

On my another dev machine, with Win11 installed, the same function gives: Microsoft Windows 11 Pro.

The difference is in string values - "10" vs "11"- but this is at least something far better than the "build greater than" solution.

C# and C++ work well.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Denis B
  • 56
  • 3
  • This answer is a [duplicate answer](https://stackoverflow.com/questions/69836878/detecting-windows-11-properly/70077011#70077011) to [this question](https://stackoverflow.com/questions/69836878/detecting-windows-11-properly/70077011#70077011). Not necessarily a bad thing, but should be noted, especially as Windows 11 is beginning to appear in the industry. – ouflak Nov 23 '21 at 10:17
0

The simplest way is to get the version of Kernel32.dll and if Major Version is 10 and Build Version is >= 22000 then you have Windows 11.

See my code here: How can I find the Windows product name in Windows 11?

Elmue
  • 7,602
  • 3
  • 47
  • 57
  • 2
    Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](//meta.stackexchange.com/q/104227) - although this isn't _quite_ a duplicate answer, it's basically link-only. –  Dec 31 '21 at 19:37