How to know which OS we r using?

Posted on 2001-08-24
Medium Priority
Last Modified: 2008-03-06
Is there any API function to know, which operating system is being used whether its Windows98/ME/2000?
Question by:rinosh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 53

Expert Comment

by:Ryan Chong
ID: 6421451

Obtaining Windows' Version Information:http://www.mvps.org/vbnet/code/system/getversionex.htm
LVL 39

Expert Comment

ID: 6421458
use GetVersionEx API to get the OS details

Expert Comment

ID: 6421462
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6421483
GetVersionEx gives u the info about the OS.

Expert Comment

ID: 6421544
Using microsoft sysinfo control . you can read many other property related operating system.


Expert Comment

ID: 6421580
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


Expert Comment

ID: 6421609
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 :)


Accepted Solution

Ryan9999 earned 150 total points
ID: 6421654
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


Author Comment

ID: 6424362
Thanks for the solution!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question