RegSaveKeyW function (winreg.h)

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

To specify the format for the saved key or hive, use the RegSaveKeyEx function.

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.

Syntax

LSTATUS RegSaveKeyW(
  [in]           HKEY                        hKey,
  [in]           LPCWSTR                     lpFile,
  [in, optional] const LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

Parameters

[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:

HKEY_CLASSES_ROOT
HKEY_CURRENT_USER

[in] lpFile

The name of the file in which the specified key and subkeys are to be saved. If the file already exists, the function fails.

If the string does not include a path, the file is created in the current directory of the calling process for a local key, or in the %systemroot%\system32 directory for a remote key. The new file has the archive attribute.

[in, optional] lpSecurityAttributes

A pointer to a SECURITY_ATTRIBUTES structure that specifies a security descriptor for the new file. If lpSecurityAttributes is NULL, the file gets a default security descriptor. The ACLs in a default security descriptor for a file are inherited from its parent directory.

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.

If the file already exists, the function fails with the ERROR_ALREADY_EXISTS error.

Remarks

If hKey represents a key on a remote computer, the path described by lpFile is relative to the remote computer.

The RegSaveKey function saves only nonvolatile keys. It does not save volatile keys. A key is made volatile or nonvolatile at its creation; see RegCreateKeyEx.

You can use the file created by RegSaveKey in subsequent calls to the RegLoadKey, RegReplaceKey, or RegRestoreKey functions. If RegSaveKey fails part way through its operation, the file will be corrupt and subsequent calls to RegLoadKey, RegReplaceKey, or RegRestoreKey for the file will fail.

Using RegSaveKey together with RegRestoreKey to copy subtrees in the registry is not recommended. This method does not trigger notifications and can invalidate handles used by other applications. Instead, use the SHCopyKey function or the RegCopyTree function.

The calling process must have the SE_BACKUP_NAME privilege enabled. For more information, see Running with Special Privileges.

Note

The winreg.h header defines RegSaveKey as an alias that 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 is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.

Requirements

Requirement Value
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

RegCreateKeyEx

RegDeleteKey

RegLoadKey

RegReplaceKey

RegRestoreKey

RegSaveKeyEx

Registry Files

Registry Functions

SECURITY_ATTRIBUTES