Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using windows environment variables as registry values in Visual Studio Installer

Hey, i want to use windows environment variables as value for a registry entry. Unfortunately i can not simply write sth like %systemroot%\system32\MyScreensaver.scr.

As you can guess, i want to point some reg values to my own app, such as the auto start and screensaver and some other things.

Any ideas?

like image 663
Martin Booka Weser Avatar asked May 23 '11 19:05

Martin Booka Weser


People also ask

Can you use environment variables in registry?

With REG_EXPAND_SZ string values, you can use environment variables for paths that are stored in the registry. These entries require special formatting in order to be recognized by the operating system as environment variables.

How do I set environment variables in registry?

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment".

How do I set environment variables in Visual Studio?

To access this page, select a project node in Solution Explorer, select Project > Properties from the Visual Studio menu, and then select the Environment Variables tab. Specifies the name of an environment variable that will be used when the project is built or when the project is run from Visual Studio.

Where is environment variables in the registry?

Machine environment variables are stored or retrieved from the following registry location: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment . Process environment variables are generated dynamically every time a user logs in to the device and are restricted to a single process.


3 Answers

The Windows registry supports this natively with the REG_EXPAND_SZ registry value type.

Just use REG_EXPAND_SZ instead of REG_SZ when you want to embed environment variables in the registry key value.

Here is an example of C# code accessing a REG_EXPAND_SZ and the expansion is handled automatically:

var registry = Registry.CurrentUser.OpenSubKey("Environment");
var temp = registry.GetValue("TEMP") as string;

Here is an example of creating an expandable registry value:

registry.SetValue("TEMP", @"%USERPROFILE%\AppData\Local\Temp", RegistryValueKind.ExpandString);

Other platforms or scripting languages have other mechanisms to support this. Here is the low-level Win32 description of REG_EXPAND_SZ:

  • Registry Value Types
like image 176
Rick Sladkey Avatar answered Oct 16 '22 19:10

Rick Sladkey


You can use the Windows Installer formatted type. For example, your registry value can be:

[%SystemRoot]\system32\MyScreensaver.scr

This way Windows Installer will automatically resolve the environment variable during installation.

like image 41
rmrrm Avatar answered Oct 16 '22 21:10

rmrrm


It's kind of tricky but very easy to do. This example would allow you to open a certain type of file (*.test) up with a program (Notepad++) that resides in a user specific directory. I used this for a scenario on my Windows 2008 server running Remote Desktop (AKA Terminal Server) to allow each user to use a program installed PER user account so that different settings could be used per user (ex: *.ini files located within the apps directory). Note: Not that it matters but the folder "programs" is hidden so the users do not see it.

Paths:
Application (notepad++): Y:\%username%\programs\Notepad++\notepad++.exe
File To Open (File.test): Y:\%username%\TestFiles\File.test

Step 1. IF you are using mandatory profiles be sure to change the NTUSER.MAN file back to NTUSER.DAT.

Step 2. Log into the profile you wish to edit using Remote Desktop Client.

Step 3. Open up regedit and delete the following keys if they exist.

HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.test

Step 4. Create a new text file and name it "original.reg". Fill it with the following...

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open\command]
@=""

Step 5. Refresh registry (F5) and look for the new key. Should see the "command" key with "(Default)" value blank. In the "command" key add a new "Expandable String Value" called "New Value #1". Set the value to the application's path "Y:\%username%\programs\Notepad++\notepad++.exe" "%1". Then export the "command" key to the desktop as "expanded.reg".

Step 6. Edit the "expanded.reg" file in notepad and copy all the data after the "New Value #1". Ex: =hex(2):22...

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open\command]
@=""
"New Value #1"=hex(2):22,00,59,00,3a,00,5c,00,25,00,75,00,73,00,65,00,72,00,6e,00,\
61,00,6d,00,65,00,25,00,5c,00,70,00,72,00,6f,00,67,00,72,00,61,00,6d,00,73,\
00,5c,00,4e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2b,00,2b,00,5c,00,6e,00,\
6f,00,74,00,65,00,70,00,61,00,64,00,2b,00,2b,00,2e,00,65,00,78,00,65,00,22,\
00,20,00,22,00,25,00,31,00,22,00,00,00

Step 7. Close expanded.reg file and open the "original.reg" file then replace the default command (that is empty "") with the new hex value we have in our clipboard. The file "original.reg" should look like the following now...

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open\command]
@=hex(2):22,00,59,00,3a,00,5c,00,25,00,75,00,73,00,65,00,72,00,6e,00,\
61,00,6d,00,65,00,25,00,5c,00,70,00,72,00,6f,00,67,00,72,00,61,00,6d,00,73,\
00,5c,00,4e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2b,00,2b,00,5c,00,6e,00,\
6f,00,74,00,65,00,70,00,61,00,64,00,2b,00,2b,00,2e,00,65,00,78,00,65,00,22,\
00,20,00,22,00,25,00,31,00,22,00,00,00

Step 8. Now that we have our expanded variable ready to go lets just add an extension to associate with the application. Add the following to the "original.reg" file making it look like the following...

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open]

[HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe\shell\open\command]
@=hex(2):22,00,59,00,3a,00,5c,00,25,00,75,00,73,00,65,00,72,00,6e,00,\
61,00,6d,00,65,00,25,00,5c,00,70,00,72,00,6f,00,67,00,72,00,61,00,6d,00,73,\
00,5c,00,4e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2b,00,2b,00,5c,00,6e,00,\
6f,00,74,00,65,00,70,00,61,00,64,00,2b,00,2b,00,2e,00,65,00,78,00,65,00,22,\
00,20,00,22,00,25,00,31,00,22,00,00,00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.test]

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.test\OpenWithList]
"a"="notepad++.exe"
"MRUList"="a"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.test\OpenWithProgids]
"Notepad++_file"=hex(0):

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.test\UserChoice]
"Progid"="Applications\\notepad++.exe"

Step 9. Open up regedit and delete the following keys if they exist. (Yes do it again)... Now the registry is like we never did anything and we have a registry file ready to insert into the registry!

HKEY_CURRENT_USER\Software\Classes\Applications\notepad++.exe HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.test

Step 10. If you want to apply this to the current profile then RUN THE REGISTRY FILE "original.reg" and insert it into the registry.

And that's it! Now all *.test files will open up with "Y:\%username%\programs\Notepad++\notepad++.exe". Go check the registry and see the new default entry.

like image 23
Arvo Bowen Avatar answered Oct 16 '22 21:10

Arvo Bowen