Function Reference


_WinAPI_RegSaveKey

Saves the specified key and all of its subkeys and values to a new file, in the standard format

#include <WinAPIReg.au3>
_WinAPI_RegSaveKey ( $hKey, $sFilePath [, $bReplace = False [, $tSecurity = 0]] )

Parameters

$hKey Handle to an open registry key.
$sFilePath The name of the file in which the specified key and subkeys are to be saved.
$bReplace [optional] Specifies whether to replace the file if it already exists, valid values:
    True - The function attempts to replace the existing file.
    False - The function fails if the file already exists (Default).
$tSecurity [optional] $tagSECURITY_ATTRIBUTES structure that specifies a security descriptor for the new file.
If this parameter is 0 (Default), the file gets a default security descriptor.

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_BACKUP_NAME privilege, otherwise, the function fails, and _WinAPI_GetLastError() returns ERROR_PRIVILEGE_NOT_HELD (1314).

You can use the file created by this function in subsequent calls to the _WinAPI_RegRestoreKey() functions.

Related

_WinAPI_RegRestoreKey

See Also

Search RegSaveKey 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