Registry Management API Reference
This section provides details on the Windows Registry API, enabling developers to programmatically access and manipulate the system registry.
Overview
The Windows Registry is a hierarchical database that stores low-level settings for the operating system and for applications that opt to use the registry to store configuration data.
Important: Incorrectly modifying the registry can cause serious system problems. Always back up your registry before making changes.
Key Concepts
- Hives: Top-level registry keys (e.g., HKEY_CLASSES_ROOT, HKEY_CURRENT_USER).
- Keys: Folders within the registry hierarchy.
- Values: Data entries associated with keys. Values have a name, a type, and data.
- Value Types: REG_SZ (string), REG_DWORD (32-bit integer), REG_BINARY (binary data), etc.
Core Functions
The following functions are commonly used for registry management:
Opening and Closing Keys
RegOpenKeyEx: Opens an existing registry key.RegCreateKeyEx: Creates a new registry key or opens an existing one.RegCloseKey: Closes an open registry key handle.
Reading Values
RegQueryValueEx: Retrieves the data and type for a specified registry value.RegEnumKeyEx: Enumerates the subkeys of a specified registry key.RegEnumValue: Enumerates the values of a specified registry key.
Writing Values
RegSetValueEx: Sets the data and type for a specified registry value.RegDeleteKey: Deletes a subkey and its values from the registry.RegDeleteValue: Deletes a named entry (value) from a registry key.
Example: Reading a Registry Value
The following C++ code snippet demonstrates how to read a string value from the registry:
#include <windows.h>
#include <iostream>
int main() {
HKEY hKey;
LONG result;
DWORD type = REG_SZ;
CHAR value[256];
DWORD dataSize = sizeof(value);
// Open the key
result = RegOpenKeyEx(
HKEY_CURRENT_USER,
TEXT("Software\\MyApp"),
0,
KEY_READ,
&hKey);
if (result == ERROR_SUCCESS) {
// Query the value
result = RegQueryValueEx(
hKey,
TEXT("SettingName"),
NULL,
&type,
(LPBYTE)value,
&dataSize);
if (result == ERROR_SUCCESS && type == REG_SZ) {
std::wcout << L"Value: " << value << std::endl;
} else {
std::cerr << "Failed to query value or incorrect type." << std::endl;
}
RegCloseKey(hKey);
} else {
std::cerr << "Failed to open registry key." << std::endl;
}
return 0;
}
Registry Viewer Tools
For manual inspection and editing, the built-in Windows Registry Editor (regedit.exe) is available.