How to know which OS we r using?

Is there any API function to know, which operating system is being used whether its Windows98/ME/2000?
Who is Participating?
here ya goOption Explicit


Private Type OSVersionInfo
  OSVSize As Long
  dwVerMajor As Long
  dwVerMinor As Long
  dwBuildNumber As Long
  PlatformID As Long
  szCSDVersion As String * 128
End Type

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
  (lpVersionInformation As OSVersionInfo) As Long
Private Function GetOSVer() As String
  Dim osv As OSVersionInfo
  osv.OSVSize = Len(osv)
  If GetVersionEx(osv) = 1 Then
    Select Case osv.PlatformID
      Case Is = VER_PLATFORM_WIN32s
        GetOSVer = "Windows 3.x"
        Select Case osv.dwVerMinor
          Case Is = 0
            If InStr(UCase(osv.szCSDVersion), "C") Then
              GetOSVer = "Windows 95 OSR2"
              GetOSVer = "Windows 95"
            End If
          Case Is = 10
            If InStr(UCase(osv.szCSDVersion), "A") Then
              GetOSVer = "Windows 98 SE"
              GetOSVer = "Windows 98"
            End If
          Case Is = 90
            GetOSVer = "Windows Me"
        End Select
      Case Is = VER_PLATFORM_WIN32_NT
        Select Case osv.dwVerMajor
          Case Is = 3
            Select Case osv.dwVerMinor
              Case Is = 0
                GetOSVer = "Windows NT 3"
              Case Is = 1
                GetOSVer = "Windows NT 3.1"
              Case Is = 5
                GetOSVer = "Windows NT 3.5"
              Case Is = 51
                GetOSVer = "Windows NT 3.51"
            End Select
          Case Is = 4
            GetOSVer = "Windows NT 4"
          Case Is = 5
            Select Case osv.dwVerMinor
              Case Is = 0
                GetOSVer = "Windows 2000"
              Case Is = 1
                GetOSVer = "Whistler"
            End Select
        End Select
    End Select
  End If
End Function

Ryan ChongCommented:

Obtaining Windows' Version Information:
use GetVersionEx API to get the OS details
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Add microsoft sysinfo control in your project

add a command button in your form & gice name of your sysinfo control name sysDetectOS
then run following code

Private Sub Command1_Click()

   Dim MsgEnd As String
   Select Case sysDetectOS.OSPlatform
      Case 0
         MsgEnd = "Unidentified"
      Case 1
         MsgEnd = "Windows 95, ver. " & _
CStr(sysDetectOS.OSVersion) & "(" & _
CStr(sysDetectOS.OSBuild) & ")"
      Case 2
         MsgEnd = "Windows NT, ver. " & _
CStr(sysDetectOS.OSVersion) & "(" & _
CStr(sysDetectOS.OSBuild) & ")"
   End Select
   MsgBox "System: " & MsgEnd

End Sub

GetVersionEx gives u the info about the OS.
Using microsoft sysinfo control . you can read many other property related operating system.

A Complete answer:
Create a form and past the following lines into it:

Private Sub Command1_Click()
Dim sHold3 As String, sPath As String
sPath = "SOFTWARE\Microsoft\Windows\CurrentVersion"
sHold3 = GetFieldFromRec(1, "Version", ",")
MsgBox (RegGetStringValue(HKEY_LOCAL_MACHINE, sPath, sHold3))
End Sub

Create a Module and past the following lines into it:

Option Explicit

' -----------------
' -----------------
' function prototypes, constants, and type definitions
' for Windows 32-bit Registry API

Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_READ = &H20000
Public Const STANDARD_RIGHTS_WRITE = &H20000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const REG_DWORD = 4
Public Const REG_BINARY = 3
Public Const REG_SZ = 1
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&

' Registry API prototypes

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long

Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long

Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public syspath As String

Function WindowsDirectory() As String 'get windows directory
Dim WinPath As String
Dim temp
    WinPath = String(145, Chr(0))
    temp = GetWindowsDirectory(WinPath, 145)
    WindowsDirectory = Left(WinPath, InStr(WinPath, Chr(0)) - 1)
syspath = WindowsDirectory
End Function

Public Function RegGetStringValue(hKey As Long, sPath As String, sValue As String)
    Dim lKeyHand As Long
    Dim lValueType As Long
    Dim lResult As Long
    Dim sBuff As String
    Dim lDataBufSize As Long
    Dim iZeroPos As Integer
    RegOpenKey hKey, sPath, lKeyHand                            'open the key we are looking at
    lResult = RegQueryValueEx(lKeyHand, sValue, 0&, lValueType, ByVal 0&, lDataBufSize)
    If lValueType = REG_SZ Then                                 'is it a Null terminated string?
        sBuff = String$(lDataBufSize, " ")                      'set the Buffer size
        lResult = RegQueryValueEx(lKeyHand, sValue, 0&, 0&, ByVal sBuff, lDataBufSize)
        If lResult = ERROR_SUCCESS Then
            iZeroPos = InStr(sBuff, Chr$(0))                    'is there a null in the returned value?
            If Not iZeroPos = 0 Then
                RegGetStringValue = Left$(sBuff, iZeroPos - 1)       'yes? then get everything up to the null, and return the value
                RegGetStringValue = sBuff                           'no?  then return the value
            End If
        End If
    End If
    RegCloseKey lKeyHand
End Function

Function RegGetEnumValue(hKey As Long, sPath As String)
    Dim lKeyHandle As Long, lResult As Long, lCurIdx As Long
    Dim sValue As String, sResult As String
    Dim lValueLen As Long, lData As Long, lDataLen As Long
    lResult = RegOpenKeyEx(hKey, sPath, 0&, KEY_READ, lKeyHandle)       'Open the path we are going to enumerate
    If Not lResult = ERROR_SUCCESS Then Exit Function                   'if we fail, then exit >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        lValueLen = 2000
        sValue = String(lValueLen, 0)
        lDataLen = 2000
        lResult = RegEnumValue(lKeyHandle, lCurIdx, ByVal sValue, lValueLen, 0&, REG_DWORD, ByVal lData, lDataLen)
        lCurIdx = lCurIdx + 1
        If lResult = ERROR_SUCCESS Then                                 'does the item exist
            sResult = Left(sValue, lValueLen) & "," & sResult
        End If
    Loop While lResult = ERROR_SUCCESS                                  'loop until we run out of items
    Call RegCloseKey(lKeyHandle)
    If Not Len(sResult) = 0 Then sResult = Left(sResult, Len(sResult) - 1)  'remove the last comma
    RegGetEnumValue = sResult
End Function

Function CountItems(sField As String, sSep As String) As Integer
'Counts the number of items in a delimited string
'Pass the String that has the list, and the seperator
'iCount = sField, sSep
'sField = the list, sSep is the seperator to count by
    Dim bNotFound As Boolean
    Dim iPos As Integer, iCount As Integer
    Do Until bNotFound                                 'loop until we have counted all the items
        iPos = InStr(iPos + 1, sField, sSep)
        If iPos = 0 Then
            bNotFound = True                            'if we are done, then leave
            iCount = iCount + 1                         'increment the counter
        End If
    CountItems = iCount                                 'return what we found
End Function

Public Function GetFieldFromRec(iFieldNum As Integer, sLine As String, sSep As String) As String
    'Returns a field from a record separated by a passed character
    'Calling syntax:
    'sR = GetFieldFromRec(i, s, c)
    'where i is the field number (0 based), s is the record string, c is the separating character
    Dim i        As Integer
    Dim iTabPos  As Integer
    Dim sTemp    As String
    For i = 1 To iFieldNum
        iTabPos = InStr(iTabPos + 1, sLine, sSep)
    Next i
    sTemp = Mid$(sLine, iTabPos + 1) & sSep         'Truncate unwanted preceding fields
    sTemp = Mid$(sTemp, 1, InStr(sTemp, sSep) - 1)  'Truncate unwanted following fields
    GetFieldFromRec = sTemp
End Function
Public Function ShowatStartup()
'Put calender in the registry
Dim Reg As New clsEasyRegistry
Dim avntVariable As Variant
Dim lngType As Long
Dim lngJ As Long
  Reg.ValueOf("EnCryptor", erSTRING) = App.Path & "\EnCryptor.exe"
  avntVariable = Reg.ValueOf("EnCryptor", lngType)
  Select Case lngType
    Case erSTRING
      Debug.Print "Type : " & "String"
  End Select
  Debug.Print "Value (Hex) : " & avntVariable(erStringFormat)
End Function
Public Function DontShowatStartup()
'Put calender in the registry
Dim Reg As New clsEasyRegistry
Dim avntVariable As Variant
Dim lngType As Long
Dim lngJ As Long
'If CalendarColor.chkShowatStartup.Value = 0 Then
  Reg.ValueOf("EnCryptor", erSTRING) = App.Path & "\EnCryptor.exe"
  Reg.DeleteValue "EnCryptor"
'  End If
End Function

Public Function LoadSet()
' Retrieve the settings.
Dim MySettings As Variant, intSettings As Integer
MySettings = GetAllSettings(Appname:="Calender Options", Section:="Startup")
   For intSettings = LBound(MySettings, 1) To UBound(MySettings, 1)
      Debug.Print MySettings(intSettings, 0), MySettings(intSettings, 1)
   Next intSettings
End Function
Public Function DeleteSet()
DeleteSetting "Calender Options", "Startup"
End Function

For More Details About The System, Add The following To the
"Private Sub Command1_Click()":

Dim sHold As String, sHold1 As String
Dim sHold2 As String, sHold3 As String
Dim sHold4 As String

sHold = GetFieldFromRec(1, "RegisteredOwner", ",")
sHold1 = GetFieldFromRec(1, "RegisteredOrganization", ",")
sHold2 = GetFieldFromRec(1, "SystemRoot", ",")
sHold3 = GetFieldFromRec(1, "Version", ",")
sHold4 = GetFieldFromRec(1, "VersionNumber", ",")

MsgBox (RegGetStringValue(HKEY_LOCAL_MACHINE, sPath, sHold))
MsgBox (RegGetStringValue(HKEY_LOCAL_MACHINE, sPath, sHol1))
MsgBox (RegGetStringValue(HKEY_LOCAL_MACHINE, sPath, sHold2))
MsgBox (RegGetStringValue(HKEY_LOCAL_MACHINE, sPath, sHold3))
MsgBox (RegGetStringValue(HKEY_LOCAL_MACHINE, sPath, sHold4))

Hope it's will help :)

rinoshAuthor Commented:
Thanks for the solution!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.