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
 
 
 
 
 
TitleEnumerate the subkeys for a registry key
DescriptionThis example shows how to enumerate the subkeys for a registry key in Visual Basic 6. It uses RegEnumKey to enumerate the key's subkeys and then recursively examines the subkeys.
KeywordsRegsitry, System Registry, key, subkey
CategoriesTips and Tricks, Windows
 
When the form loads, it uses the GetKeyInfo function to list the subkeys of the Registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio.
 
Private Sub Form_Load()
Dim key_name As String

    key_name = "SOFTWARE\Microsoft\VisualStudio"
    txtKeys.Text = _
        key_name & vbCrLf & _
        GetKeyInfo(key_name, 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 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(HKEY_LOCAL_MACHINE, _
        key_name, _
        0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS _
    Then
        MsgBox "Error opening key."
        Exit Function
    End If
    
    ' Enumerate the subkeys.
    subkey_num = 0
    Do
        ' 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 _
        Then
            subkey_values.Add "Error"
        Else
            ' Remove the trailing null character.
            subkey_value = Left$(subkey_value, length - 1)
            subkey_values.Add subkey_value
        End If
    Loop
    
    ' 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) & _
            vbCrLf
        txt = txt & GetKeyInfo( _
            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.
  Updated