Sign in to follow this  
Followers 0
FreeRider

RegEnumKey

5 posts in this topic

Hi all,

I'm looking for a function which could perform RegEnumKey in recurse mode (all sub levels must me catched for all keys of the desired registry tree).

I made some code but it only works for the first level and thenstops... The final goal is to save a complete tree when needed to able to restore it if necessary

Does anybody already produced such code ?.... Any idea is welcome...

Thanks in advance.

Just to help you will find below the code I already developed

#include <Array.au3>

Dim $GlobalKeyList

$RegTree="HKCU\Software\ODBC\ODBC.INI" ; Registry Tree to look in

$ErrMsg=""

$GlobalKeyList=""

$KeyToTest="" ; Variable containing the list of Reg keys which contains subkeys

$RootKeyList=Call ("ListKeys",$RegTree) ; This is the root key list (directly under the $RegTree)

$RootKeyList=StringSplit($RootKeyList,@LF) ; Put the result into an array

_ArrayDisplay(StringSplit(StringStripWS($KeyToTest,2),@LF),"Initial Key List...") ; This is just for control...

$KeyToTest=StringSplit(StringStripWS($KeyToTest,2),@LF)

If not IsArray($KeyToTest) Or $KeyToTest[1]="ERROR" Then

MsgBox(0,"Error !!!",$ErrMsg)

Exit

EndIf

Call ("TestSubKey",$KeyToTest)

Func TestSubKey ($KeyList)

$KeyToTest="" ; Keylist must be re-initialized to avoid stacking the new list on the previous one

For $i=1 To $KeyList[0]

Call ("ListKeys",$KeyList[$i])

Next

$KeyToTest=StringSplit(StringStripWS($KeyToTest,2),@LF)

_ArrayDisplay($KeyToTest,"List of Keys to be tested...") ; This is just for control

EndFunc

_ArrayDisplay(StringSplit(StringStripWS($GlobalKeyList,2),@LF),"Global Keys list...") ; This is just for control

Func ListKeys($Tree)

Local $KeyInstance,$KeyName

$KeyInstance=1

$Output=""

While 1 ; Loop Start

$KeyName=RegEnumKey($Tree,$KeyInstance)

If @error <> 0 Then

If @error = 1 Then $ErrMsg="Unable to open the requested key."

If @error = 2 Then $ErrMsg="Unable to open the requested main key."

If @error = 3 Then $ErrMsg ="Unable to connect to remote registry."

If @error = -1 And StringInStr($KeyName,"No more data is available",0,1) Then ; Last key read

$GlobalKeyList=$GlobalKeyList & $Output ; Updates the global key list

Return StringStripWS($KeyToTest,2) ; returns the list of keys to be tested

EndIf

Return "ERROR"

ExitLoop ; Exits loop if key cannot be read.

Else

$Output=$Output & $Tree & "\" & $KeyName & @LF

If RegEnumKey($Tree & "\" & $KeyName,1) = "No more data is available" Then

ContinueLoop

Else

$KeyToTest=$KeyToTest & $Tree & "\" & $KeyName & @LF

EndIf

EndIf

$KeyInstance=$KeyInstance+1

WEnd

EndFunc


FreeRiderHonour & Fidelity

Share this post


Link to post
Share on other sites



There are many. This is one I wrote a long while ago: _RegSearch().

Its workings are very simple, so you should be able to adapt it easily to what you need.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks my friend... I'm sure it will work...


FreeRiderHonour & Fidelity

Share this post


Link to post
Share on other sites

Hi PsaltyDS,

Here is your _RegSearch function adapted to what I need it to do... and it works perfectly... Many thanks again for your help

$SearchKey = "HKCU\Software\ODBC\ODBC.INI"

$Results = _RegListKeys($SearchKey)

MsgBox(64, "_RegListKeys", "Results of searching registry Keys" & @CRLF & _

"In: " & $SearchKey & @CRLF & _

"---------------------------------------" & @CRLF & _

$Results)

;*****************************************************

; Function _RegListKeys($startkey,$recursive=0)

; Performs a nonrecursive (by default) or recursive search of the registry Starting at $sStartKey

; Returns a string containing a list of key names

; If a key name matches, it is listed as a reg path without trailing backslash:

; i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion

;*****************************************************

Func _RegListKeys($startkey,$recurse=0)

Local $v, $val, $k, $key, $found = ""

$k = 1

While 1 ; This loop checks subkeys

$key = RegEnumKey($startkey, $k)

If @error = 0 Then

$found = $found & @LF & $startkey & "\" & $key ; Valid key - tests it's name and stores it into the Key List

;======= RECURSE CHOICE START =====

If $recurse=0 Then ; Now search it (Recurse mode is 'off')

$k += 1

ContinueLoop

Else ; Now search it (Recurse mode is 'On')

$found = $found & _RegListKeys($startkey & "\" & $key,$recurse)

EndIf

;======= RECURSE CHOICE END =======

Else ; No more keys here

ExitLoop

EndIf

$k += 1

WEnd

Return $found ; Return results

EndFunc ;==>_RegSearch


FreeRiderHonour & Fidelity

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0