"getOSName" and "getOSVersionNumber" functions...

Started by airr, June 21, 2024, 04:50:31 PM

Previous topic - Next topic

airr

In the "Com Programming Adventure" thread, I noted that querying the registry for OS info can return the incorrect OS name.

I threw the following together a little while ago that hopefully addresses that (needed this for work).

Can I ask someone who has Win10 (or anything other than Win11) to test this for me? It works on Win10 Enterprise and Win11 Enterprise at my job.

Thanks!



MACRO HKLM = HKEY_LOCAL_MACHINE
MACRO REGKEY = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

Function main(argc as integer, argv as pchar ptr)

    Print "OS Name: ", getOSName()
    Print "OS Version:", getOSVersionNumber()

    Pause   

End Function

Function getOSName() as string
    dim as string szProductName, szDisplayVersion, szOSName

    szProductName = REGSTRING$(HKLM, REGKEY, "ProductName")
    szDisplayVersion = REGSTRING$(HKLM, REGKEY, "DisplayVersion")

    if val(regstring$(HKLM, REGKEY, "CurrentBuildNumber")) >= 22000 Then
        szProductName = replace$(szProductName, "10","11")
    end if

    sprint szOSName, szProductName,spc$,szDisplayVersion
   
    Return szOSName

End Function

Function getOSVersionNumber() as string
    dim as integer iMajorVersion, iMinorVersion, iInstallDate, iUBR
    dim as string szBuildNumber, szOSBuild

    iMajorVersion = regint(HKLM, REGKEY, "CurrentMajorVersionNumber")
    iMinorVersion = regint(HKLM, REGKEY, "CurrentMinorVersionNumber")
    szBuildNumber = regstring$(HKLM, REGKEY, "CurrentBuildNumber")
    iInstallDate = regint(HKLM, REGKEY, "InstallDate")
    iUBR = regint(HKLM, REGKEY, "UBR")

    sprint szOSBuild, iMajorVersion, ".", str$(iMinorVersion,1), ".",szBuildNumber, ".", str$(iUBR,1)

    return szOSBuild
End Function


AIR.

Quin

This works on my Windows 10 22H2 system, printing:

OS Name: Windows 10 Home 22H2
OS Version: 10.0.19045.4529
-Quin.
GitHub

djsb

OS Name: Windows 10 Pro 22H2
OS Version: 10.0.19045.4529

On my laptop.

airr


MrBcx

OS Name: Windows 11 Pro 22H2
OS Version: 10.0.22621.3807

Press any key to continue . . .

airr

Thanks, MrB.

My personal machine shows:

OS Name: Windows 11 Pro 23H2
OS Version: 10.0.22631.3737

Press any key to continue . . .

AIR.