Jump to content

Enumerating the registry


Recommended Posts

Hi,

I'm new to Auto-it so please forgive my silly question.

I'm trying list all the valuenames in a registry key but I'm currenly specifying which item I want to be displayed using RegEnumKey

Func _RegCheck_ ($RegKey, $valuename)

If $valuename = "" Then

$valuename = RegEnumKey($RegKey, 1)

EndIf

EndFunc

But I would like to identify the value 2, 3 and etc.. recursively. Any clue?

Thanks

Link to comment
Share on other sites

Checkout the example in the help file for RegEnumKey.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Checkout the example in the help file for RegEnumKey.

Hi BigDod, first of all thanks for spending your time replying to my question. I actually looked at the example, but maybe I wasn't clear on what I'm trying to achieve. I'm trying to run the RegEnumKey recursively until there is "No more valid date available.". I believed I was going to get an error if RegEnumKey couldn't find any more valid data and @error would receive 1 or -1 but instead @error=0 and $valuename="No more data is available." I was thinking about using regular expression but didn't find the proper sintaxe yet. Below is the piece of crap. ops code that I've put together

Func _RegCheck_ ($RegKey, $valuename)

$i=0

If $valuename = "" Then

Do

$i=$i+1

$valuename = RegEnumKey($RegKey, $i)

$subkey=$RegKey & "\" &$valuename

Until "Regular expression comparison here"

EndIf

EndFunc

Link to comment
Share on other sites

Run this

For $i = 1 To 100
    $var = RegEnumKey("HKEY_LOCAL_MACHINE\HARDWARE", $i)
    If @error <> 0 Then
        MsgBox(0, "", "No more items to display")
        ExitLoop
    EndIf
    MsgBox(4096, "SubKey #" & $i & " under HKLM\Hardware: ", $var)
Next

As you can see there is only a few keys to show here and it tells you when it is finished.

Edit - On re-reading your last post I think that I still get it wrong, but at least it is a free bump.

Edited by BigDod


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Hi BigDod, first of all thanks for spending your time replying to my question. I actually looked at the example, but maybe I wasn't clear on what I'm trying to achieve. I'm trying to run the RegEnumKey recursively until there is "No more valid date available.". I believed I was going to get an error if RegEnumKey couldn't find any more valid data and @error would receive 1 or -1 but instead @error=0 and $valuename="No more data is available." I was thinking about using regular expression but didn't find the proper sintaxe yet. Below is the piece of crap. ops code that I've put together

Func _RegCheck_ ($RegKey, $valuename)
    
    $i=0
    If $valuename = "" Then
        Do
            $i=$i+1
            $valuename = RegEnumKey($RegKey, $i)
            $subkey=$RegKey & "\" &$valuename
            
                                 Until "Regular expression comparison here"
    EndIf

EndFunc
You might want to look at my _RegSearch() function, and see how it detects completion at each level of recursion:

$SearchKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion"
$SearchString = "Wall"

$Timer = TimerInit()
$Results = _RegSearch($SearchKey, $SearchString)
$Timer = TimerDiff($Timer) / 1000
MsgBox(64, "_RegSearch() Test", "Results of searching registry:" & @CRLF & _
        "In: " & $SearchKey & @CRLF & _
        "For: " & $SearchString & @CRLF & _
        "Time taken (in seconds): " & $Timer & @CRLF & _
        "---------------------------------------" & @CRLF & _
        $Results)

;*****************************************************
; Function _RegSearch($startkey, $searchval)
;   Performs a recursive search of the registry
;     Starting at $sStartKey, looking for $sSearchVal
; Returns a string containing a list of key names and values.
;   If a key name matches, it is listed as a reg path with trailing backslash:
;     i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\
;   If a value name matches, it is listed as a reg path without trailing backslash:
;     i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WallPaperDir
;   If the data matches, the format is path = data:
;       i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WallPaperDir = %SystemRoot%\Web\Wallpaper
;*****************************************************
Func _RegSearch($startkey, $searchval)
    Local $v, $val, $k, $key, $found = ""
    
    ; This loop checks values
    $v = 1
    While 1
        $val = RegEnumVal($startkey, $v)
        If @error = 0 Then
            ; Valid value - test it's name
            If StringInStr($val, $searchval) Then
                $found = $found & $startkey & "\" & $val & @LF
            EndIf
            ; test it's data
            $readval = RegRead($startkey, $val)
            If StringInStr($readval, $searchval) Then
                $found = $found & $startkey & "\" & $val & " = " & $readval & @LF
            EndIf
            $v += 1
        Else
            ; No more values here
            ExitLoop
        EndIf
    WEnd
    
    ; This loop checks subkeys
    $k = 1
    While 1
        $key = RegEnumKey($startkey, $k)
        If @error = 0 Then
            ; Valid key - test it's name
            If StringInStr($key, $searchval) Then
                $found = $found & $startkey & "\" & $key & "\" & @LF
            EndIf
            ; Now search it
            $found = $found & _RegSearch($startkey & "\" & $key, $searchval)
        Else
            ; No more keys here
            ExitLoop
        EndIf
        $k += 1
    WEnd
    
    ; Return results
    Return $found
EndFunc   ;==>_RegSearch

:P

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
Link to comment
Share on other sites

You might want to look at my _RegSearch() function, and see how it detects completion at each level of recursion:

$SearchKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion"
$SearchString = "Wall"

$Timer = TimerInit()
$Results = _RegSearch($SearchKey, $SearchString)
$Timer = TimerDiff($Timer) / 1000
MsgBox(64, "_RegSearch() Test", "Results of searching registry:" & @CRLF & _
        "In: " & $SearchKey & @CRLF & _
        "For: " & $SearchString & @CRLF & _
        "Time taken (in seconds): " & $Timer & @CRLF & _
        "---------------------------------------" & @CRLF & _
        $Results)

;*****************************************************
; Function _RegSearch($startkey, $searchval)
;   Performs a recursive search of the registry
;     Starting at $sStartKey, looking for $sSearchVal
; Returns a string containing a list of key names and values.
;   If a key name matches, it is listed as a reg path with trailing backslash:
;     i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\
;   If a value name matches, it is listed as a reg path without trailing backslash:
;     i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WallPaperDir
;   If the data matches, the format is path = data:
;       i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WallPaperDir = %SystemRoot%\Web\Wallpaper
;*****************************************************
Func _RegSearch($startkey, $searchval)
    Local $v, $val, $k, $key, $found = ""
    
    ; This loop checks values
    $v = 1
    While 1
        $val = RegEnumVal($startkey, $v)
        If @error = 0 Then
            ; Valid value - test it's name
            If StringInStr($val, $searchval) Then
                $found = $found & $startkey & "\" & $val & @LF
            EndIf
            ; test it's data
            $readval = RegRead($startkey, $val)
            If StringInStr($readval, $searchval) Then
                $found = $found & $startkey & "\" & $val & " = " & $readval & @LF
            EndIf
            $v += 1
        Else
            ; No more values here
            ExitLoop
        EndIf
    WEnd
    
    ; This loop checks subkeys
    $k = 1
    While 1
        $key = RegEnumKey($startkey, $k)
        If @error = 0 Then
            ; Valid key - test it's name
            If StringInStr($key, $searchval) Then
                $found = $found & $startkey & "\" & $key & "\" & @LF
            EndIf
            ; Now search it
            $found = $found & _RegSearch($startkey & "\" & $key, $searchval)
        Else
            ; No more keys here
            ExitLoop
        EndIf
        $k += 1
    WEnd
    
    ; Return results
    Return $found
EndFunc   ;==>_RegSearch

:P

Thanks a lot for your help. It really answer all my questions about it. Great work by the way.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...