Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a key to HKEY_CURRENT_USER for all users

I have an installer which installs a key on the HKEY_CURRENT_USER. When I run the installer, it only adds it on the user that is installing. Is there a way to add the key to all users at once?

Thanks

like image 590
David Brunelle Avatar asked Apr 25 '13 17:04

David Brunelle


People also ask

Is HKEY_CURRENT_USER different for each user?

The HKEY_CURRENT_USER registry hive is specific to each user, so in order to modify this for another user, we first need to identify where that information is stored.

How do I access Hkcu of another user?

Open regedit and select HKEY_USERS then go to File->Load Hive. Browse to the users Hive file, usually NTUSER. DAT located at %userprofile%. This of course requires you to be admin or have access to the users hive file.

Does HKEY_CURRENT_USER override Hkey_local_machine?

No, HKCU doesn't override HKLM. If you make a change in HKCU, it will be applied to the current user only. However, if you apply for a change in HKLM, it will apply to the entire computer.


2 Answers

You'd have to go through all the different users under HKEY_USERS, which requires elevated rights. And doesn't capture any users that have not yet been created. That's just the wrong approach.

The way to do it is to add the default values to a corresponding key under HKLM at install time. When your program attempts to read from the registry, it looks in HKCU first, and if your key is not present, it copies the information from the corresponding key in HKLM to the key in HKCU.

A general rule of installer programs is that they should not rely on being run by the user that will subsequently use the program that has been installed. Certainly in corporate settings programs are usually installed under a user account that will never subsequently run the program being installed.

like image 97
David Heffernan Avatar answered Sep 23 '22 00:09

David Heffernan


In some cases, Active Setup may be the solution.

It works by adding a key to HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name% with a version number. When a user logs in Windows checks this location and compares it to HKCU\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%. If it is missing or a lower version then it runs whatever has been set in HKLM\SOFTWARE\Microsoft\Active Setup\Installed Components\%package name%\StubPath.

You can do some custom things this way, for example I used it to add a certain script (to map a network drive) to the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run the following way:

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive" /v "Version" /d "1" /t REG_SZ /f

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive" /v "StubPath" /d "reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v "MountDrive" /d "C:\map.cmd" /t REG_DWORD /f" /f

What happens here:

  • when the user next logs on, Active Setup checks if there is a string Version with value of 1 or greater in the registry under key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive. There is none, therefore it creates it, and also runs the second reg add command, which adds a string with a value C:\map.cmd under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
  • it only happens once, because on each consecutive log on Active Setup will find out that the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\MapDrive now has a Version under it.

Sounds complicated, but makes sense once you figure it out, and very useful.

http://wpkg.org/Adding_Registry_Settings#Adding_entries_to_HKCU_for_all_users

like image 41
Evgeny Avatar answered Sep 25 '22 00:09

Evgeny