RegReplaceKeyA function (winreg.h)

Replaces the file backing a registry key and all its subkeys with another file, so that when the system is next started, the key and subkeys will have the values stored in the new file.

Applications that back up or restore system state including system files and registry hives should use the Volume Shadow Copy Service instead of the registry functions.


LSTATUS RegReplaceKeyA(
  [in]           HKEY   hKey,
  [in, optional] LPCSTR lpSubKey,
  [in]           LPCSTR lpNewFile,
  [in]           LPCSTR lpOldFile


[in] hKey

A handle to an open registry key. This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys:


[in, optional] lpSubKey

The name of the registry key whose subkeys and values are to be replaced. If the key exists, it must be a subkey of the key identified by the hKey parameter. If the subkey does not exist, it is created. This parameter can be NULL.

If the specified subkey is not the root of a hive, RegReplaceKey traverses up the hive tree structure until it encounters a hive root, then it replaces the contents of that hive with the contents of the data file specified by lpNewFile.

For more information, see Registry Element Size Limits.

[in] lpNewFile

The name of the file with the registry information. This file is typically created by using the RegSaveKey function.

[in] lpOldFile

The name of the file that receives a backup copy of the registry information being replaced.

Return value

If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.


There are two different registry hive file formats. Registry hives created on current operating systems typically cannot be loaded by earlier ones.

The file specified by the lpNewFile parameter remains open until the system is restarted.

If hKey is a handle returned by RegConnectRegistry, then the paths specified in lpNewFile and lpOldFile are relative to the remote computer.

The calling process must have the SE_RESTORE_NAME and SE_BACKUP_NAME privileges on the computer in which the registry resides. For more information, see Running with Special Privileges.


The winreg.h header defines RegReplaceKey as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.


Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header winreg.h (include Windows.h)
Library Advapi32.lib
DLL Advapi32.dll

See also






Registry Functions

Registry Overview