Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet the operating system version in Visual Basic 6
DescriptionThis example shows how to get the operating system version in Visual Basic 6.
KeywordsOS, OS version, operating system, operating system version, Visual Basic 6
CategoriesVB.NET, Windows
 
Function GetOSVersion uses the GetVersionExA API function to get operating system information. It looks at the returned platform ID, major, and minor version information to see which flavor or Windows is running.

If the platform is 2 (Win32NT) and the major version is 6, then the system is running Windows Vista or Windows 2008 Server. The function uses the Registry to figure out which.

 
Public Function GetOSVersion() As String
Dim version_info As OSVERSIONINFO
Dim txt As String
Dim hKey As Long

    GetOSVersion = "Unknown"

    version_info.dwOSVersionInfoSize = 148
    version_info.szCSDVersion = Space$(128)
    GetVersionExA version_info

    Select Case version_info.dwPlatformId
        Case 0
            GetOSVersion = "Win 3.1"
        Case 1
            Select Case version_info.dwMinorVersion
                Case 0
                    GetOSVersion = "Win95"
                Case 10
                    GetOSVersion = "Win98"
                Case 90
                    GetOSVersion = "WinME"
                Case Else
                    GetOSVersion = "Unknown Win32"
            End Select
        Case 2
            Select Case version_info.dwMajorVersion
                Case 3
                    GetOSVersion = "NT 3.51"
                Case 4
                    GetOSVersion = "NT 4.0"
                Case 5
                    Select Case version_info.dwMinorVersion
                        Case 0
                            GetOSVersion = "Win2000"
                        Case 1
                            GetOSVersion = "WinXP"
                        Case 2
                            GetOSVersion = "Win2003"
                    End Select
                Case 6
                    If RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
                        REG_PRODUCT_KEY, _
                        0&, KEY_QUERY_VALUE, hKey) <> _
                            ERROR_SUCCESS _
                    Then
                        txt = ""
                    Else
                        txt = GetRegistryValue(hKey, _
                            "ProductName")

                        ' Close the key.
                        If RegCloseKey(hKey) <> _
                            ERROR_SUCCESS Then
                            MsgBox "Error closing key."
                        End If
                    End If

                    txt = LCase$(txt)
                    If InStr(txt, "vista") > 0 Then
                        GetOSVersion = "Vista"
                    ElseIf InStr(txt, "server") > 0 Then
                        GetOSVersion = "Win2008"
                    Else
                        GetOSVersion = "Unknown Win32NT6"
                    End If
                Case Else
                    GetOSVersion = "Unknown Win32NT"
            End Select
        Case 3
            GetOSVersion = "Win CE"
        Case 4
            GetOSVersion = "Unix"
    End Select

    GetOSVersion = "Unknown"
End Function
 
When it starts, the program opens a Registry key leading to information about the operating system. It calls function GetRegistryValue to read values from that key.
 
Private Const REG_PRODUCT_KEY As String = _
    "SOFTWARE\Microsoft\Windows NT\CurrentVersion"

Private Sub Form_Load()
Dim hKey As Long

    Label1.Caption = Environ("OS")

    ' Get the interpreted version information.
    ' Open the key.
    If RegOpenKeyEx(HKEY_LOCAL_MACHINE, _
        REG_PRODUCT_KEY, _
        0&, KEY_QUERY_VALUE, hKey) <> ERROR_SUCCESS _
    Then
        MsgBox "Error opening key."
        Exit Sub
    End If

    ' Get the subkeys' values.
    lblProductName.Caption = GetRegistryValue(hKey, _
        "ProductName")
    lblCurrentBuildNumber.Caption = GetRegistryValue(hKey, _
        "CurrentBuildNumber")
    lblCurrentVersion.Caption = GetRegistryValue(hKey, _
        "CurrentVersion")
    lblCSDVersion.Caption = GetRegistryValue(hKey, _
        "CSDVersion")

    ' Close the key.
    If RegCloseKey(hKey) <> ERROR_SUCCESS Then
        MsgBox "Error closing key."
    End If
End Sub
 
Function GetRegistryValue uses RegQueryValueEx to read and return a Registry value.
 
' Return the registry value.
Private Function GetRegistryValue(ByVal hKey As Long, ByVal _
    subkey_name As String) As String
Dim value As String
Dim length As Long
Dim value_type As Long

    length = 256
    value = Space$(length)
    If RegQueryValueEx(hKey, subkey_name, _
        0&, value_type, ByVal value, length) _
            <> ERROR_SUCCESS _
    Then
        value = "<Error>"
    Else
        ' Remove the trailing null character.
        value = Left$(value, length - 1)
    End If
    
    GetRegistryValue = value
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated