Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
Registry
Registry Reference
Registry Functions
 RegSetValueEx Function
RegSetValueEx Function

Sets the data and type of a specified value under a registry key.

Syntax

C++
LONG WINAPI RegSetValueEx(
  __in        HKEY hKey,
  __in_opt    LPCTSTR lpValueName,
  __reserved  DWORD Reserved,
  __in        DWORD dwType,
  __in_opt    const BYTE *lpData,
  __in        DWORD cbData
);

Parameters

hKey [in]

A handle to an open registry key. The key must have been opened with the KEY_SET_VALUE access right. For more information, see Registry Key Security and Access Rights.

This handle is returned by the RegCreateKeyEx, RegCreateKeyTransacted, RegOpenKeyEx, or RegOpenKeyTransacted function. It can also be one of the following predefined keys:

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_PERFORMANCE_DATA
HKEY_USERS

lpValueName [in, optional]

The name of the value to be 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.

For more information, see Registry Element Size Limits.

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

Reserved

This parameter is reserved and must be zero.

dwType [in]

The type of data pointed to by the lpData parameter. For a list of the possible types, see Registry Value Types.

lpData [in, optional]

The data to be stored.

For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters. A backslash must be preceded by another backslash as an escape character. For example, specify "C:\\mydir\\myfile" to store the string "C:\mydir\myfile".

cbData [in]

The size of the information pointed to by the lpData parameter, in bytes. 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 or characters.

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.

Remarks

Value sizes are limited by available memory. However, storing large values in the registry can affect its performance. Long values (more than 2,048 bytes) should be stored as files, with the locations of the files stored in the registry.

Application elements such as icons, bitmaps, and executable files should be stored as files and not be placed in the registry.

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.

Note that operations that access certain registry keys are redirected. For more information, see Registry Virtualization and 32-bit and 64-bit Application Data in the Registry.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinreg.h (include Windows.h)
LibraryAdvapi32.lib
DLLAdvapi32.dll
Unicode and ANSI namesRegSetValueExW (Unicode) and RegSetValueExA (ANSI)

See Also

RegCreateKeyEx
RegFlushKey
Registry Functions
Registry Overview
RegOpenKeyEx
RegQueryValueEx

Send comments about this topic to Microsoft

Build date: 11/19/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Note for Visual Basic      JMatuzek   |   Edit   |   Show History
When lpData is a string, make sure to use the alternate function declaration found here: http://guidepc.altervista.org/windows/api/functions/regsetvalueex.html.

Using the "general" declaration doesn't cause any exceptions when lpData is a string, but the value will usually be set to random gibberish/garbage instead of the intended string.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker