Loop (recursively) through registry subkeys

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Loop (recursively) through registry subkeys

Post by firace »

I'm trying to list all subkeys of a registry branch.

I think I'm almost there, but can't figure out how to show the full path+name of each subkey.

For instance, instead of "220\NGen\Policy", I would like to get "SOFTWARE\Microsoft\.NETFramework\NGen\Policy"

Any ideas?

Code: Select all


; note: based on an old snippet by Fred

Procedure RegListKeysRecursive(StartKey, pKeyName$)
  
  Result = ~#ERROR_SUCCESS
  
  If pKeyName$
    
    Result = RegOpenKeyEx_(StartKey, pKeyName$, 0, #KEY_ENUMERATE_SUB_KEYS , @Key )
    If Result = #ERROR_SUCCESS
      
      SubKeyName$ = Space(512)
      
      While Result = #ERROR_SUCCESS And Quit = 0
        SubKeyLength = 512
        Result = RegEnumKeyEx_(Key, SubKeyIndex, SubKeyName$, @SubKeyLength, 0, 0, 0, 0)
        SubKeyIndex+1
        
        If Result = #ERROR_NO_MORE_ITEMS
          If Len(Trim(SubKeyName$)) > 1
            Debug Str(key) + "\"+ pKeyName$ + "\" + SubKeyName$
            EndIf
          Result = #ERROR_SUCCESS
          Quit = 1
          
        ElseIf Result = #ERROR_SUCCESS
          
          If RegListKeysRecursive(Key, SubKeyName$)
            Result = #ERROR_SUCCESS;
          EndIf
        EndIf
      Wend
      
      RegCloseKey_(Key)
    EndIf
  EndIf
  
  If Result = #ERROR_SUCCESS
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf 
  
EndProcedure


; Test 
;
RegListKeysRecursive(#HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\.NETFramework")

forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: Loop (recursively) through registry subkeys

Post by forumuser »

E.g.:

Code: Select all

; note: based on an old snippet by Fred

Procedure RegListKeysRecursive(StartKey, pKeyName$)
  Static rootKey$
  If rootKey$ = "" : rootKey$ = pKeyName$ : EndIf

  Result = ~#ERROR_SUCCESS

  If pKeyName$

    Result = RegOpenKeyEx_(StartKey, pKeyName$, 0, #KEY_ENUMERATE_SUB_KEYS, @Key)
    If Result = #ERROR_SUCCESS

      SubKeyName$ = Space(512)

      While Result = #ERROR_SUCCESS And Quit = 0
        SubKeyLength = 512
        Result = RegEnumKeyEx_(Key, SubKeyIndex, SubKeyName$, @SubKeyLength, 0, 0, 0, 0)
        SubKeyIndex+1

        If Result = #ERROR_NO_MORE_ITEMS
          If Len(Trim(SubKeyName$)) > 1 And rootKey$ <> pKeyName$
            Debug rootKey$ + "\"+ pKeyName$ + "\" + SubKeyName$
          EndIf
          Result = #ERROR_SUCCESS
          Quit = 1

        ElseIf Result = #ERROR_SUCCESS

          If RegListKeysRecursive(Key, SubKeyName$)
            Result = #ERROR_SUCCESS
          EndIf
        EndIf
      Wend

      RegCloseKey_(Key)
    EndIf
  EndIf

  If Result = #ERROR_SUCCESS
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf

EndProcedure



; Test
;
RegListKeysRecursive(#HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\.NETFramework")
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Loop (recursively) through registry subkeys

Post by firace »

Thanks a lot!

But meanwhile I've realized there is another issue: the code fails to find some of the subkeys. For instance it's missing lots of subkeys in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\policy\Servicing :(
forumuser
User
User
Posts: 98
Joined: Wed Apr 18, 2018 8:24 am

Re: Loop (recursively) through registry subkeys

Post by forumuser »

Did you try #KEY_ALL_ACCESS instead of #KEY_ENUMERATE_SUB_KEYS?

That alone yields approx. twice the number of entries.

And if this doesn't help, the mentioned keys probably need higher permissions (did you try it with "Start as administrator")?
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Loop (recursively) through registry subkeys

Post by firace »

forumuser wrote:Did you try #KEY_ALL_ACCESS instead of #KEY_ENUMERATE_SUB_KEYS?

That alone yields approx. twice the number of entries.

And if this doesn't help, the mentioned keys probably need higher permissions (did you try it with "Start as administrator")?
That didn't help unfortunately.

The issue is more obvious when starting from the "policy" key:

RegListKeysRecursive(#HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\.NETFramework\policy")


The output doesn't match what regedit shows (with the same permissions).


EDIT: I've found another set of procedures that seems to work better (haven't fully tested yet):

viewtopic.php?f=13&t=14091
Post Reply