What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGet subkeys from any part of the registry
DescriptionThis example shows how to get subkeys from any part of the registry in Visual Basic 6.
KeywordsRegsitry, System Registry, key, subkey
CategoriesTips and Tricks, Windows
When the user selects a Registry section, enters a key, and clicks a button, the program calls subroutine GetKeyInfo, passing it the section, key name, and an indentation level.
Private Sub Command1_Click()
    txtKeys.Text = Text1.Text & vbCrLf & _
        GetKeyInfo(m_SelectedSection, Text1.Text, 4)
End Sub
Function GetKeyInfo uses RegOpenKeyEx to open a key. It then calls RegEnumKey repeatedly to get subkey values until RegEnumKey returns something other than ERROR_SUCCESS. When it finds a subkey, it saves its name in the subkeys collection and saves the subkey's value in the subkey_values collection.

The function closes the Registry key and recursively calls itself to examine the subkeys.

' Get the key information for this key and
' its subkeys.
Private Function GetKeyInfo(ByVal section As Long, ByVal _
    key_name As String, ByVal indent As Integer) As String
Dim subkeys As Collection
Dim subkey_values As Collection
Dim subkey_num As Integer
Dim subkey_name As String
Dim subkey_value As String
Dim length As Long
Dim hKey As Long
Dim txt As String

    Set subkeys = New Collection
    Set subkey_values = New Collection
    ' Open the key.
    If RegOpenKeyEx(section, _
        key_name, _
        0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS _
        MsgBox "Error opening key."
        Exit Function
    End If
    ' Enumerate the subkeys.
    subkey_num = 0
        ' Enumerate subkeys until we get an error.
        length = 256
        subkey_name = Space$(length)
        If RegEnumKey(hKey, subkey_num, _
            subkey_name, length) _
                <> ERROR_SUCCESS Then Exit Do
        subkey_num = subkey_num + 1
        subkey_name = Left$(subkey_name, InStr(subkey_name, _
            Chr$(0)) - 1)
        subkeys.Add subkey_name
        ' Get the subkey's value.
        length = 256
        subkey_value = Space$(length)
        If RegQueryValue(hKey, subkey_name, _
            subkey_value, length) _
            <> ERROR_SUCCESS _
            subkey_values.Add "Error"
            ' Remove the trailing null character.
            subkey_value = Left$(subkey_value, length - 1)
            subkey_values.Add subkey_value
        End If
    ' Close the key.
    If RegCloseKey(hKey) <> ERROR_SUCCESS Then
        MsgBox "Error closing key."
    End If

    ' Recursively get information on the keys.
    For subkey_num = 1 To subkeys.Count
        txt = txt & Space$(indent) & _
            subkeys(subkey_num) & _
            ": " & subkey_values(subkey_num) & _
        txt = txt & GetKeyInfo(section, _
            key_name & "\" & subkeys(subkey_num), _
            indent + 4)
    Next subkey_num
    GetKeyInfo = txt
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.