Jump to content

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

Share this post


Link to post
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

Share this post


Link to post
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 2019-10-24 - Version 1.4.14.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-11-30 - Version 1.4.0.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (NEW 2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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

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

  • Similar Content

    • By D2thunder
      Hi all, I wasnt active in this Forum forum for Years. 😅
      Since I need something with Json in AutoIt i write a little UDF to save a variable to file to read it with another language - and back. After a while until my other script grows i thought - lets integrate all variables wich is possible, because i want have arrays to.
      This is my first public UDF in Autoit - normaly my AutoIt scripts are quick an dirty.... - like the examples for this udf. - someone want to write nice examples?
      I tried to write this UDF properly, cared for processing speed, low mem consumption as possible, a short code, and a high usability without beeing very familiar with arrays.
      What is it for? - convert nearly all types of variables to a json string for saving it in a file or to an ini. Read it back in to a same formated variable and preserv variable types. All array types are supported with nested arrays, the only restriction is ram and array/stringsize - and time if the array is very big. You can use Filters for variable types and some other options.
      If anyone found a bug or any suggestions, please post it.
       
      get Json.au3 from here Json.au3
       
       
      JsonVar 2019.01.25.1.zip
    • By Ho3ein
      Hi, i have a source code of "Danny35d" and i have 2 main question that are:
       How to extract this function's results (Datas are as Array) to combo-box without set number of datas?  When i type 1, 2 in _SystemUsers($AccountType = 0) to get only Local or Domain users, it just give me both of them, it's old problem of this function, please share us debugged code :)❤ #include <Array.au3> $Users = _ArrayToString(_SystemUsers(), "|", 1) ConsoleWrite($Users & @CRLF) #cs =============================================================================== Function: _SystemUsers($AccountType = 0) Description: Return an array with the local or domain username Parameter(s): $AccountType: Local, domain or both username 0 = Local and Domain usernames 1 = Local usernames only 2 = Domain usernames only Returns: An array with the list of usernames - Succeeded @error 1 - Didn't query any username @error 2 - Failed to create Win32_SystemUsers object @error 3 - Invalid $AccountType Author(s): Danny35d #ce =============================================================================== Func _SystemUsers($AccountType = 0) Local $aSystemUsers Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20 Local $colItems = "", $strComputer = "localhost" If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '') $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) Then For $objItem In $colItems $Output = StringSplit($objItem.PartComponent, ',') If IsArray($Output) Then $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '') If $AccountType = 0 Or ($AccountType = 1 And @ComputerName = $Temp) Then $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|' ElseIf $AccountType = 2 And @ComputerName <> $Temp Then $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|' EndIf EndIf Next $aSystemUsers = StringTrimRight($aSystemUsers, 1) If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers)) Return(SetError(0, 0, StringSplit($aSystemUsers, '|'))) Else $aSystemUsers = '' Return(SetError(2, 2, $aSystemUsers)) EndIf EndFunc ;==>_SystemUsers Thanks to your best Team.
    • By sksbir
      Hi
      Trying this from autoit v3.3.14.5 and SCITE 3.19.102.1901.0 :
      help file , page "Language Reference - Variables" , sample autoit script about maps:

      Maps must be declared before use by defining their scope using the 'Global/Local/Static' keywords. Local $mControls[]
      So is my test script : only with this local declation.
      -check syntax is OK
      - running script : 

      test.au3" (13) : ==> Variable subscript badly formatted.: Local $mControls[] Local $mControls[^ ERROR ->14:51:49 AutoIt3.exe ended.rc:1
      any clue ?
    • By joseLB
      Hi
      This piece of code creates and reads OK a key at  "HKEY_LOCAL_MACHINE" and can be changed for a key at "HKEY_CURRENT_USER"
      $sta= RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor", "wav", "REG_SZ", "5555") MsgBox(4096,"wrote", $sta &@cr& @error) $zz= RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor", "wav") MsgBox(4096,"readed","="&$zz &@cr& @error) Exit With  HKEY_CURRENT_USER, in RegEdit we can see the created key, and we can create the key by hand/RegEdit and everything Works OK.
      At  HKEY_LOCAL_MACHINE we can´t see the created key above  thru RegEdit, but it Works (even not seeing, I can read). But  if I create "by hand"/RegEdit  the key,  it can´t read it with   $zz= RegRead  ("HKEY_LOCAL_MACHINE.... above.
      I´m the PC´s WIN.7 administrator. Even so I ran RegEdit as administrator and also the compiled AU3 and also plain. No changes.
      edit: even if Try   "HKEY_LOCAL_MACHINE\SOFTWARE\AAA", "wav", the same holds true.
      $sta= RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\AAA", "wav", "REG_SZ", "4444") MsgBox(4096,"wrote", $sta &@cr& @error) $zz= RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\AAA", "wav") MsgBox(4096,"readed","="&$zz &@cr& @error) Exit Seems that it creates this key at another place.... I can read the above value ("4444"), even after a boot, even the key not showing in regedit. And if I create it by hand key AAA/wav with a distinct value (666), t, it continues Reading the old value = 444.
      Thanks
      Jose
       
    • By Blitzkid
      Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
      to smth. like
       
      Here is my Code
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> $filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local $arr[3] = ["music", "picture", "video"] For $i = 0 To UBound($arr) - 1 Local $arrayfiles = _FileListToArray($filedir & $arr[$i], $arr[$i] & "*", 1) If @error = 1 Then ConsoleWrite($arr[$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite($arr[$i] & "Error 2") ;Exit EndIf $arrayfilter = _ArrayMax($arrayfiles, 0, 1) Global $stringfiles = StringReplace($arrayfilter, $arr[$i], "") ConsoleWrite($arrayfilter & @CRLF) Next EndFunc ;==>_checkfile  
×
×
  • Create New...