Results 1 to 10 of 10

Thread: list all keys, subkeys and values in registry hive

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    list all keys, subkeys and values in registry hive

    here is a recursive scripting method to get all keys and subkeys in a hive starting from root or any level
    takes a while, probably should have a doevents somewhere, to prevent computer lockup while running
    vb Code:
    1. Sub getsubkeys(hkey As Long, strpath As String, sk As Variant)
    2. Dim strkeypath As String, subkey As Variant, arrsubkeys As Variant
    3. strkeypath = strpath
    4. If Not Len(sk) = 0 And Not Len(strpath) = 0 Then strkeypath = strpath & "\"
    5. strkeypath = strkeypath & sk
    6. oreg.EnumKey hkey, strkeypath, arrsubkeys
    7. If Not IsNull(arrsubkeys) Then
    8. For Each subkey In arrsubkeys
    9.     Debug.Print strkeypath & "\" & subkey     ' or print to form or whatever
    10.       ' for speed considerations should be stored to memory (variable) and written to whatever in sections, trying to read the entire registry into memory may or may not be possible, depending on machine configuration
    11.     getsubkeys hkey, strkeypath, subkey
    12. Next
    13. End If
    14. End Sub
    15.  
    16. ' call like
    17. Sub yy()
    18. Const HKEY_CURRENT_USER As Long = &H80000001
    19. strComputer = "."
    20. Set oreg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
    21. getsubkeys HKEY_CURRENT_USER, "", ""   ' get from root level
    22. 'or
    23. getsubkeys HKEY_CURRENT_USER, "software\adobe", ""   ' starting at adobe
    24. Set oreg =  Nothing
    25. End Sub
    declare oreg as object in the general section
    Last edited by westconn1; Dec 24th, 2009 at 07:36 AM. Reason: modified function arguments
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: list all keys and subkeys in registry hive

    How/Where is "arrsubkeys" supposed to be defined?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: list all keys and subkeys in registry hive

    has to be declared variant, i have edited the code to add the declaration

    disclaimer here too, while this proceedure is pretty safe, anyone working with registry should take extra care, even to the extent of backing up the registry first

    there is no way the the immediate window can contain all the subkeys from a hive, (works ok for a smaller tree), debug.print is for test purposes only
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: list all keys and subkeys in registry hive

    Ok, but now I get error on line 11,

    ByRef argument type mismatch

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: list all keys and subkeys in registry hive

    i had not given a type to sk before, so it was a variant,
    either change sk to variant or cstr(subkey)
    i will modify the code to variant
    arrsubkeys and subkey are afaik, required to be variant
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: list all keys and subkeys in registry hive

    OK, changed to Variant, working now!

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: list all keys and subkeys in registry hive

    updated to list all values as well as the keys
    vb Code:
    1. Sub getvals(hkey As Long, strpath As String, sk As Variant)
    2.       Dim strkeypath As String, subkey As Variant, arrsubkeys As Variant, arrvalues As Variant
    3.       Dim strvalue As Variant, arrvals As Variant, i As Integer, arrtypes As Variant, v As Integer
    4.       DoEvents
    5.       strkeypath = strpath
    6.       If Not Len(sk) = 0 And Not Len(strpath) = 0 Then strkeypath = strpath & "\"
    7.       strkeypath = strkeypath & sk
    8.         oreg.enumvalues hkey, strkeypath, arrvalues, arrtypes
    9. '        Debug.Print strkeypath & "\" & subkey
    10.         If Not IsNull(arrvalues) Then
    11.             ReDim Preserve regd(UBound(regd) + UBound(arrvalues) + 2)
    12.             For i = 0 To UBound(arrvalues)
    13.             strvalue = vbNullString
    14.                 Select Case arrtypes(i)
    15.                     Case REG_SZ: oreg.getstringvalue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue
    16.                     Case REG_DWORD: oreg.GetDWORDValue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue
    17.                     Case REG_BINARY
    18.                         oreg.getbinaryvalue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue
    19.                         For v = 0 To UBound(strvalue)
    20.                             strvalue(v) = Format(Hex(strvalue(v)), "00")
    21.                         Next
    22.                         strvalue = Join(strvalue, ",")
    23.                     Case REG_EXPAND_SZ: oreg.GetExpandedStringValue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue
    24.                     Case REG_MULTI_SZ
    25.                         oreg.GetMultiStringValue hkey, strkeypath & "\" & subkey, arrvalues(i), arrvals
    26.                         strvalue = Join(arrvals, vbNewLine & String(6, vbTab))
    27.                 End Select
    28.                 regd(j).strKeyName = strkeypath & "\" & subkey
    29.                 regd(j).strValueName = arrvalues(i)
    30.                 regd(j).strData = strvalue & ""
    31.                 j = j + 1
    32. '                Debug.Print vbTab & vbTab & vbTab & arrvalues(i) & " = " & strvalue
    33.                     ' or print to form or whatever
    34.                 ' for speed considerations should be stored to memory (variable) and written to whatever in sections, trying to read the entire registry into memory may or may not be possible, depending on machine configuration
    35.             Next
    36.         Else
    37.                 oreg.getstringvalue hkey, strkeypath & "\" & subkey, "", strvalue, arrtypes
    38.                 If j > UBound(regd) Then ReDim Preserve regd(j)
    39.                 regd(j).strKeyName = strkeypath & "\" & subkey
    40.                 regd(j).strValueName = "@"
    41.                 regd(j).strData = strvalue & ""
    42.                 j = j + 1
    43.  
    44.         End If
    45.       oreg.EnumKey hkey, strkeypath, arrsubkeys
    46.       If Not IsNull(arrsubkeys) Then
    47.           For Each subkey In arrsubkeys
    48.               getvals hkey, strkeypath, subkey
    49.           Next
    50.       End If
    51.       End Sub

    you need to put in the general section at the top
    vb Code:
    1. Dim oreg As Object
    2. Private Const HKEY_LOCAL_MACHINE = &H80000002
    3. Private Const HKEY_CURRENT_USER As Long = &H80000001
    4. Private Const REG_SZ = 1
    5. Private Const REG_EXPAND_SZ = 2
    6. Private Const REG_BINARY = 3
    7. Private Const REG_DWORD = 4
    8. Private Const REG_MULTI_SZ = 7
    9. Private Type RegData
    10.     strKeyName As String
    11.     strValueName As String
    12.     strData As String
    13. End Type
    14.       Dim regd() As RegData, j As Long

    also need minor change to calling procedure
    vb Code:
    1. Sub yy()
    2.       strComputer = "."
    3.       ReDim regd(0)
    4.       j = 0
    5.       Set oreg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
    6. '      getvals HKEY_CURRENT_USER, "", ""   ' get from root level
    7.       'or
    8.       getvals HKEY_CURRENT_USER, "software\foxit software", ""   ' starting at some key
    9.       Set oreg = Nothing
    10.       If UBound(regd) > j - 1 Then ReDim Preserve regd(j - 1)  ' remove any empty elements from end of array
    11.       End Sub

    edit: i have fixed the code now to save binary values as a Hex string, a bit similar to regedit
    edit: i have modified the code to also return the default values, which are not returned by enumerating the values, if you don't want the empty ones added test for isnull(strvalue)
    i have assumed that the default value is always of type REG_SZ, if anyone knows better please pm me
    Last edited by westconn1; Jul 17th, 2011 at 12:42 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: list all keys, subkeys and values in registry hive

    Dear westconn1

    I am writing this reply on your comment on my question about registry manipulation. I copied your code to my excel file and compiled it, but met an error on line 8 in Sub getvals. Compliler says "arrtypes" was not defined. I also copied second code to general section (in the top of same module). Would you kindly explain why?

    Thank you.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: list all keys, subkeys and values in registry hive

    Compliler says "arrtypes" was not defined.
    you obviously have option explicit at the top of you module (as you should) , whereas i did not,
    arrtypes should be declared a variant and v as integer, i have amended the posted code above

    I also copied second code to general section (in the top of same module). Would you kindly explain why?
    as this is a recursive procedure, so the same sub runs many times, called from within itself the type and variables declared at the top of the module are able to be accessed from any instance of the procedure, whereas the variable declared within the procedure may be different for each instance of the procedure

    tested in excel 2000
    Last edited by westconn1; Jul 17th, 2011 at 12:44 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    Member
    Join Date
    Feb 2010
    Posts
    36

    Re: list all keys, subkeys and values in registry hive

    Dear westconn1

    I already solved my problem when I refer to your post above. I tried to post saying SOLVED but I just found it was not, right now.

    Thank you for your comment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width