Determine which version of Windows OS

I realize this question has been answered before but nothing with recent Windows OS.
I have modified my code to work for all versions up to Windows 10 but it doesn’t see Windows 11 as being different.

#elseif TargetWin32
  dim OS_CODE as Integer
  os = "Windows"
  
  //try to be more specific of windows version
  Soft Declare Sub GetVersionExA Lib "Kernel32" ( info As Ptr )
  Soft Declare Sub GetVersionExW Lib "Kernel32" ( info As Ptr )
  
  Dim info As MemoryBlock
  
  If System.IsFunctionAvailable( "GetVersionExW", "Kernel32" ) Then
    info =  New MemoryBlock( 20 + (2 * 128) )
    info.Long( 0 ) = info.Size
    GetVersionExW( info )
  Else
    info =  New MemoryBlock( 148 )
    info.Long( 0 ) = info.Size
    GetVersionExA( info )
  End If
  
  OS_CODE=info.Long(4)*100+info.long(8)
  
  Select Case OS_CODE
  Case 400  
    os = "Windows 95/NT 4.0"
  Case 410
    os = "Windows 98"
  Case 490
    os = "Windows Me"
  Case 300 To 399
    os = "Windows NT 3.51"
    OS_CODE=30
  Case 500
    os = "Windows 2000"
  Case 501
    os = "Windows XP"
  Case 502
    os = "Windows Server 2003"
  Case 600
    os = "Windows Vista"
  Case 601
    os = "Windows 7"
  case 602 // 32bit Windows 8.1 and 10
    dim s as new shell
    s.execute("ver")
    dim res as string = s.Result
    if val(mid(res,30,2)) = 10 then
      os = "Windows 10"
    else
      os = "Windows 8/8.1"
    end if
  case 603 // 64 bit Windows 8.1
    os = "Windows 8/8.1"
  case 1000 // 64 bit Windows 10
    os = "Windows 10/11"
 
  case else
    os = "Unknown Windows build - " + str(OS_CODE)
  End Select

Have you tried using System.VersionData?

Thanks Anthony, I thought there might be a built in function by now but for some reason my search of the Documentation showed nothing.

1 Like

As I understand it, System.Version still can’t detect Windows 11:

The ToString function? That’s too bad.

I would suspect the reported MajorVersion is incorrect leading to the ToString function generating the unexpected value. Can’t complain though, the documentation says that Xojo doesn’t officially support Windows 11 :upside_down_face:

I don’t have it front of me at the moment, but it takes the build number to distinguish 10 from 11.

I’m not trying to be annoying, sorry if I am, I just need to know that it works really. I currently have to use SystemInformationMBS as a workaround.

well I thought last build number of Win 10 was [Version 10.0.19044.1706]

So anything 10.0.22000 would be Win 11?

1 Like

Currently 10.0.22000 and 10.0.22621 is Win 11.

22000 today is 21H2 release, but 22621 is the next RTM for the next 22H2.

1 Like

Your code has many problems, if you use the shell, why bother with the API? But your shell code is going to fail if the os is on other languaje.

Using only the MajorVersion and MinorVersion is wrong, 10.0 is vallid for 5 diferent Versions:
-Windows 11
-Windows 10
-Windows Server 2022
-Windows Server 2019
-Windows Server 2016

Instead of the “OS_CODE”, get the dwMajorVersion, dwMinorVersion, dwBuildNumber and dwPlatformId:

Dim dwMajorVersion As UInt32 = info.UInt32Value(4)
Dim dwMinorVersion As UInt32 = info.UInt32Value(8)
Dim dwBuildNumber As UInt32 = info.UInt32Value(12)
Dim dwPlatformId As UInt32 = info.UInt32Value(16)

Then use them to find out the windows version:

https://docs.microsoft.com/en-us/windows/release-health/release-information

If you have latest Xojo, you can use the System.VersionData, PatchVersion gives you the it gives you the BuildNumber.

1 Like

If you use MBS Xojo Plugins, you can also check SystemInformationMBS module there.
It can tell you whether it’s Windows 10, 11 or older.

1 Like