[Func] Quelques fonctions pour le registre.

Partagez des fonctions et des UDF AutoIt.
Règles du forum
.
Répondre
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

[Func] Quelques fonctions pour le registre.

#1

Message par Tlem »

Voici une première fonction : _RegClone() par Alex Peters.
Cette fonction permet de cloner une clé de registre entière.

Code : Tout sélectionner

;
; _regClone()
; Clones an entire registry key to another location.
;
; Requirements:
;   AutoIt v3.1.1.56 or later
;
; Syntax:
;   _regClone($srcKey, $tgtKey)
;
; Parameters:
;   *  $srcKey: location of key to clone.
;   *  $tgtKey: location of cloned key.
;
; Examples:
;   *  _regClone("HKLM\Software\AutoIt v3", "HKLM\Software\AutoIt v3 Backup")
;   *  _regClone("HKCU\Software\Adobe", "HKCU\Software\AdobeBackup")
;
; @error return values:
;   *  0: success
;   *  1: could not write to target sub/key
;   *  2: could not read from source sub/key
;
; Author:
;   Alex Peters, 9/7/2005
;
; Notes:
;   *  The _regClone() function calls itself recursively to clone subkeys. If
;      renaming the function then be sure to update the function's code to
;      reflect this name change.
; ____________________________________________________________________________

func _regClone($srcKey, $tgtKey)

    ; Create target key (necessary if source key is empty).
    if (regWrite($tgtKey) = 0) then
        setError(1)
        return
    endIf

    ; Enumerate source key's values and write them to the target key.
    local $valIdx = 1
    while (1)
        local $valName = regEnumVal($srcKey, $valIdx)
        ; There are no more values if @error = -1.
        if (@error = -1) then exitLoop
        ; The source key could not be read if @error = 1.
        if (@error = 1) then
            setError(2)
            return
        endIf
        local $valData = regRead($srcKey, $valName)
        local $valType
        select
            case @extended = 1
                $valType = "REG_SZ"
            case @extended = 2
                $valType = "REG_EXPAND_SZ"
            case @extended = 3
                $valType = "REG_BINARY"
            case @extended = 4
                $valType = "REG_DWORD"
            case @extended = 7
                $valType = "REG_MULTI_SZ"
        endSelect
        if (regWrite($tgtKey, $valName, $valType, $valData) = 0) then
            setError(1)
            return
        endIf
        $valIdx = $valIdx + 1
    wEnd

    ; Enumerate source key's subkeys and write them to the target key.
    local $subkeyIdx = 1
    while (1)
        local $subkey = "\" & regEnumKey($srcKey, $subkeyIdx)
        ; @error = -1 if there are no more subkeys.
        if (@error = -1) then exitLoop
        ; @error = 1 if the key could not be read.
        if (@error = 1) then
            setError(2)
            return
        endIf
        _regClone($srcKey & $subkey, $tgtKey & $subkey)
        ; Propagate any error back to the caller.
        if (@error) then
            setError(@error)
            return
        endIf
        $subkeyIdx = $subkeyIdx + 1
    wEnd

endFunc
Thierry

Rechercher sur le forum ----- Les règles du forum
Le "ça ne marche pas" est une conséquence commune découlant de beaucoup trop de raisons potentielles ...

Une idée ne peut pas appartenir à quelqu'un. (Albert Jacquard) tiré du documentaire "Copié n'est pas volé".
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

Re: [Func] Quelques fonctions pour le registre.

#2

Message par Tlem »

Voici une nouvelle fonction : _RegValMove() par Tlem :D
Cette fonction permet de renommer/déplacer une valeur de clé de registre.

Code : Tout sélectionner

;===============================================================================
;
; Function Name:    _RegValMove()
; Description:      Move or rename a reg key
; Parameter(s):     $SrcKey        - The source Reg key
;                   $SrcValName    - The source key Name
;                   $DstValName    - The destination key Name
;                   $DstKey        - The destination Reg key (optional for moving key)
; Requirement(s):   
; Return Value(s):  On Success - Return 1
;                   On Failure - Return 0
; Author(s):        Tlem
;
; Example :         _RegValMove("HKLM\SOFTWARE\_Test", "Test1", "Test2", "")
;===============================================================================

Func _RegValMove($SrcKey, $SrcValName, $DstValName, $DstKey = "")
    Local $Value, $Type
    
    If $DstKey = "" Then $DstKey = $SrcKey

    $Value = RegRead($SrcKey, $SrcValName)
    If @error Then
        SetError(1)
        Return 0
    EndIf

    Select
        Case @extended = 1
            $Type = "REG_SZ"
        Case @extended = 2
            $Type = "REG_EXPAND_SZ"
        Case @extended = 3
            $Type = "REG_BINARY"
        Case @extended = 4
            $Type = "REG_DWORD"
        Case @extended = 7
            $Type = "REG_MULTI_SZ"
    EndSelect
    
    If Not RegWrite($DstKey, $DstValName, $Type, $Value) Then
        SetError(1)
        Return 0
    EndIf

    If Not RegDelete($SrcKey, $SrcValName) Then
        SetError(1)
        Return 0
    Else
        Return 1
    EndIf
EndFunc
Thierry

Rechercher sur le forum ----- Les règles du forum
Le "ça ne marche pas" est une conséquence commune découlant de beaucoup trop de raisons potentielles ...

Une idée ne peut pas appartenir à quelqu'un. (Albert Jacquard) tiré du documentaire "Copié n'est pas volé".
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

Re: [Func] Quelques fonctions pour le registre.

#3

Message par Tlem »

Voici une nouvelle fonction : _RegRenameKey() par zorphnog.
Cette fonction permet de renommer/déplacer une clé de registre.

Code : Tout sélectionner

; http://www.autoitscript.com/forum/topic/108126-registry-key-rename/page__view__findpost__p__762365

Global Const _      ; Predefined Keys
    $HKEY_CLASSES_ROOT  = 0x80000000, _
    $HKEY_CURRENT_USER  = 0x80000001, _
    $HKEY_LOCAL_MACHINE = 0x80000002, _
    $HKEY_USERS         = 0x80000003
Global Const _      ; Registry Key Security and Access Rights
    $KEY_ALL_ACCESS         = 0xF003F, _
    $KEY_CREATE_LINK        = 0x0020, _
    $KEY_CREATE_SUB_KEY     = 0x0004, _
    $KEY_ENUMERATE_SUB_KEYS = 0x0008, _
    $KEY_EXECUTE            = 0x20019, _
    $KEY_NOTIFY             = 0x0010, _
    $KEY_QUERY_VALUE        = 0x0001, _
    $KEY_READ               = 0x20019, _
    $KEY_SET_VALUE          = 0x0002, _
    $KEY_WOW64_32KEY        = 0x0200, _
    $KEY_WOW64_64KEY        = 0x0100, _
    $KEY_WRITE              = 0x20006
Global Const _       ; Registry creation options
    $REG_OPTION_NON_VOLATILE   = 0x00000000, _
    $REG_OPTION_VOLATILE       = 0x00000001, _
    $REG_OPTION_CREATE_LINK    = 0x00000002, _
    $REG_OPTION_BACKUP_RESTORE = 0x00000004

;_RegRenameKey("HKLM\SOFTWARE", "TortoiseOverlays1", "TortoiseOverlays")

Func _RegRenameKey($sKey, $sSubKeyOld, $sSubKeyNew)
    Local $sMainKey, $sMainSubKey, $hKeySrc, $hKeyDest

    ; Check for subkey existance
    RegRead($sKey & "\" & $sSubKeyOld, "")
    If @error = 1 Then
        ConsoleWrite(StringFormat("-> ERROR key does not exist [%s\\%s]\r\n", $sKey, $sSubKeyOld))
        Return SetError(-1, 0, -1)
    EndIf

    ; Parse key for system key and subkey
    $aResult = StringRegExp($sKey, "(?iU)(.+)(?:\\(.*\Z)|\Z)", 3)
    If Not @error Then
        Switch UBound($aResult)
            Case 1
                $sMainKey = $aResult[0]
                $sMainSubKey = ""
            Case 2
                $sMainKey = $aResult[0]
                $sMainSubKey = $aResult[1]
        EndSwitch
    Else
        Return SetError(1, 0, -1)
    EndIf

    ; Open main subkey
    $hKeySrc = __RegOpenKeyEx($sMainKey, $sMainSubKey)
    If @error Then
        ConsoleWrite(StringFormat("-> ERROR opening key [%s\\%s]\r\nError: %d\tExtended: %d\r\n", $sMainKey, $sMainSubKey, @error, @extended))
        Return SetError(2, 0, -1)
    EndIf

    ; Create new subkey
    $hKeyDest = __RegCreateKeyEx($hKeySrc, $sSubKeyNew)
    If @error Then
        ConsoleWrite(StringFormat("-> ERROR creating key [%s\\%s]\r\nError: %d\tExtended: %d\r\n", $sKey, $sSubKeyNew, @error, @extended))
        __RegCloseKey($hKeySrc)
        Return SetError(3, 0, -1)
    EndIf

    ; Copy tree from old subkey to new
    __SHCopyKey($hKeySrc, $sSubKeyOld, $hKeyDest)
    If @error Then
        ConsoleWrite(StringFormat("-> ERROR copying tree [%s\\%s] to [%s\\%s]\r\nError: %d\tExtended: %d\r\n", $sKey, $sSubKeyOld, $sKey, $sSubKeyNew, @error, @extended))
        __RegCloseKey($hKeySrc)
        __RegCloseKey($hKeyDest)
        Return SetError(4, 0, -1)
    EndIf
    __RegCloseKey($hKeyDest)

    ; Delete old subkey
    __SHDeleteKey($hKeySrc, $sSubKeyOld)
    If @error Then
        ConsoleWrite(StringFormat("-> ERROR deleting tree [%s\\%s]\r\nError: %d\tExtended: %d\r\n", $sKey, $sSubKeyOld, @error, @extended))
        __RegCloseKey($hKeySrc)
        Return SetError(5, 0, -1)
    EndIf
    __RegCloseKey($hKeySrc)
EndFunc  ;==>_RegRenameKey

Func __GetHKey($hKey)
    If IsString($hKey) Then
        Switch StringUpper($hKey)
            Case "HKCR", "HKEY_CLASSES_ROOT"
                Return $HKEY_CLASSES_ROOT
            Case "HKCU", "HKEY_CURRENT_USER"
                Return $HKEY_CURRENT_USER
            Case "HKLM", "HKEY_LOCAL_MACHINE"
                Return $HKEY_LOCAL_MACHINE
            Case "HKU", "HKEY_USERS"
                Return $HKEY_USERS
            Case Else
                Return SetError(1, 0, -1)
        EndSwitch
    EndIf
    Return $hKey
EndFunc  ;==>__GetHKey

Func __RegCloseKey($hKey)
    Local $aResult = DllCall("advapi32.dll", "int", "RegCloseKey", "int", $hKey)
    If @error Then Return SetError(1, @error, -1)
    If $aResult[0] <> 0 Then Return SetError(2, $aResult[0], -1)
EndFunc  ;==>__RegCloseKey

Func __RegCreateKeyEx($hKey, $sSubKey, $b64 = False)
    Local $tDATA, $iSAM, $aResult, $hkResult
    $hKey = __GetHKey($hKey)
    If @error Then Return SetError(1, 0, -1)  ; Invalid Key
    $iSAM = $KEY_WOW64_32KEY
    If $b64 Then $iSAM = $KEY_WOW64_64KEY
    $tDATA = DllStructCreate("int Result;dword Disposition;wchar SubKey[255]")
    DllStructSetData($tDATA, "SubKey", $sSubKey)
    $aResult = DllCall("advapi32.dll", "int", "RegCreateKeyExW", _
        "int", $hKey, _
        "ptr", DllStructGetPtr($tDATA, "SubKey"), _
        "dword", 0, _
        "ptr", 0, _
        "dword", $REG_OPTION_NON_VOLATILE, _
        "int", BitOR($KEY_ALL_ACCESS, $iSAM), _
        "ptr", 0, _
        "ptr", DllStructGetPtr($tDATA, "Result"), _
        "ptr", DllStructGetPtr($tDATA, "Disposition"))
    If @error Then
        $tDATA = 0
        Return SetError(2, @error, -1)  ; Error using DllCall
    EndIf
    $hkResult = DllStructGetData($tDATA, "Result")
    $tDATA = 0
    If $aResult[0] <> 0 Then Return SetError(3, $aResult[0], $hkResult)  ; Error using RegCreateKeyEx
    Return $hkResult
EndFunc  ;==>__RegCreateKeyEx

Func __RegOpenKeyEx($hKey, $sSubKey = "", $b64 = False)
    Local $tDATA, $aResult, $hkResult
    $hKey = __GetHKey($hKey)
    If @error Then Return SetError(1, 0, -1)  ; Invalid Key
    $iSAM = $KEY_WOW64_32KEY
    If $b64 Then $iSAM = $KEY_WOW64_64KEY
    $tDATA = DllStructCreate("int Result;wchar SubKey[255]")
    DllStructSetData($tDATA, "SubKey", $sSubKey)
    $aResult = DllCall("advapi32.dll", "int", "RegOpenKeyExW", _
        "int", $hKey, _
        "ptr", DllStructGetPtr($tDATA, "SubKey"), _
        "dword", 0, _
        "int", BitOR($KEY_ALL_ACCESS, $iSAM), _
        "ptr", DllStructGetPtr($tDATA, "Result"))
    If @error Then
        $tDATA = 0
        Return SetError(2, @error, -1)  ; Error using DllCall
    EndIf
    $hkResult = DllStructGetData($tDATA, "Result")
    $tDATA = 0
    If $aResult[0] <> 0 Then Return SetError(3, $aResult[0], $hkResult)  ; Error using RegOpenKeyEx
    Return $hkResult
EndFunc  ;==>__RegOpenKeyEx

Func __SHCopyKey($hKey, $sSubKey, $hKeyNew)
    Local $tDATA, $aResult
    $hKey = __GetHKey($hKey)
    If @error Then Return SetError(1, 0, -1)  ; Invalid source key
    $hKeyNew = __GetHKey($hKeyNew)
    If @error Then Return SetError(2, 0, -1)  ; Invalid destination key
    $tDATA = DllStructCreate("wchar SubKey[255]")
    DllStructSetData($tDATA, "SubKey", $sSubKey)
    $aResult = DllCall("shlwapi.dll", "int", "SHCopyKeyW", _
        "int", $hKey, _
        "ptr", DllStructGetPtr($tDATA, "SubKey"), _
        "int", $hKeyNew, _
        "dword", 0)
    If @error Then
        $tDATA = 0
        Return SetError(3, @error, -1)  ; Error using DllCall
    EndIf
    $tDATA = 0
    If $aResult[0] <> 0 Then Return SetError(4, $aResult[0], -1)  ; Error using SHCopyKey
EndFunc  ;==>__SHCopyKey

Func __SHDeleteKey($hKey, $sSubKey)
    Local $tDATA, $aResult
    $hKey = __GetHKey($hKey)
    If @error Then Return SetError(1, 0, -1)  ; Invalid source key
    $tDATA = DllStructCreate("wchar SubKey[255]")
    DllStructSetData($tDATA, "SubKey", $sSubKey)
    $aResult = DllCall("shlwapi.dll", "int", "SHDeleteKeyW", _
        "int", $hKey, _
        "ptr", DllStructGetPtr($tDATA, "SubKey"))
    If @error Then
        $tDATA = 0
        Return SetError(2, @error, -1)  ; Error using DllCall
    EndIf
    $tDATA = 0
    If $aResult[0] <> 0 Then Return SetError(3, $aResult[0], -1)  ; Error using SHDeleteKey
EndFunc  ;==>__SHDeleteKey
Thierry

Rechercher sur le forum ----- Les règles du forum
Le "ça ne marche pas" est une conséquence commune découlant de beaucoup trop de raisons potentielles ...

Une idée ne peut pas appartenir à quelqu'un. (Albert Jacquard) tiré du documentaire "Copié n'est pas volé".
Répondre