Platform SDK: Registry

RegSetValue

The RegSetValue function sets the data for the default or unnamed value of a specified registry key. The data must be a text string.

Note  This function is provided only for compatibility with 16-bit versions of Windows. Win32-based applications should use the RegSetValueEx function.

LONG RegSetValue(
  HKEY hKey,         // handle to key
  LPCTSTR lpSubKey,  // subkey name
  DWORD dwType,      // information type
  LPCTSTR lpData,    // value data
  DWORD cbData       // size of value data
);

Parameters

hKey
[in] Handle to a currently open key or any of the following predefined reserved handle values:

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
Windows NT/2000:
HKEY_PERFORMANCE_DATA
Windows 95/98: HKEY_DYN_DATA

lpSubKey
[in] Pointer to a null-terminated string containing the name of a subkey of the hKey parameter. The function sets the default value of the specified subkey. If this parameter is NULL or points to an empty string, the function sets the default value of the key identified by hKey.
dwType
[in] Specifies the type of information to be stored. This parameter must be the REG_SZ type. To store other data types, use the RegSetValueEx function.
lpData
[in] Pointer to a null-terminated string containing the data to set for the default value of the specified key.
cbData
[in] Specifies the length, in bytes, of the string pointed to by the lpData parameter, not including the terminating null character.

Return Values

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.

Remarks

If the key specified by the lpSubKey parameter does not exist, the RegSetValue function creates it.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the registry. This helps the registry perform efficiently.

The key identified by the hKey parameter must have been opened with KEY_SET_VALUE access. To open the key, use the RegCreateKeyEx or RegOpenKeyEx function. If the ANSI version of this function is used (either by explicitly calling RegSetValueA or by not defining UNICODE before including the Windows.h file), the lpData parameter must be an ANSI character string. The string is converted to Unicode before it is stored in the registry.

Windows 95/98: No registry subkey or value name can exceed 255 characters.

Windows 95/98: The maximum size of a single registry value is 16,300 bytes. This limit includes the size of the data (cbData).

Windows 95/98: There is a 64K limit for the total size of all values of a key.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Winreg.h; include Windows.h.
  Library: Use Advapi32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.

See Also

Registry Overview, Registry Functions, RegCreateKeyEx, RegFlushKey, RegOpenKeyEx, RegQueryValue, RegQueryValueEx, RegSetValueEx