Windows OS Version

Share your advanced PureBasic knowledge/code with the community.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Windows OS Version

Post by nco2k »

i updated my code, to support Windows 7 and Windows Server 2008 R2 and took the chance to post it in tricks n tips, so people may find it easier:

Code: Select all

#My_OS_Windows_NT3 = 5
#My_OS_Windows_95 = 10
#My_OS_Windows_NT4 = 15
#My_OS_Windows_98 = 20
#My_OS_Windows_ME = 25
#My_OS_Windows_2000 = 30
#My_OS_Windows_XP = 35
#My_OS_Windows_Server_2003 = 40
#My_OS_Windows_Vista = 45
#My_OS_Windows_Server_2008 = 50
#My_OS_Windows_7 = 55
#My_OS_Windows_Future = 100

Procedure MyOSVersionVal()
  
  Protected Result, osvi.OSVERSIONINFO, osviex.OSVERSIONINFOEX
  
  osvi\dwOsVersionInfoSize = SizeOf(OSVERSIONINFO)
  If GetVersionEx_(@osvi)
    Select osvi\dwPlatformId
      
      Case 1
        
        If osvi\dwMajorVersion = 4
          Select osvi\dwMinorVersion
            Case 0
              Result = #My_OS_Windows_95
            Case 10
              Result = #My_OS_Windows_98
            Case 90
              Result = #My_OS_Windows_ME
          EndSelect
        EndIf
        
      Case 2
        
        osviex\dwOsVersionInfoSize = SizeOf(OSVERSIONINFOEX)
        If GetVersionEx_(@osviex)
          Select osviex\dwMajorVersion
            
            Case 3
              Result = #My_OS_Windows_NT3
            Case 4
              Result = #My_OS_Windows_NT4
            Case 5
              Select osviex\dwMinorVersion
                Case 0
                  Result = #My_OS_Windows_2000
                Case 1
                  Result = #My_OS_Windows_XP
                Case 2
                  If osviex\wProductType = 1
                    Result = #My_OS_Windows_XP; 64Bit
                  Else
                    Result = #My_OS_Windows_Server_2003
                  EndIf
              EndSelect
            Case 6
              Select osviex\dwMinorVersion
                Case 0
                  If osviex\wProductType = 1
                    Result = #My_OS_Windows_Vista
                  Else
                    Result = #My_OS_Windows_Server_2008
                  EndIf
                Case 1
                  If osviex\wProductType = 1
                    Result = #My_OS_Windows_7
                  Else
                    Result = #My_OS_Windows_Server_2008; R2
                  EndIf
              EndSelect
              
          EndSelect
        Else
          Result = -1
        EndIf
        
    EndSelect
  Else
    Result = -1
  EndIf
  
  Select Result
    Case 0
      Result = #My_OS_Windows_Future
    Case -1
      Result = 0; Error
  EndSelect
  
  ProcedureReturn Result
EndProcedure

Procedure.s MyOSVersionStr()
  
  Protected Result.s
  
  Select MyOSVersionVal()
    Case #My_OS_Windows_NT3
      Result = "Windows NT3"
    Case #My_OS_Windows_95
      Result = "Windows 95"
    Case #My_OS_Windows_NT4
      Result = "Windows NT4"
    Case #My_OS_Windows_98
      Result = "Windows 98"
    Case #My_OS_Windows_ME
      Result = "Windows ME"
    Case #My_OS_Windows_2000
      Result = "Windows 2000"
    Case #My_OS_Windows_XP
      Result = "Windows XP"
    Case #My_OS_Windows_Server_2003
      Result = "Windows Server 2003"
    Case #My_OS_Windows_Vista
      Result = "Windows Vista"
    Case #My_OS_Windows_Server_2008
      Result = "Windows Server 2008"
    Case #My_OS_Windows_7
      Result = "Windows 7"
    Case #My_OS_Windows_Future
      Result = "Windows Future"
  EndSelect
  
  ProcedureReturn Result
EndProcedure

MessageRequester("My OS Version", MyOSVersionStr())
Description: http://msdn.microsoft.com/en-gb/library/ms724451.aspx

C++ Example: http://msdn.microsoft.com/en-gb/library/ms724429.aspx

c ya,
nco2k
Last edited by nco2k on Mon Apr 27, 2009 6:24 pm, edited 1 time in total.
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Windows OS Version

Post by PB »

You'd think that Microsoft would have a simple API call to return the string. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post by lexvictory »

thanks for this!
I've put it into droopy's lib for those who still use it
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Windows OS Version

Post by SFSxOI »

PB wrote:You'd think that Microsoft would have a simple API call to return the string. :)
They do, its in WMI though.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

@lexvictory
if you want to put it in a userlib, imo it would be best to provide two functions. one which returns a value and one which returns a string. i have edited my above post.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Post by lexvictory »

Droopy's lib already had a function to output it as a string, I replaced it with this one seeing as it can differentiate between server/workstation and supports windows 7
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
Post Reply