Registry API Reference

This section provides detailed information about the Windows Registry API, which allows applications to access and manage the system's configuration data.

Core Registry Functions

RegOpenKeyEx

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

Opens an existing key in the registry. If the key or any subkeys in the path do not exist, the function does not create them.

Parameters

  • hKey: A handle to an open registry key.
  • lpSubKey: The name of the registry subkey to be opened.
  • ulOptions: Reserved; must be zero.
  • samDesired: A mask that specifies the desired access rights to the key.
  • phkResult: A pointer to a variable that receives a handle to the opened key.

Return Value

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

RegCreateKeyEx

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

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

Parameters

  • hKey: A handle to an open registry key.
  • lpSubKey: The name of the subkey to create or open.
  • Reserved: Reserved; must be zero.
  • lpClass: User-defined class type. May be NULL.
  • dwOptions: Specifies other options for the key.
  • samDesired: A mask that specifies the desired access rights to the key.
  • lpSecurityAttributes: Security attributes.
  • phkResult: Pointer to a variable that receives a handle to the opened key.
  • lpDisposition: Pointer to a variable that receives one of the following values indicating whether the key was created or opened.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

RegCloseKey

LONG RegCloseKey(HKEY hKey);

Closes a handle to the specified registry key. The function then purges all information about the key from the specified handle.

Parameters

  • hKey: A handle to the open registry key to close.

Return Value

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

RegQueryValueEx

LONG RegQueryValueEx(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData);

Retrieves the data and type of a specified registry value.

Parameters

  • hKey: A handle to an open registry key.
  • lpValueName: The name of the registry value to query.
  • lpReserved: Reserved; must be NULL.
  • lpType: A pointer to a variable that receives the type code.
  • lpData: A pointer to a buffer that receives the value's data.
  • lpcbData: A pointer to a variable that specifies the size of the buffer pointed to by lpData.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

See Also

RegSetValueEx

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

Creates or opens a specified registry value and sets its data and extended properties.

Parameters

  • hKey: A handle to an open registry key.
  • lpValueName: The name of the registry value to set.
  • Reserved: Reserved; must be zero.
  • dwType: The type of the data to be stored.
  • lpData: A pointer to a buffer containing the data to be stored.
  • cbData: The size, in bytes, of the data buffer pointed to by lpData.

Return Value

If the function succeeds, the return value is ERROR_SUCCESS. Otherwise, the return value is a system error code.

See Also

Registry Data Types

The registry supports several data types, including:

  • REG_SZ: A null-terminated string.
  • REG_EXPAND_SZ: A null-terminated string that contains unexpanded references to environment variables.
  • REG_MULTI_SZ: An array of null-terminated strings, terminated by a final NULL character.
  • REG_DWORD: A 32-bit unsigned integer.
  • REG_QWORD: A 64-bit unsigned integer.
  • REG_BINARY: Binary data in any form.

Registry Keys and Hives

The Windows Registry is organized hierarchically into hives, which are the root level of the registry structure. Common hives include:

  • HKEY_CLASSES_ROOT: Information about file associations and COM object registrations.
  • HKEY_CURRENT_USER: Settings for the user currently logged on.
  • HKEY_LOCAL_MACHINE: System-wide configuration settings.
  • HKEY_USERS: Settings for all user profiles on the system.
  • HKEY_CURRENT_CONFIG: Information about the hardware profile currently used by the system.

Each hive contains keys, which are like directories, and values, which store the actual data.