Windows API Reference

RegQueryValueEx function

Retrieves the type and data for a specified registry value.

Syntax

LONG RegQueryValueEx(
  HKEY phkResult,
  LPCWSTR lpValueName,
  LPDWORD lpReserved,
  LPDWORD lpType,
  LPBYTE  lpData,
  LPDWORD lpcbData
);

Parameters

phkResult
HKEY

A handle to an open registry key. The handle is returned by the RegOpenKeyEx or RegCreateKeyEx function. It can also be one of the following predefined keys: HKEY_CLASSES_ROOT, HKEY_CURRENT_CONFIG, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS.

lpValueName
LPCWSTR

A pointer to a null-terminated string that contains the name of the registry value to query. For more information, see Remarks.

lpReserved
LPDWORD

This parameter is reserved and must be NULL.

lpType
LPDWORD

A pointer to a variable that receives the type of the data specified by lpValueName. This parameter can be NULL if the type of the value is not required.

The possible values for the type are defined in Winnt.h. The following are some common values:

  • REG_SZ: A null-terminated string.
  • REG_DWORD: A 32-bit or 64-bit number in little-endian format.
  • REG_BINARY: Binary data in any form.
  • REG_MULTI_SZ: An array of null-terminated strings, terminated by a second null character.
lpData
LPBYTE

A pointer to a buffer that receives the data of the specified registry value. If this parameter is NULL, the function returns the size of the data buffer required in lpcbData.

lpcbData
LPDWORD

A pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the lpData parameter. When the function returns, this variable contains the size of the data copied to lpData, in bytes. If the buffer pointed to by lpData is not large enough to hold the data, the function returns ERROR_MORE_DATA and the variable pointed to by lpcbData contains the required buffer size in bytes.

Return value

If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is a system error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to retrieve a generic description of the error.

Important
The registry contains sensitive information. Applications should not directly access the registry unless absolutely necessary. Consider using higher-level APIs that abstract registry access, such as COM or configuration files.

Remarks

If lpValueName is NULL or an empty string (""), the function retrieves the type and data of the key's default value.

To read a value, you typically follow these steps:

  1. Call RegQueryValueEx with lpData set to NULL. The function returns the required size for the data buffer in lpcbData.
  2. Allocate a buffer of the specified size.
  3. Call RegQueryValueEx again, passing a pointer to the allocated buffer in lpData and the size in lpcbData.
Tip
For string values (REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ), the buffer size returned in lpcbData includes the null-terminator.

Example

The following example retrieves the REG_SZ value of a registry entry.


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

int main() {
    HKEY hKey;
    LONG result;
    DWORD type;
    WCHAR buffer[256]; // Assuming the value won't exceed 256 characters
    DWORD bufferSize = sizeof(buffer);

    // Open a registry key (replace with your actual key path)
    result = RegOpenKeyEx(HKEY_CURRENT_USER,
                          L"Software\\MyApp",
                          0,
                          KEY_READ,
                          &hKey);

    if (result == ERROR_SUCCESS) {
        // Query the value (replace "MyValueName" with your actual value name)
        result = RegQueryValueEx(hKey,
                                 L"MyValueName",
                                 NULL,
                                 &type,
                                 (LPBYTE)buffer,
                                 &bufferSize);

        if (result == ERROR_SUCCESS) {
            if (type == REG_SZ) {
                std::wcout << L"Value: " << buffer << std::endl;
            } else {
                std::wcerr << L"Value is not a REG_SZ type." << std::endl;
            }
        } else {
            std::wcerr << L"Error querying registry value: " << result << std::endl;
        }

        RegCloseKey(hKey);
    } else {
        std::wcerr << L"Error opening registry key: " << result << std::endl;
    }

    return 0;
}
        

See also