Platform SDK: Registry

RegSetValueEx

The RegSetValueEx function sets the data and type of a specified value under a registry key.

LONG RegSetValueEx(
  HKEY hKey,           // handle to key
  LPCTSTR lpValueName, // value name
  DWORD Reserved,      // reserved
  DWORD dwType,        // value type
  CONST BYTE *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

lpValueName
[in] Pointer to a string containing the name of the value to set. If a value with this name is not already present in the key, the function adds it to the key.

If lpValueName is NULL or an empty string, "", the function sets the type and data for the key's unnamed or default value.

Windows 95/98: On Windows 95, the type of a key's default value is always REG_SZ, so the dwType parameter must specify REG_SZ for an unnamed value. On Windows 98, an unnamed value can be of any type.

Windows NT/2000: Registry keys do not have default values, but they can have one unnamed value, which can be of any type.

Reserved
Reserved; must be zero.
dwType
[in] Specifies a code indicating the type of data pointed to by the lpData parameter. For a list of the possible type codes, see Registry Value Types.
lpData
[in] Pointer to a buffer containing the data to be stored with the specified value name.

Windows 95: For string-based data types, such as REG_SZ, RegSetValueEx appends a NULL to the end of the value string.

Windows NT: For string-based data types, RegSetValueEx does not append a NULL to the end of the value string.

cbData
[in] Specifies the size, in bytes, of the information pointed to by the lpData parameter. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character.

Windows 95: The size includes the size of the NULL added to the end of the string passed in the lpData parameter.

Windows NT: The size is the exact size of the string passed in the lpData parameter.

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

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. Application elements such as icons, bitmaps, and executable files should be stored as files and not be placed in the registry.

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 dwType is the REG_SZ, REG_MULTI_SZ, or REG_EXPAND_SZ type and the ANSI version of this function is used (either by explicitly calling RegSetValueExA or by not defining UNICODE before including the Windows.h file), the data pointed to by 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: The maximum size of a single registry value is 16,300 bytes. This limit includes the size of the data (cbData) and the size of the value name (lpValueName).

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, RegSetValue