RegSetValueEx Function

The RegSetValueEx function creates or opens a specified registry key and sets the data for a specified registry value. If the key or value does not exist, the function creates it. If the key exists and the value exists, the function overwrites the existing value and closes the handle.

Syntax


LONG RegSetValueEx(
  HKEY  hKey,
  LPCWSTR lpValueName,
  DWORD  Reserved,
  DWORD  Type,
  const BYTE *lpData,
  DWORD  cbData
);
                

Parameters

Parameter Description
hKey A handle to an open registry key. The handle is obtained by calling RegCreateKeyEx or RegOpenKeyEx. It can also be one of the predefined keys: HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, or HKEY_USERS.
lpValueName A pointer to a null-terminated string that contains the name of the registry value to set. If this parameter is NULL or an empty string, the function sets the type and data of the key's default value.
Reserved This parameter is reserved and must be zero.
Type A value that specifies the type of the data pointed to by the lpData parameter. This can be one of the registry data types:
  • REG_BINARY
  • REG_DWORD
  • REG_EXPAND_SZ
  • REG_LINK
  • REG_MULTI_SZ
  • REG_NONE
  • REG_QWORD
  • REG_SZ
lpData A pointer to a buffer that contains the data to be stored in the given registry value. For string types, the string must be null-terminated.
cbData The size, in bytes, of the data pointed to by the lpData parameter. For string types, this is the size of the buffer including the null terminator.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a non-zero error code defined in WinError.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to retrieve a system-defined message that describes the error.

Remarks

The RegSetValueEx function can be used to set a wide variety of data types. The Type parameter specifies the format of the data.

For string data types (REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ), the buffer pointed to by lpData must include the null terminator. If the data is an empty string, the buffer should contain only the null terminator (size 1).

Note: Applications that write to HKEY_LOCAL_MACHINE or HKEY_CLASSES_ROOT require administrator privileges.

Example

The following example demonstrates how to set a string value:


#include <windows.h>
#include <iostream>

int main() {
    HKEY hKey;
    LONG lResult;
    LPCWSTR valueName = L"MySetting";
    LPCWSTR data = L"This is my custom setting.";
    DWORD dataSize = (wcslen(data) + 1) * sizeof(WCHAR); // +1 for null terminator

    // Open or create the key
    lResult = RegCreateKeyExW(
        HKEY_CURRENT_USER, // Handle to an open key
        L"Software\\MyApp\\Settings", // Subkey name
        0, // Reserved
        NULL, // Class type
        REG_OPTION_NON_VOLATILE, // Storage options
        KEY_WRITE, // Access mask
        NULL, // Security attributes
        &hKey, // Address of handle to open or created key
        NULL // Address of disposition value
    );

    if (lResult == ERROR_SUCCESS) {
        // Set the string value
        lResult = RegSetValueExW(
            hKey, // Handle to an open key
            valueName, // Address of name of the value to set
            0, // Reserved
            REG_SZ, // Type of data
            (const BYTE*)data, // Address of the data to store
            dataSize // Size of the data
        );

        if (lResult == ERROR_SUCCESS) {
            std::wcout << L"Successfully set registry value: " << valueName << std::endl;
        } else {
            std::wcerr << L"Error setting registry value: " << lResult << std::endl;
        }

        // Close the key handle
        RegCloseKey(hKey);
    } else {
        std::wcerr << L"Error creating or opening registry key: " << lResult << std::endl;
    }

    return 0;
}