C++ – Setting registry value for all users

cregistrywinapiwindows

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.

Best Answer

You can't write to parts of the registry owned by other user's unless you have admin rights. If you can get admin rights, you would be much better using HKLM. Even if you could iterate over HKEY_USERS it is a brittle solution at best.

You probably don't want to require admin rights. In which case the registry does not have a place where non-admin users can store data to be shared between all users. Consequently you should save to a file in CSIDL_COMMON_APPDATA.

On the other hand, perhaps what you are trying to achieve is to set a value at install time which users will pick up. If they then modify the setting in your app you want it saved under HKCU. You can do this quite easily by writing your default settings to HKLM. Then when your app reads the settings it first looks in HKCU. If the setting is not present, it reads it out of HKLM. The app always writes the values to HKCU. Another variant is to build the default settings into the program rather than HKLM which simplifies the installer.

The bottom line is that iterating of HKEY_USERS is not a good solution to any problem that I can envisage.