Persistent storage and the registry

This came up in the newsgroup yesterday but I think it’s worth noting here as well…

For performance reasons, the system registry is loaded into memory and changes are flushed back to persistent storage periodically.  This is great for perf but it also means is that if the user soft resets their device or yanks a battery during the period of time between a registy value changing and those changes being flushed, the changes will be lost when the device is tuned back on.

Developers can prevent this by calling RegFlushKey to force a flush to happen after making registry changes.  But what about legacy code that doesn’t call RegFlushKey?  Will users need to worry about unexpected things happening whenever they reset or change batteries?  Nope.  Very few people will ever encounter this scenario of registry changes being lost due to a ton of backwards compatibility work we did around this for Windows Mobile 5.0.  On top of flushing after the device has been idle for a certain amount of time, we’ve taken the liberty of identifying the activiites that most commonly precede a device being reset and automatically flush the registry at those times.

Tap the device’s power button.  Change a setting in a control panel (including 3rd party control panels) and then hit “OK” (PPC) or “Done” (SP).  Install an app through ActiveSync or a CAB on the device.  Run and app that resets the device by calling KernelIoCtl or ExitWindowsEx.  Any of these things will result in the OS jumping in and flushing the registry.


Comments (6)

  1. ce_base says:

    I don’t know why your blog didn’t automatically cross-reference my post, so I’m adding my own cross-reference:

  2. ChunFai says:

    I tried to modify the key HKLMsoftwaremicrosoftspeech to disable Cyberon Voice Tag but whenever I reboot my phone the modification is reverted. Any idea?

  3. Matthew says:

    I believe there is some highly errornous information in this post. Rebooting via ExitWindowEx() does indeed flush the fliesystem cach and the registrty. However, rebooting via KernelIoCtl() DOES NOT. That method has been discouraged for several versions of the operating system and, under the proper conditions, could cause data loss even on the RAM-based object store of prior versions. For versions prior to 5, where ExitWindowsEx() is not available, the SetSystemPoertState() call is a safe alternative to KernelIoCtl().

  4. scyost says:

    Matthew – Have you tried that on WM5? I’ve written code to set a reg key and then reboot via IOCTL and verified that the registry key persists. (and if you set the reg key and then immediately pull battery, the change is lost)

  5. ce_base says:

    To follow up on Matthew / Scott’s discussion of rebooting via IOCTL — in Windows Mobile 5 we hacked IOCTL_HAL_REBOOT to call into the safe power down which flushes the registry and other cached data.  We still want to discourage people from using the IOCTL, but we made it as safe as we could.  Because too many people were still relying on the IOCTL instead of better methods.


  6. temez says:

    I wonder what’s wrong. Some of my registry changes always disappear if the battery runs low or the device reboots. I’ve tried calling RegFlushKey() and resetting the device. Still some of the keys always disappear. The weird part is that some settings do persist, some don’t.

    I wonder if there’s something wrong with HTC’s TyTN that I’m debugging on or if it has something to do with calling RegFlushKey() from withing SetupDLL.