Retrieves the type and data for a specified registry value.
LONG RegQueryValueEx(
HKEY phkResult,
LPCWSTR lpValueName,
LPDWORD lpReserved,
LPDWORD lpType,
LPBYTE lpData,
LPDWORD lpcbData
);
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.
A pointer to a null-terminated string that contains the name of the registry value to query. For more information, see Remarks.
This parameter is reserved and must be NULL.
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:
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.
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.
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.
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:
RegQueryValueEx with lpData set to NULL. The function returns the required size for the data buffer in lpcbData.RegQueryValueEx again, passing a pointer to the allocated buffer in lpData and the size in lpcbData.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;
}