0

I've seen various posts on refreshing powershell and cmd terminals, but I'm not sure if there is also a means to update the environment variables of an already running process (outside of restarting it) that's not either powershell or cmd. These posts were all fairly helpful in helping me understand how things work behind the scenes:

https://superuser.com/questions/130029/refresh-environment-variable

https://www.michaelmiklis.de/update-environment-variables-in-current-process/

And this post (same question but in relation to linux) seems to indicate there is a means in linux:

Changing environment variable of a running process

I'm curious if theres a similar method in Windows. My assumption is no, you have to restart, but I can't find an explicit no. Why I'd like to do this is because I'm working on a system that tests hardware. The top level tool is a sequencer that's capable of calling various code modules (python, .net, .exes, etc). From within the this top level tool, I'm calling a python module that expects a specific environment variable be set. However, I won't know what this variable should be until a bar code is scanned. I've written C# code that can set an environment variable, and check if one is set, but since the top level process hasn't been made aware of the change it's not really possible for the python module to have the correct variable value unless it's somehow known before everything is started.

hallibut
  • 136
  • 1
  • 12
  • Environment variables are set in the process environment block at process creation time. There is no supported way of changing this once a process has been created. If you are creating a process, you can provide a custom environment in a call to [`CreateProcess`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw). – IInspectable Jan 04 '22 at 09:20
  • 1
    The previous comment is wrong in part: You can change a process' environment at runtime, either a single variable ([`SetEnvironmentVariable`](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentvariablew)), or the entire set of environment variables ([`SetEnvironmentStrings`](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-setenvironmentstringsw)). You can only change the environment from within a process. – IInspectable Jan 04 '22 at 12:05

1 Answers1

2

When a program changes any environment variable, it is supposed to let other processes know as follows (I think there are multiple ways to send this message)

     status2 = (DWORD)SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)NULL,SMTO_ABORTIFHUNG, 10, &result2);

All processes should listen for this message, and reload the environment variable(s) in response. MSVC2019 Professional can add as many as 16(+?) very long paths to the Path user level environment variable. Be aware that Windows 10 has a 2047 character limit in its System->Advanced->Environment variable->User Level dialog. This is causing me grief because it looks like paths beyond the limit are ignored. The GUI will not allow changes when the limit is exceeded.

jelly14
  • 31
  • 2
  • What language and context would one implement the snippet of code you shared? When I create an environment variable, I'm doing so via C#. Can I send this message from that code or should I write something else to do so? – hallibut Jan 03 '22 at 19:38
  • 3
    Your code is incorrect, lparam should be "Environment". Explorer.exe is the only process that cares about this message. – Anders Jan 03 '22 at 20:31