Page 1 of 1 1
Topic Options
#181207 - 2007-10-06 03:11 PM Enumerating the Registry
Arkane Offline
Getting the hang of it

Registered: 2004-08-14
Posts: 50
Loc: Bath, UK
I'm trying to write a script that will enumerate (and display) all keys, subkeys and values in the registry.

I've looked at EnumKey and EnumValue but I can't seem to enumerate ALL keys and subkeys, it seems to only enumerate keys up until it finds it can't enumerate anymore (HKCU\SomeKey\SomeKey1 etc..) but it doesn't go back to the next root-key (HKCU\SomeKey2, HKCU\SomeKey3 etc..).

Does anyone have any ideas why this might be?

The code I'm using is below :

 Code:
Function EnumRegKeys($Key)
	$SubKey = EnumKey($Key, $KIndex)
	
	WHILE @ERROR = 0
		$SaveKey = $Key
   		$Key = $Key + "\" + $SubKey
		? "DEBUG-RK [" + $Key + "]"
		EnumRegKeys($Key)
		$Key = $SaveKey
   		IF @ERROR = 0
			; try for next subkey
			$KIndex = $KIndex + 1
      		$SubKey = EnumKey($Key, $KIndex)
   		ENDIF
	LOOP
EndFunction 


The $key value is currently HKEY_CURRENT_USER.

Any advice or assistance would be greatly appreciated.

Arkane


[changed quote to code - ntdoc]

Top
#181209 - 2007-10-06 03:19 PM Re: Enumerating the Registry [Re: Arkane]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
You should be thinking about "recursion" here.. where your function calls itself when it encounters a KEY to enumerate the SubKEYs. When there are no more SubKEYs, you enumerate the Values.

Recursion is a fairly simple implementation, but can be an intimidating concept if you haven't used it before.

Jens has a great example of recursion in his DirList UDF that could point you in the right direction.

Give that a look, and pop back if you need more help.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#181210 - 2007-10-06 03:25 PM Re: Enumerating the Registry [Re: Glenn Barnas]
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I think Shawn also has a Registry Browser that would demonstrate how to do it. You may have to check the KiXforms site for it.

However, enumarting/displaying all registry keys and values would be extremely slow with KiXtart. Why do you eed to do this? What's the business case?
_________________________
There are two types of vessels, submarines and targets.

Top
#181212 - 2007-10-06 05:00 PM Re: Enumerating the Registry [Re: Sealeopard]
Arkane Offline
Getting the hang of it

Registered: 2004-08-14
Posts: 50
Loc: Bath, UK
Glen: I've looked at the DirList UDF - I still can't see what I'm doing wrong, it's probably something really simple that I'm missing.

If you take the script I wrote - run it against HKEY_CURRENT_USER you will see what I mean, it enumerates the subkeys but doesn't jump to the next root-key and I'm not sure how to make it do this.

Sealeopard: I appreciate that it may be extremely slow, is there a better way to do this? All I'd need is something that could perhaps enumerate all keys/values in a specified key (perhaps passed as an argument) and then it dumps it out to a text file (not a .reg file) and then I could get KiX to enumerate that instead.

The business case? Hard to say, more curiousity than a business case. I wanted to see how easy it would be (and how quick) to enumerate a registry hive with KIX and dump it out to a file. I'm wanting to compare 2 sets of registry information from different users - I've looked for free tools, most of them are GUI based and I'm looking for something small, quick and command-line based that would dump out the differences to a text file.

Arkane

Top
#181227 - 2007-10-06 11:54 PM Re: Enumerating the Registry [Re: Arkane]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Well I'd look at these two UDFs from Jens that certainly can do it for you and if you want you can see how it's being done.

ARRAYENUMKEY() - Creates an array of names of the subkeys contained in a registry key
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=82422


ArrayEnumValue() - Creates an array of names of the registry entries contained in a registry key or subkey
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=82425

Top
#181243 - 2007-10-07 09:56 AM Re: Enumerating the Registry [Re: NTDOC]
Arkane Offline
Getting the hang of it

Registered: 2004-08-14
Posts: 50
Loc: Bath, UK
I've looked at both of those UDFs and saved them for greater study later on, but I've run them and they only seem to enumerate the first set of keys, they don't seem to go into subkeys, is this by design or is it really simple to make them do this?
Top
#181259 - 2007-10-07 11:01 PM Re: Enumerating the Registry [Re: Arkane]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I think you want to use a function that recurses itself.
First time trying to write something like that. I hope it works as it should.
;*************************************************************************
;  Script Name:   EnumKeyAll
;  Author:        Wim Rotty
;  Date:          7/10/2007
;  Description:   Recursive function to list all subkeys
;*************************************************************************

 
;Script Options
If Not @LOGONMODE
   Break On
Else
   Break Off
EndIf
Dim $RC
$RC = SetOption("Explicit", "On")
$RC = SetOption("NoMacrosInStrings", "On")
$RC = SetOption("NoVarsInStrings", "On")
If @SCRIPTEXE = "KIX32.EXE"
   $RC = SetOption("WrapAtEOL", "On")
EndIf

;Declare variables
Dim
 
$Key

;Initialize variables
$Key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion";\Uninstall";\Installshield Uninstall Information"

;Code
???

For Each $RC in EnumKeyAll($Key)
   
$RC ?
Next
Get $RC

;Personal UDF Section
Function
 EnumKeyAll($Key)
   
Dim $i, $strKey, $arrKeys[0]
   
ReDim Preserve $EnumKeyAll[0]
   
$i = 0
   
$strKey = EnumKey($Key,$i)
   
While Not @ERROR
        $strKey = $Key+"\"+$strKey
        ;$strKey ?
        If Not $EnumKeyAll[0] = ""
            ReDim Preserve $EnumKeyAll[UBound($EnumKeyAll)+1]
       
EndIf
        $EnumKeyAll[UBound($EnumKeyAll)] = $strKey
        $i = $i + 1
       
$strKey = EnumKey($Key,$i)
   
Loop
    If Not $EnumKeyAll[0] = ""
        For Each $strKey in $EnumKeyAll
            ;$strKey ?
            $ArrKeys = EnumKeyAll($strKey)
           
If Not $arrKeys[0] = ""
                For Each $strKey in $arrKeys
                    ReDim Preserve $EnumKeyAll[UBound($EnumKeyAll)+1]
                   
$EnumKeyAll[UBound($EnumKeyAll)] = $strKey
                Next
            EndIf
        Next
    EndIf
EndFunction

;UDF Section

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 484 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.135 seconds in which 0.071 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org