Jump to content

_RegReadToArray


 Share

Recommended Posts

good morning everybody.
today i liked to share an small example with you
which it an function to read the registry values as an array
the result array is 2d array witch
$a_array[n][0] = value name
$a_array[n][1] = value Data
$a_array[0][0] = values count

here's the function

#include <Array.au3>
#include <WinAPIReg.au3>
#include <APIRegConstants.au3>
    Local $a_array = _RegReadToArray("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
If @error Then
    MsgBox(16, "error", @error)
    Exit
EndIf
_ArrayDisplay($a_array)
Func _RegReadToArray($s_RegKey)
    Local $a_KeySplitInfo = StringSplit($s_RegKey, "\\", 2)
    If UBound($a_KeySplitInfo) <= 1 Then
        $a_KeySplitInfo = StringSplit($s_RegKey, "\", 2)
        If UBound($a_KeySplitInfo) <= 1 Then Return (1, 1, 0)
    EndIf
    Local $H_KeyInfo = "", $s_RegKeyInfo = ""
    Switch $a_KeySplitInfo[0]
        Case "hklm", "HKEY_LOCAL_MACHINE", "hklm64", "HKEY_LOCAL_MACHINE64"
            $H_KeyInfo = $HKEY_LOCAL_MACHINE
        Case "hkCu", "HKEY_CURRENT_USER", "hkCU64", "HKEY_CURRENT_USER64"
            $H_KeyInfo = $HKEY_CURRENT_USER
        Case "hkCr", "HKEY_CLASSES_ROOT", "HKCR64", "HKEY_CLASSES_ROOT64"
            $H_KeyInfo = $HKEY_CLASSES_ROOT
        Case "HKU", "HKEY_USERS", "HKU64", "HKEY_USERS64"
            $H_KeyInfo = $HKEY_USERS
        Case Else
            Return SetError(2, 2, 0)
    EndSwitch
    _ArrayDelete($a_KeySplitInfo, 0)
    $s_RegKeyInfo = _ArrayToString($a_KeySplitInfo, "\")
    Local $H_KeyInfoOpen = _WinAPI_RegOpenKey($H_KeyInfo, $s_RegKeyInfo, $KEY_READ)
    Local $A_KeyInfo = _WinAPI_RegQueryInfoKey($H_KeyInfoOpen)
    If @error Then Return SetError(1, 1, 0)
    _WinAPI_RegCloseKey($H_KeyInfoOpen)
    Local $A_RegVal[$A_KeyInfo[2] + 1][2]
    Local $iV = 1, $s_RegRead = ""
    While 1
        $s_RegVal = RegEnumVal($s_RegKey, $iV)
        If @error <> 0 Then ExitLoop
        $s_RegRead = RegRead($s_RegKey, $s_RegVal)
        If Not (@error) Then
            $A_RegVal[$iV][0] = $s_RegVal
            $A_RegVal[$iV][1] = $s_RegRead
        EndIf
        $iV += 1
    WEnd
    $A_RegVal[0][0] = UBound($A_RegVal) - 1
    If $A_RegVal[0][0] >= 1 Then
        Return $A_RegVal
    Else
        Return SetError(3, 3, 0)
    EndIf
EndFunc   ;==>_RegReadToArray


i hope you benefit from it

with my greetings

Edited by nacerbaaziz
updated function and changed some codes and tidied the function
Link to comment
Share on other sites

@Polymath

Thank you! This is nice!

I guess you're using msgbox for demonstrative purposes. Easier to see output though when maybe just add #include <Array.au3> at the top

and instead of the msgBox to display each value one at a time, just use _ArrayDisplay($a_array) (outside of the loop)

(you probably already knew that) Thanks again though!

Edited by coffeeturtle
Link to comment
Share on other sites

My 2 cents worth :)

  • Do not unse Local in a Loop
  • Do not use ReDim for every entry - Slooows down your script. Create/enlarge the Array in steps of 100 and set the real size when all registry keys have been processed.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

7 hours ago, coffeeturtle said:

@Polymath

Thank you! This is nice!

I guess you're using msgbox for demonstrative purposes. Easier to see output though when maybe just add #include <Array.au3> at the top

and instead of the msgBox to display each value one at a time, just use _ArrayDisplay($a_array) (outside of the loop)

(you probably already knew that) Thanks again though!

 

thank you sir to your interesting

i added what you told me

Link to comment
Share on other sites

6 hours ago, water said:

My 2 cents worth :)

  • Do not unse Local in a Loop
  • Do not use ReDim for every entry - Slooows down your script. Create/enlarge the Array in steps of 100 and set the real size when all registry keys have been processed.

good morning sir and thank you to your interesting, i

Completelyy

changed the function

i hope that you like it

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

×
×
  • Create New...