Search code examples
c++windowswinapiregistry

Setting registry value for all users


What is the best way to set registry value for all users on a machine. Enumerating HKEY_USERS and changing ntuser.dat is one solution.

Any other solution that can be used in XP,VISTA as well as Windows 7?

Actually installer needs to write one key into registry. And that key need to be used by all users. Once its used key should be reset.

This cannot be done if we keep only one copy of key in HKLM. Since if first user use it and reset it and then other users wont be able to use it.

Only solution I can think of is writing that key in all the users but that is not working in case of windows 7.


Solution

  • The pattern used by all software that needs this facility is, HKLM is a read-only store of defaults, set at installation time.

    At runtime, the software must always try to read from HKCU first, and fallback to HKLM if the HKCU data is not present, or stale. Regular users always write to HKCU, ensuring that their own settings track their own preferences.

    Administrative tools can write to HKLM to change users defaults. It's a bit of work, but you can use explicit timestamp entries, saving the last time a value in the hive was changed, and use this to refresh HKCU from HKLM whenever HKLM has a fresher timestamp to cater for "system administrators" perhaps needing to be able to reset certain settings to different defaults.