Registry Functions

The Windows Registry is a hierarchical database that stores configuration settings and options for the operating system and for applications that opt to use the registry to store their settings. This section provides detailed API reference documentation for interacting with the Windows Registry.

RegOpenKeyEx

Opens an existing registry key. If the key does not exist, the function does not create it. To create a key, use the RegCreateKeyEx function.

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

Parameters

Name Type Description
hKey HKEY A handle to an open key. This handle cannot be NULL. This key is assumed to be under the root of the registry.
lpSubKey LPCTSTR The name of the subkey to be opened.
ulOptions DWORD This parameter is reserved and must be zero.
samDesired REGSAM A mask that specifies the access rights for the key to be opened.
phkResult PHKEY A pointer to a variable that receives a handle to the opened key.

Return Value

ERROR_SUCCESS if the function succeeds, or a Win32 error code otherwise.
Important: Always close the handle returned by this function using RegCloseKey when you are finished with it.

RegQueryValueEx

Retrieves the type and data for the specified value name associated with an open registry key.

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

Parameters

Name Type Description
hKey HKEY A handle to an open key.
lpValueName LPCTSTR The name of the registry value to query.
lpReserved LPDWORD This parameter is reserved and must be NULL.
lpType LPDWORD A pointer to a variable that receives the type of the value.
lpData LPBYTE A pointer to a buffer that receives the value's data.
lpcbData LPDWORD A pointer to a variable that specifies the size, in bytes, of the buffer pointed to by lpData.

Return Value

ERROR_SUCCESS if the function succeeds, or a Win32 error code otherwise.

RegSetValueEx

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

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

Parameters

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

Return Value

ERROR_SUCCESS if the function succeeds, or a Win32 error code otherwise.

RegCloseKey

Closes the specified handle to an open registry key. The handle must be closed when it is no longer needed.

LONG RegCloseKey(
  HKEY   hKey
);

Parameters

Name Type Description
hKey HKEY A handle to an open registry key.

Return Value

ERROR_SUCCESS if the function succeeds, or a Win32 error code otherwise.