Setting environment variables requires reboot on 64-bit
Asked Answered
R

2

29

I am working on an installer using Wix 3.5 that needs to set the system PATH environment variable.

This is how I am setting the environment variable:

<Directory Id="DirectoryName">
    <Component Id="ComponentID" Guid="{BE20AF67-5943-4AF4-BE66-226E2D4B844F}">
        <Environment Id="EnvironmentID" Name="PATH" Action="set" Value="the path" Part="last" Separator=";" System="yes" />
    </Component>
</Directory>

This seems to be working on 32-bit systems without requiring a reboot for the changes to be recognized. However, when I try it on 64-bit Windows 7 a reboot is required.

Is this a known issue on 64-bit systems?

Maybe the best approach would be to schedule a reboot to cover all bases.

Thanks, Alan

Reckless answered 8/8, 2011 at 8:57 Comment(3)
Is your installer 64 bit? If it's not, the update value for PATH maybe written into WOW64 branch of the registry instead of 64 bit one.Hereby
Thanks for your response. The installer was built as 64-bit. The symptoms we experienced were that the Windows GUI reported the updated PATH variable but the command prompt did not. Restarting the machine resolved it. Is this what you would expect if it was in the WOW64 branch?Reckless
No, I would not expect that. If it's shown in the Windows GUI, then it written to the right place. But I think MSI does not call a function so that the system re-reads the environment variables. When you add or change a variable using registry directly, they're not automatically re-read; Windows GUI calls a function or something when you click OK, and new processes inherit the updated values.Hereby
H
30

Check this question on ServerFault: How do you add a Windows environment variable without rebooting?

So to propagate the change to the list of environment variables, you can write a small program which broadcasts WM_SETTINGCHANGE message as described in KB article How to propagate environment variables to the system.

Hereby answered 8/8, 2011 at 16:36 Comment(2)
Thanks Alexey. This looks like the answer.Reckless
@Alan You're welcome. I'm not sure why Windows Installer behaves differently. You can test it by writing a program which handles WM_SETTINGCHANGE message.Hereby
S
7

When you add or set an environment variable, a WM_SETTINGCHANGE message is sent to all programs to inform them of the change. However, any already running program will not get the updated environment, unless it can handle this message itself. Rebooting the system updates every program.

A workaround without rebooting:

  1. Kill and restart explorer.exe, allthough this does not work for every running process, and only for the current logged on user.
  2. Restart the process or program you want to use, ie. try this out with cmd.exe. Again, this only works for the logged on user.

So summing-up, in order to get this to work for every user, you still need to reboot.

Streamline answered 11/2, 2016 at 9:17 Comment(1)
Restarted the program that could not find the new environment variable (workaround 2 above), and this worked.Renal

© 2022 - 2024 — McMap. All rights reserved.