MSDN Documentation

Windows Registry

The Windows Registry is a hierarchical database that stores low-level settings for the operating system and for applications that opt to use it. It provides a unified, central location for configuration data, system policies, device drivers, services, and user preferences.

On this page

Registry Structure

The registry is organized as a tree of keys and values. Keys act like folders, while values hold the actual data.

HKEY_CURRENT_USER
│   └─ Software
│        └─ MyApp
│            ├─ Settings (value)
│            └─ Preferences (value)
        

Root Hives

HiveDescription
HKEY_CLASSES_ROOTAssociates file extensions with applications.
HKEY_CURRENT_USERSettings specific to the currently logged‑on user.
HKEY_LOCAL_MACHINESettings for the local computer that apply to all users.
HKEY_USERSAll user profiles loaded on the system.
HKEY_CURRENT_CONFIGHardware configuration information at boot.

Keys and Values

Each key can contain subkeys and values. Values consist of a name, type, and data.

Key: HKEY_CURRENT_USER\Software\MyApp
└─ Value: (Default) REG_SZ = "My Application"
└─ Value: InstallPath REG_EXPAND_SZ = "%ProgramFiles%\\MyApp"
└─ Value: Enabled REG_DWORD = 1
        

Value Types

TypeIdentifierUse
StringREG_SZStandard text.
Expandable StringREG_EXPAND_SZContains environment variables.
BinaryREG_BINARYRaw binary data.
DWORD (32‑bit)REG_DWORDUnsigned integer.
QWORD (64‑bit)REG_QWORDUnsigned 64‑bit integer.
Multi‑StringREG_MULTI_SZMultiple null‑terminated strings.
NoneREG_NONEUnspecified data type.

Access Permissions

Security descriptors control who can read, write, or delete keys. Use the RegSetKeySecurity and RegGetKeySecurity APIs to manage ACLs.

Using the Registry API (C++)

Below is a minimal example that reads a string value from HKEY_CURRENT_USER.

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

int main()
{
    HKEY hKey;
    LONG result = RegOpenKeyExW(
        HKEY_CURRENT_USER,
        L"Software\\MyApp",
        0,
        KEY_READ,
        &hKey);

    if (result != ERROR_SUCCESS) {
        std::wcerr << L"Failed to open key: " << result << std::endl;
        return 1;
    }

    wchar_t buffer[256];
    DWORD bufSize = sizeof(buffer);
    result = RegQueryValueExW(
        hKey,
        L"InstallPath",
        nullptr,
        nullptr,
        reinterpret_cast<LPBYTE>(buffer),
        &bufSize);

    if (result == ERROR_SUCCESS) {
        std::wcout << L"InstallPath = " << buffer << std::endl;
    } else {
        std::wcerr << L"Failed to read value: " << result << std::endl;
    }

    RegCloseKey(hKey);
    return 0;
}
        

PowerShell Access

PowerShell provides Get-ItemProperty and Set-ItemProperty cmdlets for registry interaction.

# Read the InstallPath value
$path = Get-ItemProperty -Path "HKCU:\Software\MyApp" -Name InstallPath
Write-Output $path.InstallPath

# Write a new DWORD value
Set-ItemProperty -Path "HKCU:\Software\MyApp" -Name Enabled -Value 1 -Type DWord
        

Best Practices