Registry API Reference
This section provides detailed documentation for the Windows Registry API, which allows applications to access and manipulate the system's registry. The registry is a hierarchical database that stores configuration settings and options for the operating system, hardware, and applications.
Overview
The Windows Registry is organized as a tree of keys and values. Each key can contain subkeys and values, similar to a file system. Applications can use the registry API to:
- Create, open, and close registry keys.
- Set, retrieve, and delete registry values.
- Enumerate subkeys and values within a key.
- Manage security and permissions for registry keys.
Key Functions
The following are some of the most commonly used functions for interacting with the Windows Registry:
Key Management
RegCreateKeyEx: Creates a new registry key or opens an existing one.RegOpenKeyEx: Opens an existing registry key.RegCloseKey: Closes an open registry key handle.RegDeleteKeyEx: Deletes a registry key and its subkeys.RegEnumKeyEx: Enumerates the subkeys of a registry key.
Value Management
RegSetValueEx: Sets the data and type for a specified registry value.RegQueryValueEx: Retrieves the type and data for a specified registry value.RegDeleteValue: Deletes a specified registry value.RegEnumValue: Enumerates the values of a registry key.
Registry Hives and Root Keys
The registry is composed of several root keys, often referred to as hives. Common root keys include:
HKEY_CLASSES_ROOT: File associations and COM object information.HKEY_CURRENT_USER: User-specific settings for the currently logged-in user.HKEY_LOCAL_MACHINE: System-wide hardware and software configuration.HKEY_USERS: Settings for all user profiles on the system.HKEY_CURRENT_CONFIG: Hardware profile information for the system.
Example Usage
Here's a basic C++ example demonstrating how to create a registry key and set a string value:
#include <windows.h>
#include <iostream>
int main() {
HKEY hKey;
DWORD dwDisposition;
LPCWSTR subKeyPath = L"Software\\MyApplication\\Settings";
LPCWSTR valueName = L"DefaultPath";
WCHAR valueData[] = L"C:\\Program Files\\MyApplication\\Data";
LONG result = RegCreateKeyExW(
HKEY_CURRENT_USER,
subKeyPath,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
NULL,
&hKey,
&dwDisposition
);
if (result == ERROR_SUCCESS) {
result = RegSetValueExW(
hKey,
valueName,
0,
REG_SZ,
(LPBYTE)valueData,
sizeof(valueData)
);
if (result == ERROR_SUCCESS) {
std::wcout << L"Registry value '" << valueName << L"' set successfully." << std::endl;
} else {
std::wcerr << L"Failed to set registry value. Error code: " << result << std::endl;
}
RegCloseKey(hKey);
} else {
std::wcerr << L"Failed to create or open registry key. Error code: " << result << std::endl;
}
return 0;
}
regedit.exe) to explore existing keys and values.