Function Reference


_WinAPI_RegRestoreKey

Reads the registry information in a specified file and copies it over the specified key

#include <WinAPIReg.au3>
_WinAPI_RegRestoreKey ( $hKey, $sFilePath )

Parameters

$hKey Handle to an open registry key. This handle is returned by the _WinAPI_RegCreateKey() or _WinAPI_RegOpenKey() function.
It can also be one of the following predefined keys:
    $HKEY_CLASSES_ROOT
    $HKEY_CURRENT_CONFIG
    $HKEY_CURRENT_USER
    $HKEY_LOCAL_MACHINE
    $HKEY_USERS
$sFilePath The name of the file with the registry information.
This file is typically created by using the _WinAPI_RegSaveKey() function.

Return Value

Success: 1.
Failure: 0 and sets the @error flag to non-zero, @extended flag may contain the system error code.

Remarks

The calling process must have $SE_RESTORE_NAME privilege,
otherwise the function fails, and _WinAPI_GetLastError() returns ERROR_PRIVILEGE_NOT_HELD (1314).

If any subkeys of the specified registry key are open, the _WinAPI_RegRestoreKey() fails.

Related

_WinAPI_RegCreateKey, _WinAPI_RegOpenKey, _WinAPI_RegSaveKey

See Also

Search RegRestoreKey in MSDN Library.

Example

#include <APIRegConstants.au3>
#include <Debug.au3>
#include <WinAPIError.au3>
#include <WinAPIHObj.au3>
#include <WinAPIProc.au3>
#include <WinAPIReg.au3>

#RequireAdmin

_DebugSetup(Default, True)

Example()

Func Example()
        Local $aPrivileges[2] = [$SE_BACKUP_NAME, $SE_RESTORE_NAME]

        ; Enable "SeBackupPrivilege" and "SeRestorePrivilege" privileges to save and restore registry hive
        Local $hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))
        Local $aAdjust
        _WinAPI_AdjustTokenPrivileges($hToken, $aPrivileges, $SE_PRIVILEGE_ENABLED, $aAdjust)
        If @error Or @extended Then
                _DebugReport('Error' & @TAB & 'You do not have the required privileges.' & @CRLF)
                Exit
        EndIf

        ; Save "HKEY_CURRENT_USER\Software\AutoIt v3" to reg.dat
        Local $hKey = _WinAPI_RegOpenKey($HKEY_CURRENT_USER, 'Software\AutoIt v3', $KEY_READ)
        If _WinAPI_RegSaveKey($hKey, @TempDir & '\reg.dat', 1) Then
                _DebugReport('- "HKEY_CURRENT_USER\Software\AutoIt v3" has been saved to reg.dat.' & @CRLF)
        Else
                _DebugReport("! RegSaveKey @error =" & @error & @TAB & _WinAPI_GetErrorMessage(@extended) & @CRLF)
        EndIf
        _WinAPI_RegCloseKey($hKey)

        ; Restore "HKEY_CURRENT_USER\Software\AutoIt v3" to "HKEY_CURRENT_USER\Software\AutoIt v3 (Duplicate)"
        $hKey = _WinAPI_RegCreateKey($HKEY_CURRENT_USER, 'Software\AutoIt v3 (Duplicate)', $KEY_WRITE)
        If _WinAPI_RegRestoreKey($hKey, @TempDir & '\reg.dat') Then
                _DebugReport('- "HKEY_CURRENT_USER\Software\AutoIt v3" has been restored to "HKEY_CURRENT_USER\Software\AutoIt v3 (Duplicate)".' & @CRLF)
        Else
                _DebugReport("! RegRestoreKey @error =" & @error & @TAB & _WinAPI_GetErrorMessage(@extended) & @CRLF)
        EndIf
        _WinAPI_RegCloseKey($hKey)

        ; Restore "SeBackupPrivilege" and "SeRestorePrivilege" privileges by default
        _WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
        _WinAPI_CloseHandle($hToken)

        FileDelete(@TempDir & '\reg.dat')

EndFunc   ;==>Example