|
-
May 3rd, 2009, 06:10 AM
#1
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:
Sub getsubkeys(hkey As Long, strpath As String, sk As Variant)
Dim strkeypath As String, subkey As Variant, arrsubkeys As Variant
strkeypath = strpath
If Not Len(sk) = 0 And Not Len(strpath) = 0 Then strkeypath = strpath & "\"
strkeypath = strkeypath & sk
oreg.EnumKey hkey, strkeypath, arrsubkeys
If Not IsNull(arrsubkeys) Then
For Each subkey In arrsubkeys
Debug.Print strkeypath & "\" & subkey ' or print to form or whatever
' 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
getsubkeys hkey, strkeypath, subkey
Next
End If
End Sub
' call like
Sub yy()
Const HKEY_CURRENT_USER As Long = &H80000001
strComputer = "."
Set oreg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
getsubkeys HKEY_CURRENT_USER, "", "" ' get from root level
'or
getsubkeys HKEY_CURRENT_USER, "software\adobe", "" ' starting at adobe
Set oreg = Nothing
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
-
May 3rd, 2009, 11:58 AM
#2
Re: list all keys and subkeys in registry hive
How/Where is "arrsubkeys" supposed to be defined?
-
May 3rd, 2009, 04:24 PM
#3
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
-
May 4th, 2009, 03:42 AM
#4
Re: list all keys and subkeys in registry hive
Ok, but now I get error on line 11,
ByRef argument type mismatch
-
May 4th, 2009, 03:47 AM
#5
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
-
May 4th, 2009, 03:48 AM
#6
Re: list all keys and subkeys in registry hive
OK, changed to Variant, working now!
-
Dec 24th, 2009, 07:31 AM
#7
Re: list all keys and subkeys in registry hive
updated to list all values as well as the keys
vb Code:
Sub getvals(hkey As Long, strpath As String, sk As Variant) Dim strkeypath As String, subkey As Variant, arrsubkeys As Variant, arrvalues As Variant Dim strvalue As Variant, arrvals As Variant, i As Integer, arrtypes As Variant, v As Integer DoEvents strkeypath = strpath If Not Len(sk) = 0 And Not Len(strpath) = 0 Then strkeypath = strpath & "\" strkeypath = strkeypath & sk oreg.enumvalues hkey, strkeypath, arrvalues, arrtypes ' Debug.Print strkeypath & "\" & subkey If Not IsNull(arrvalues) Then ReDim Preserve regd(UBound(regd) + UBound(arrvalues) + 2) For i = 0 To UBound(arrvalues) strvalue = vbNullString Select Case arrtypes(i) Case REG_SZ: oreg.getstringvalue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue Case REG_DWORD: oreg.GetDWORDValue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue Case REG_BINARY oreg.getbinaryvalue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue For v = 0 To UBound(strvalue) strvalue(v) = Format(Hex(strvalue(v)), "00") Next strvalue = Join(strvalue, ",") Case REG_EXPAND_SZ: oreg.GetExpandedStringValue hkey, strkeypath & "\" & subkey, arrvalues(i), strvalue Case REG_MULTI_SZ oreg.GetMultiStringValue hkey, strkeypath & "\" & subkey, arrvalues(i), arrvals strvalue = Join(arrvals, vbNewLine & String(6, vbTab)) End Select regd(j).strKeyName = strkeypath & "\" & subkey regd(j).strValueName = arrvalues(i) regd(j).strData = strvalue & "" j = j + 1 ' Debug.Print vbTab & vbTab & vbTab & arrvalues(i) & " = " & strvalue ' or print to form or whatever ' 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 Next Else oreg.getstringvalue hkey, strkeypath & "\" & subkey, "", strvalue, arrtypes If j > UBound(regd) Then ReDim Preserve regd(j) regd(j).strKeyName = strkeypath & "\" & subkey regd(j).strValueName = "@" regd(j).strData = strvalue & "" j = j + 1 End If oreg.EnumKey hkey, strkeypath, arrsubkeys If Not IsNull(arrsubkeys) Then For Each subkey In arrsubkeys getvals hkey, strkeypath, subkey Next End If End Sub
you need to put in the general section at the top
vb Code:
Dim oreg As Object Private Const HKEY_LOCAL_MACHINE = &H80000002 Private Const HKEY_CURRENT_USER As Long = &H80000001 Private Const REG_SZ = 1 Private Const REG_EXPAND_SZ = 2 Private Const REG_BINARY = 3 Private Const REG_DWORD = 4 Private Const REG_MULTI_SZ = 7 Private Type RegData strKeyName As String strValueName As String strData As String End Type Dim regd() As RegData, j As Long
also need minor change to calling procedure
vb Code:
Sub yy() strComputer = "." ReDim regd(0) j = 0 Set oreg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv") ' getvals HKEY_CURRENT_USER, "", "" ' get from root level 'or getvals HKEY_CURRENT_USER, "software\foxit software", "" ' starting at some key Set oreg = Nothing If UBound(regd) > j - 1 Then ReDim Preserve regd(j - 1) ' remove any empty elements from end of array 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
-
Jul 15th, 2011, 06:26 AM
#8
Member
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.
-
Jul 17th, 2011, 12:41 AM
#9
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
-
Jul 17th, 2011, 03:00 AM
#10
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|