1
votes

I use Delphi 10 Seattle on a Windows7 64bit machine.

Here is the problem:

In order to create a test environment for my software, I created a little application that adds environment variables into the Windows registry under

HKEY_CURRENT_USER\Environment

I use these variables in the search paths of my packages and the main project, in order to be able to also commit project settings into source control and to easily switch from production to test branches etc ...

Now, the problem is, that after creating the environment variable, Delphi does not update its "internal System Variable store" under

Tools --> Options --> Environment Variables

and the project does not compile.

Even restarting Delphi did not bring the desired effect. I had to reboot Windows completely, in order to make it work.

is there a way to update the System Variables in Delphi within my application? Am I missing something else?

Thanks a lot!

1
Out of curiosity, try killing and restarting explorer.exe - maybe restarting the shell will force a refresh.JosephStyons
You should read the documentation for environment variables, which tells you how to do this. It feels rather as though you are trying to reverse engineer this. Did you look for the documentation?David Heffernan
Thank you alltogether. To be honest, searching for delphi embarcadero documentation is quite tedious. I always end up using google or directly go to stackoverflow.com. In this case, I found for instance SetEnvironmentVariable() but this only sets the variable at runtime for the running process. What I need is a true system variable which lives on after I created it.rocksteady
When you were taken to Stack Overflow, did you read the answers? Try this one: stackoverflow.com/q/5898131/33732. Note that the solution is two steps, not just one. (Found by searching Google for "windows permanently set environment variable.")Rob Kennedy

1 Answers

6
votes

I am doing something similar in my Path Compressor described in this blog post. The relevant method is this one:

procedure TPathCompressor.NotifyChanges;
{ Sending a WM_SETTINGCHANGE message to all top level windows. Otherwise the new environment variables
  will only be visible after logoff/logon. }
begin
  {$IFDEF DEBUG}
  Exit;
  {$ENDIF}
  SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, NativeInt(PChar('Environment')), SMTO_ABORTIFHUNG, 5000, nil);
end;