Windows Registry Reference

The Windows Registry is a hierarchical database that stores low-level settings for the Microsoft Windows operating system and for applications that opt to use the registry. It contains information such as user profiles, installed hardware, software configuration, and operating system settings.

This section provides detailed documentation for the Core APIs related to interacting with the Windows Registry. These APIs allow developers to programmatically read, write, and manage registry keys and values.

Key Registry Functions

RegOpenKeyEx()

Opens an existing key in the registry. If the key is not in the current computer's registry, the function supplies the information for the non-existent key.

Syntax:

LONG RegOpenKeyEx(
  HKEY hKey,
  LPCSTR lpSubKey,
  DWORD ulOptions,
  REGSAM samDesired,
  PHKEY phkResult
);

Parameters:

Name Type Description
hKey HKEY A handle to an open key in the registry.
lpSubKey LPCSTR The name of the subkey to open.
ulOptions DWORD This parameter is reserved and must be zero.
samDesired REGSAM A bitmask that specifies the desired access rights to the key.
phkResult PHKEY A pointer to a variable that receives a handle to the opened key.

Return Value:

LONG

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a system-defined error code.

RegCreateKeyEx()

Creates the specified registry key. If the key already exists, the function opens it and returns a handle to it. The key is created under the parent key indicated by the hKey parameter.

Syntax:

LONG RegCreateKeyEx(
  HKEY hKey,
  LPCSTR lpSubKey,
  DWORD Reserved,
  LPSTR lpClass,
  DWORD dwOptions,
  REGSAM samDesired,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  PHKEY phkResult,
  LPDWORD lpDisposition
);

Parameters:

Name Type Description
hKey HKEY A handle to an open key in the registry.
lpSubKey LPCSTR The name of the subkey to create or open.
Reserved DWORD This parameter is reserved and must be zero.
lpClass LPSTR The user-defined class type of the key being created.
dwOptions DWORD Specifies the options for the key.
samDesired REGSAM A bitmask that specifies the desired access rights to the key.
lpSecurityAttributes LPSECURITY_ATTRIBUTES A pointer to a SECURITY_ATTRIBUTES structure.
phkResult PHKEY A pointer to a variable that receives a handle to the opened or created key.
lpDisposition LPDWORD A pointer to a variable that receives a value indicating whether the key was created or opened.

Return Value:

LONG

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a system-defined error code.

RegSetValueEx()

Sets the data for a specified registry value under an open key.

Syntax:

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

Parameters:

Name Type Description
hKey HKEY A handle to an open key in the registry.
lpValueName LPCSTR The name of the registry value to set.
Reserved DWORD This parameter is reserved and must be zero.
Type DWORD The type of data to be stored.
lpData const BYTE* A pointer to the buffer that contains the data to be stored.
cbData DWORD The size, in bytes, of the data.

Return Value:

LONG

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a system-defined error code.

RegGetValue()

Retrieves the type and data for the specified registry value. This function supports local and remote retrieval of registry values.

Syntax:

LONG RegGetValue(
  HKEY hkey,
  PCSTR lpSubKey,
  PCSTR lpValue,
  DWORD dwFlags,
  LPDWORD pdwType,
  PVOID pvData,
  LPDWORD pcbData
);

Parameters:

Name Type Description
hkey HKEY A handle to an open key in the registry, or any of the predefined HKEY_ constants.
lpSubKey PCSTR The path to the subkey that contains the value.
lpValue PCSTR The name of the value to retrieve.
dwFlags DWORD Flags that specify how the data is to be read.
pdwType LPDWORD A pointer to a variable that receives the type of the data.
pvData PVOID A pointer to a buffer that receives the value's data.
pcbData LPDWORD A pointer to a variable that receives the size of the data.

Return Value:

LONG

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a system-defined error code.

Important Considerations

Registry operations can have a significant impact on system stability. Always ensure you have proper error handling and understand the implications of modifying registry values. Back up the registry before making critical changes.

Registry Data Types

The Windows Registry supports various data types. Common types include:

For a complete list of registry data types and more advanced registry operations, please refer to the Advanced Registry Operations page.