yaakov
yaakov

Reputation: 5850

How can I find the Windows product name in Windows 11?

Windows 11, released yesterday, reports itself as Windows 10.0 just about everywhere - RtlGetVersion says 10.0, and if you ask VerifyVersionInfo if you are 11.0 or greater, it says no.

There seems to be no new GUID to shove into app.manifest to say "hey I support Windows 11" like there was for Windows 7, 8, 8.1, and 10.

Currently I rely on HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName to tell me what the current version of Windows is, but on my machine that I've upgraded, it still says "Windows 10 Enterprise", not "Windows 11 Enterprise".

There appears to only be a single place in the registry that contains the text "Windows 11" and that is the BCD section (boot configuration), which can also be renamed so I don't want to touch that.

So far I have only identified a handful of methods to tell if I am running on Windows 11:

  1. Invoke WMI to query Win32_OperatingSystem and check the Name property, which simply says "Windows 11". This is incomplete (it does not include the SKU such as "Enterprise"), and WMI is relatively slow and slightly brittle, so it's not an acceptable solution for my use-case.

  2. Check the build number to see if it is above 21996 (beta builds) or 22000 (first public release). As above, this won't include the SKU, and would require some manual jiggery-pokery in order to build the full string.

  3. Run sysinfo and parse the output. This is quite slow, and possibly brittle (I haven't checked but the output might be localised into different languages).

  4. winver knows, but it's a GUI application so I can't exactly query it programmatically.

Does anyone have any other ideas on how to get the string "Windows 11 Enterprise" (or "Windows 11 Pro", etc. as the case may be) out of my operating system in a performant and complete manner? Where do WMI, sysinfo, and winver get it from?

I need to do this from a .NET library, but P/Invokes / native function calls are acceptable solutions.

Upvotes: 21

Views: 12549

Answers (6)

yaakov
yaakov

Reputation: 5850

It seems that Windows itself (e.g. winver) gets this information from Windows branding - specifically from the resource table in %WinDir%\Branding\Basebrd\en-US\basebrd.dll.mui.

To access this one could use the private APIs in %WinDir%\System32\winbrand.dll. Specifically, the function BrandingFormatString which accepts a wide string (LPW[C]STR) and returns a wide string.

e.g. BrandingFormatString("%WINDOWS_LONG%") returns "Windows 11 Pro" on my home PC.

I do not know the lifecycle semantics of the resulting string, i.e. if/when/how it should be freed.

The following code serves as a functional proof-of-concept (C# 9.0):

using System;
using System.Runtime.InteropServices;

[DllImport("winbrand.dll", CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
static extern string BrandingFormatString(string format);

Console.WriteLine(BrandingFormatString("Hello World from %WINDOWS_LONG%!"));

Upvotes: 15

Elmue
Elmue

Reputation: 8178

The following code has been tested on Windows XP, 7, 10, 11. It works on 32 bit and 64 bit operating systems. It works inside 32 bit and 64 bit applications.

The following strings will be generated:

  • "Microsoft Windows XP, Build 2600, 32 bit"
  • "Windows 7 Ultimate, Build 7601, 64 bit"
  • "Windows 10 Enterprise LTSC 2019, Version 1809, Build 17763, 64 bit"
  • "Windows 10 Pro, Version 1909, Build 18362, 64 bit"
  • "Windows 11 Professional, Version 21H2, Build 22000, 64 bit"

Put the code into a static constructor so it executes only once and writes the version into a static variable.

static String ms_OperatingSystem;

static Constructor()
{
    try
    {
        String s_KernelPath = Path.Combine(Environment.SystemDirectory,"Kernel32.dll");
        FileVersionInfo k_Kernel = FileVersionInfo.GetVersionInfo(s_KernelPath);

        // on 32 bit Windows this will read the 32 bit hive instead
        using (RegistryKey i_HKLM = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
        {
            using (RegistryKey i_RegVer = i_HKLM.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion", false))
            {
                // Kernel32.dll on Windows 11 has Product Version 10.0.22000.120
                // ATTENTION: Windows 10 has the following Kernel32 versions: (Microsoft is sooooo stupid!)
                // FileVersion    =  6.2.18362
                // ProductVersion = 10.0.18362
                if (k_Kernel.ProductMajorPart == 10 && k_Kernel.ProductBuildPart >= 22000)
                {
                    ms_OperatingSystem = "Windows 11";

                    Object o_Edition = i_RegVer.GetValue("EditionID"); // "Professional"
                    if (o_Edition is String)
                        ms_OperatingSystem += " " + o_Edition;
                }
                else
                {
                    // "Microsoft Windows XP"
                    // "Windows 7 Ultimate"
                    // "Windows 10 Pro"  (same string on Windows 11. Microsoft SUCKS!)
                    ms_OperatingSystem = (String)i_RegVer.GetValue("ProductName");
                }

                // See: https://en.wikipedia.org/wiki/Windows_10_version_history
                // Windows 10 older releases --> "2009" (invalid if DisplayVersion exists)
                Object o_ReleaseID = i_RegVer.GetValue("ReleaseId");

                // Windows 10 latest release --> "21H1"
                // Windows 11 first  release --> "21H2"
                Object o_DispVer = i_RegVer.GetValue("DisplayVersion");

                // Use ReleaseID ONLY if DisplayVersion does not exist in registry!
                     if (o_DispVer   is String) ms_OperatingSystem += ", Version " + o_DispVer;
                else if (o_ReleaseID is String) ms_OperatingSystem += ", Version " + o_ReleaseID;

                ms_OperatingSystem += ", Build " + k_Kernel.ProductBuildPart;

                if (Environment.Is64BitOperatingSystem) ms_OperatingSystem += ", 64 bit";
                else                                    ms_OperatingSystem += ", 32 bit";
            }
        }
     }
     catch (Exception Ex)
     {
        ms_OperatingSystem = Ex.Message;
     }
}

Upvotes: 5

NickH
NickH

Reputation: 11

The powershell command Get-ComputerInfo reports "OsName" as e.g. "Microsoft Windows 11 Pro"

Upvotes: 0

Wesam Nafaa
Wesam Nafaa

Reputation: 39

    Public Structure OsVersions
        Public Shared WindowsXPBuild As Integer = 2600
        Public Shared WindowsVistaBuild As Integer = 6000 
        Public Shared Windows7Build As Integer = 7601 
        Public Shared Windows8Build As Integer = 9200 
        Public Shared Windows8Point1Build As Integer = 9600 
        Public Shared Windows10Build As Integer = 14393 
        Public Shared Windows11Build As Integer = 22000 
    End Structure

    Public Shared Function GetWindwosInfo() As (RealWindowsName As String, RealWindowsVersion As Integer)
        Dim RealWindowsNameResult As String = Nothing
        Dim RealWindowsVersionResult As Integer = Nothing
        With Environment.OSVersion.Version
            RealWindowsVersionResult = .Build
            If .Build >= MyIO.OsVersions.Windows11Build Then RealWindowsNameResult = "Windows 11" : GoTo Finish
            If .Build >= MyIO.OsVersions.Windows10Build Then RealWindowsNameResult = "Windows 10" : GoTo Finish
            If .Build >= MyIO.OsVersions.Windows8Build Then RealWindowsNameResult = "Windows 8" : GoTo Finish
            If .Build >= MyIO.OsVersions.Windows7Build Then RealWindowsNameResult = "Windows 7" : GoTo Finish
            If .Build >= MyIO.OsVersions.WindowsVistaBuild Then RealWindowsNameResult = "Windows Vista" : GoTo Finish
            If .Build >= MyIO.OsVersions.WindowsXPBuild Then RealWindowsNameResult = "Windows XP" : GoTo Finish
        End With
Finish:
        Return (
            RealWindowsName:=RealWindowsNameResult,
            RealWindowsVersion:=RealWindowsVersionResult
            )
    End Function

Upvotes: 0

Tim
Tim

Reputation: 11

I created a remote support tool for desktop admins (see http://www.appslife-rdt.appspot.com) In VB.Dot.Net i used a call to the WMI space Win32_OperatingSystem and got the "Name" value back. This then needs to be split to get the first object which is what you require. i.e.

Dim query2 As New SelectQuery("SELECT * FROM Win32_OperatingSystem")
Dim searcher2 As New ManagementObjectSearcher(objManagementScope2, query2, QueryOptions)
        For Each mo As ManagementObject In searcher2.[Get]()
                           OSname = mo("Name")
        Next

        Dim array1 As Array = Nothing
        array1 = Split(OSname, "|")
        OSname = array1(0).ToString

OSname then gives you the "Windows 11 Pro" or "Windows XP Professional" that you require.

I also get the OSVersion with the latest patch update info from registry as follows if needed...

THIS IS FOR CLIENT VERSIONS 10/11 :-

.SetPropertyValue("sSubKeyName", "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64") .SetPropertyValue("sValueName", "Version")

THIS IS FOR SERVER VERSIONS 2019 :-

.SetPropertyValue("sSubKeyName", "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Server.OS.amd64") .SetPropertyValue("sValueName", "Version")

Hope this is of help.

Upvotes: 1

aggieNick02
aggieNick02

Reputation: 2787

tldr - Using the EditionID and the CurrentBuild from the CurrentVersion in the registry seems to be a reliable way to determine Win10 vs Win11 and the "edition" of the software. EditionID is "Professional" on Windows 10 Pro and Windows 11 Pro, and CurrentBuild >= ~22000 tells you if it is 10 or 11.

The collection of registry values in the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion reveals what feels like a lack of planning on Microsoft's part. There's ReleaseId, which is a number that changed with each Windows 10 release (e.g., 1903, 1909, 2004,...) until its last change for Windows 10 20H2, where it changed to 2009. At the same time, DisplayVersion was added, and was set to 20H2.

Then Windows 10 21H1 released, and ReleaseId inexplicably stayed at 2009.

The fact that both current Windows 10 and Windows 11 releases can have the same DisplayVersion (e.g., 21H2 when Windows 10 21H2 releases soon) and ProductName (e.g., Windows 10 Pro) is really head-scratching. (Thanks @yaakov for catching my mistake saying it was 21H1.)

Upvotes: 5

Related Questions