How to determine OS, which version, and which ver of windoz?

Hello all,

Can anyone tell me how to programatically know what version of what OS is being used? I will need to know the answer for all OS’, but right now, I am focused on knowing what version of Windoz is currently running - XP, Vista, 7, 8 etc.

Thank you,
Tim

Can’t help with Windows, but on the Mac there are a couple of ways. The most common is to use gestalts, but if you use the System Profiler, you can also get the build number of the OS.

Function getProfileInfo(Key as String, location as string) As string #if TargetMacOS then Dim s as new Shell Dim rvalue as string s.Execute "system_profiler "+location+" | grep "+chr(34)+key+chr(34) rvalue = trim(NthField(s.Result,":",2)) if rvalue <> "" then Return rvalue Return "unidentified" #endif End Function
Then pass in the following for location and key.

Return getProfileInfo("System Version","SPSoftwareDataType")

[quote=15559:@Sam Rowlands]Can’t help with Windows, but on the Mac there are a couple of ways. The most common is to use gestalts, but if you use the System Profiler, you can also get the build number of the OS.

Function getProfileInfo(Key as String, location as string) As string #if TargetMacOS then Dim s as new Shell Dim rvalue as string s.Execute "system_profiler "+location+" | grep "+chr(34)+key+chr(34) rvalue = trim(NthField(s.Result,":",2)) if rvalue <> "" then Return rvalue Return "unidentified" #endif End Function
Then pass in the following for location and key.

Return getProfileInfo("System Version","SPSoftwareDataType")

It’s worth nothing that shelling out is somewhat expensive and stalls your process until it gets the result back.

Here is what I use

  Dim noerror As Boolean
  Dim result As Integer
  Dim sver As String
  Dim sversion As String
  Dim os As String
  #If TargetMacOS
    noerror=System.Gestalt("sysv",result)
    If noerror Then
      sver=Hex(result)
      sversion=sver.Left(2) + "." + sver.Mid(3,1) + "." + sver.Right(1)
      OS_CODE=Val(sver.Mid(3,1))
      Select Case OS_CODE
        
      Case 0
        os="Cheetah"
      Case 1
        os="Puma"
      Case 2
        os="Jaguar"
      Case 3
        os="Panther"
      Case 4
        os="Tiger"
      Case 5
        os="Leopard"
      Case 6
        os="Snow Leopard"
      Case 7
        os="Lion"
      Case 8
        os="Mountain Lion"
      Case Else
        os="Unknown"
      End Select
      Return "Mac OSX "+os+" "+sversion
    Else
      Return ""
    End If
  #ElseIf TargetWin32
    
    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
    
    Dim Str As String
    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
      os = "Windows 8"
    End Select
    Str = " Build " + Str( info.Long( 12 ) )
    
    If System.IsFunctionAvailable( "GetVersionExW", "Kernel32" ) Then
      Str = Str + " " + Trim( info.WString( 20 ) )
    Else
      Str = Str + " " + Trim( info.CString( 20 ) )
    End If
    
    os = os + Str
    Return os
  #EndIf

@Dave S , You can add.

case 9 os="Mavericks"

You guys are just TOO COOL! Thank you for all of your replies! So much appreciated!

Tim

One question, does the compiler directive “#ElseIf TargetWin32” also detect if it is Windows 64 bit? Being that RS/xojo does not have a 64 bit solution, it seems reasonable that it would detect both. However it is also reasonable that it is, exactly what it states it is - 32 bit. So, if it does not detect 64 bit, how to know then it if it IS a windoz OS? And of course which one?

Thanks again all!
Tim

[quote=15567:@Tim Seyfarth]One question, does the compiler directive “#ElseIf TargetWin32” also detect if it is Windows 64 bit? Being that RS/xojo does not have a 64 bit solution, it seems reasonable that it would detect both. However it is also reasonable that it is, exactly what it states it is - 32 bit. So, if it does not detect 64 bit, how to know then it if it IS a windoz OS? And of course which one?

Thanks again all!
Tim[/quote]

TargetWin32 means “Windows using the Win32 APIs” and has nothing to do with 32- or 64-bit. In terms of compile time constants for determining that, you can use Target32Bit and Target64Bit.

Well, if you use MBS Plugins, you can use SystemInformationMBS module:
http://www.monkeybreadsoftware.net/module-systeminformationmbs.shtml

There are functions like Is64bitWindows and OSVersionString.

1 Like

Is there a simpler way for a software to determine, whether the operating system is simply Windows or MAC (not a specific version of the OS)?

To be used example if you are building a program in both Mac and windows that is depended on this info (GuancheMOS as an example)…

Ole

#if targetWin32
msgbox “this is Windows”
#elif targetMacOS
msgbox “This is Mac OS X”
#elif TargetLinux
msgbox “This is Linux”
#else
?
#endif

If TargetWin32 //do win stuff -- its Windows elseIf Target MacOS //do mac stuff -- its Mac end if

If TargetWin64
// Awesome
elseIf Target IOS
// It’s a miracle
end if

:slight_smile:

#if TargetLLVM
// We got a new compiler!
#endif

[quote]#if TargetLLVM
// We got a new compiler!
#endif[/quote]

:slight_smile: Lol stop teasing.

BTW well done with the SSH plugin

Thank you.

Thanks! :slight_smile:

how to determine the linux os version then ?

shell to

uname for name

uname -r for version

Dave’s code does not return a correct result on Yosemite.
https://forum.xojo.com/2239-how-to-determine-os-which-version-and-which-ver-of-windoz/p1#p15562

I would expect sver to be 1010 but it is 1090.

How can I determine in code that my app is running on yosemite?