Safety note. This section is about understanding. You do not need to change anything in the real registry to follow it. When you try it yourself (in the lab), use only the test key
HKCU\Software\LearningLab, and back up first (Section 10).
The Big Picture: One Click, Seven Steps
You right-click a value in regedit, type a new number, and press OK. It takes a split second. Behind that moment, seven different parts of Windows pass the request along, like a relay race.
Here is the whole race first. Then we walk through each leg.
┌──────────────────────────────────────────────────────────────────────┐
│ 1. USER MODE: regedit.exe │
│ You edit HKCU\Software\LearningLab, value Volume = 80 │
└──────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────┐
│ 2. Win32 API: RegSetValueEx (advapi32.dll) │
│ checks the arguments and the type (forwarded on to kernelbase.dll)│
└──────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────┐
│ 3. Native API: NtSetValueKey (in ntdll.dll) │
│ puts the call number in a register, runs SYSCALL │
└──────────────────────────────────────────────────────────────────────┘
│ system call: switch to kernel mode
▼
┌──────────────────────────────────────────────────────────────────────┐
│ 4. KERNEL MODE: Configuration Manager (CmSetValueKey) │
│ a) checks your access right (ACL) on the key │
│ b) finds the key in the in-memory hive and changes the cell │
│ c) marks that part of the hive as DIRTY │
│ d) writes a note into the hive's .LOG file │
└──────────────────────────────────────────────────────────────────────┘
│ in memory first, disk later
▼
┌──────────────────────────────────────────────────────────────────────┐
│ 5. Lazy flush (background: every few seconds, or on demand) │
│ writes dirty parts of the hive to the hive file on disk │
└──────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────────────┐
│ 6. Notification: waiting threads are woken │
│ (those that called RegNotifyChangeKeyValue on this key) │
└──────────────────────────────────────────────────────────────────────┘
│ tell the watchers
▼
┌──────────────────────────────────────────────────────────────────────┐
│ 7. USER MODE: apps and services re-read the value │
│ RegQueryValueEx -> new data = 80 │
└──────────────────────────────────────────────────────────────────────┘
Keep this picture in mind. Each topic below zooms in on one part of it.
A reminder about two "worlds". Windows has user mode (where normal programs such as regedit run, with limited power) and kernel mode (where the core of Windows runs, with full power). A program cannot touch the registry data directly. It must ask the kernel, using a system call: a controlled doorway from user mode into kernel mode. The same doorway is used for every kind of kernel request, such as opening a file (see Chapter 8).
11.1 RegEdit → Configuration Manager (Kernel) → Hive Update in Memory
What it is: The path a change takes from the program you click to the data inside the kernel.
Step by step:
| Step | Who | What happens |
|---|---|---|
| 1 | regedit.exe | You press OK. regedit first opens the key (RegOpenKeyEx), asking for the right to write |
| 2 | advapi32.dll (a Windows library) | regedit calls RegSetValueEx, the Windows API function from Section 9. The library checks the arguments and passes the call on (through kernelbase.dll on current Windows) |
| 3 | ntdll.dll | The call becomes NtSetValueKey, one of the native API functions. This tiny function puts a system call number in a CPU register and runs the special CPU instruction (SYSCALL on 64-bit PCs) that jumps into the kernel |
| 4 | Kernel: Configuration Manager | The kernel's registry code (its functions start with Cm, for "Configuration Manager") takes over. See below |
What the Configuration Manager does, in order:
- Checks the handle. The key was opened earlier, and the handle remembers which rights were granted. If
regeditwas not allowed to write, the call stops here with access denied (Section 6). - Finds the key inside the hive that is loaded in memory. Inside the hive, keys and values are stored in small records called cells, joined by pointers, like a tree of index cards.
- Changes the value's cell (or makes a new cell if the value is new).
- Marks that part of the hive as "dirty" (changed in memory, but not yet on disk).
- Writes a note into the hive's log file (
.LOG1or.LOG2, Section 5), so that an interrupted write can be repaired. - Updates the key's last write time.
- Returns to user mode with a success code.
User mode | Kernel mode
|
regedit ──► advapi32 ──► ntdll ────┼───► Configuration Manager
syscall │
| ▼
| hive in RAM (marked dirty)
The hive is a memory object first. Remember Section 5: the kernel loads hive files and maps them into memory. Reads and writes happen to that memory copy. This is why the registry is fast: no disk is touched in the simple case.
What the reg command and PowerShell do. They follow the same path. reg add calls RegSetValueEx. PowerShell's Set-ItemProperty goes through the .NET Registry classes, which call the same API. All roads lead to NtSetValueKey.
Windows tip. If you want to see these calls live, a free Microsoft tool called Process Monitor (from Sysinternals) can show the registry operations (RegSetValue, RegQueryValue...) in real time. It is optional and not used in this chapter's lab.
11.2 Lazy Flush to Disk
What it is: After the change is in memory, it is not immediately written to the hive file. Windows writes it later, in the background. This is called a lazy flush ("lazy" because it does not hurry).
Why wait? Disks are slow compared with RAM. If every small change forced a disk write, Windows would slow to a crawl. Programs change registry values very often, so Windows collects the changes and writes them out together.
CHANGE 1 ─┐
CHANGE 2 ─┼─► in memory, marked dirty ─── (every few seconds) ───► hive file
CHANGE 3 ─┘ (one write for many changes)
The details (they may differ between Windows versions):
- A background system flusher writes the dirty parts of each hive to disk, about every few seconds (a figure around 5 seconds is often quoted).
- The log files (
.LOG1and.LOG2) hold the changes first, so that if the power fails between flushes, the next boot can replay the log and recover most changes (Section 5). - A program that needs the change on disk now can call
RegFlushKey. This forces the write and waits. It is slow, so programs use it rarely (the kernel itself uses it for very important data). - At shutdown, restart and sign-out, the hives are flushed and unloaded properly.
The risk. Changes made in the last few seconds before a power cut may be lost, or may be recovered from the log. This is part of why a forced power-off is dangerous (Section 10.1).
What a lazy flush means for you. After you change a value, you can usually keep working. You do not need to "save". There is no Save button in regedit because the change is applied the moment you press OK.
| Moment | Where the new value lives |
|---|---|
| Just after you press OK | RAM only (and a note in the log) |
| A few seconds later | RAM and the hive file on disk |
| Power cut before the flush | Recovered from the log at next boot, if the log was written |
11.3 Notification to Listening Components (RegNotifyChangeKeyValue)
What it is: Changing the value in memory is not enough. The programs that depend on the value need to know it changed. Windows provides a notification system for this.
How a program asks to be told. A program that wants to react to changes calls:
RegNotifyChangeKeyValue(key_handle, watch_subtree, filter, event, async)
(The kernel-level version is NtNotifyChangeKey.) In plain words, it says: "Tell me when something changes in this key". The parameters pick:
| Parameter | Meaning |
|---|---|
| Key handle | Which key to watch (it must be opened with the "Notify" right, Section 6) |
| Watch subtree | Also watch every key inside it, or only this key |
| Filter | What kind of change counts: a new or deleted subkey, a changed value, a change of the security rules |
| Event | A signal object the program waits on |
What happens after the change. When the Configuration Manager finishes the update (11.1), it looks at the list of watchers of that key (and its parent keys). For each watcher it signals the event. A thread that was sleeping, waiting for this event, wakes up.
Configuration Manager: "Volume was changed"
│
├──► signals the event of Program A (it watches this key)
├──► signals the event of Service B (it watches the parent key)
└──► nobody else is watching; done
Important detail: The notification says that something changed, not what the new value is. The program must go and read the key again (11.4).
Who uses it?
| Who | What they watch |
|---|---|
| Windows Explorer | Settings such as folder options and the theme, so open windows update |
| The Group Policy client | Policy keys (Section 8) |
| Services | Their own configuration keys |
| Security software | Run keys and other startup places (to catch malware) |
regedit | Itself: its tree can refresh when keys change (press F5 to refresh by hand) |
Notifications are one-shot. After an event fires, the program must call the function again to keep watching. Programs that forget will only see the first change.
11.4 When a Restart/Logoff Is Required vs Immediate Effect
What it is: Not every change takes effect at once. The result depends on when the program reads the value.
Three kinds of behaviour:
| Kind | What the program does | Example | When you see the change |
|---|---|---|---|
| Immediate | Watches the key (11.3) or reads the value every time it needs it | Many Settings-app options, some theme choices | At once, or within moments |
| Next start of the program | Reads the value once at startup and keeps it in memory | Most applications | After you close and reopen the program |
| Next sign-in or restart | Loaded by a part of Windows that starts at sign-in or boot | Explorer's startup behaviours, services, drivers, boot settings | After sign-out or restart |
Value changed in registry
│
├── program watches it? ───────── yes ──► re-reads now (IMMEDIATE)
│
├── program reads it only at start ──────► change seen at the
│ program's next start
│
└── loaded by Windows at logon/boot ─────► change seen after
sign-out or restart
Examples to remember:
| Setting | What it takes |
|---|---|
A user preference in HKCU that a running app reads on demand | Immediate |
The value in a Run key (Section 9) | Next sign-in (it does nothing to programs already running) |
A service's Start mode in HKLM\SYSTEM\CurrentControlSet\Services\... | Next restart, or when the service is restarted (see Chapter 12) |
| A driver's parameters (see Chapter 13) | Usually a restart, or the device must be disabled and enabled again |
Environment variables in HKCU\Environment | New programs started afterwards, often after sign-out; running programs keep the old values |
| File association changes (Section 9.3) | Explorer may need a refresh or restart |
| Policies (Section 8) | At the next policy refresh, or after gpupdate /force |
Why a restart is sometimes the only safe way. Some data is read before anything can watch it, for example the boot settings in HKLM\SYSTEM that the boot loader reads (Section 5.4). Changing them while Windows is running changes the file, but nothing re-reads it until the next boot.
Fast ways to apply a change (when appropriate):
| Action | What it does |
|---|---|
| Close and reopen the program | Re-reads startup settings |
| Sign out and in | Reloads HKCU-based startup settings |
| Restart Windows Explorer (Task Manager → Windows Explorer → Restart) | Applies many shell settings without a full restart |
gpupdate /force | Re-applies policy |
| Restart the PC | Applies anything that loads at boot |
Troubleshooting the idea "I changed it but nothing happened".
- Did you change the right key? (
HKCUvsHKLM; 32-bit vs 64-bit view, Section 7.) - Does a policy override it? (Section 8.)
- Does the program read it only at startup? Restart the program.
- Is it virtualized (Section 7)? The program may be reading the VirtualStore copy.
- Did you use the right type (a number stored as text may be ignored)?
- Does the change need a sign-out or restart?
Summary
| Step | Who | What it does |
|---|---|---|
| 1 | regedit.exe | You press OK; it calls the Windows API |
| 2 | advapi32.dll | RegSetValueEx checks and forwards the call |
| 3 | ntdll.dll | NtSetValueKey, then a system call into the kernel |
| 4 | Configuration Manager | Checks access, updates the hive in memory, marks it dirty, logs it |
| 5 | Lazy flush | Writes dirty parts to the hive file every few seconds (RegFlushKey forces it) |
| 6 | Notification | RegNotifyChangeKeyValue watchers are woken |
| 7 | Applications | They re-read the value, or use it at their next start, or after sign-in or restart |
| Rule | No Save button | The change is live in memory at once, on disk soon after |