1

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!

Warren P
  • 65,725
  • 40
  • 181
  • 316
rocksteady
  • 2,320
  • 5
  • 24
  • 40
  • Out of curiosity, try killing and restarting explorer.exe - maybe restarting the shell will force a refresh. – JosephStyons Jun 02 '16 at 13:39
  • 4
    See [How do you add a Windows environment variable without rebooting?](http://serverfault.com/a/8856). – LU RD Jun 02 '16 at 13:45
  • 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 Jun 02 '16 at 14:37
  • 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 Jun 02 '16 at 17:06
  • 2
    When you were taken to Stack Overflow, did you read the answers? Try this one: http://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 Jun 02 '16 at 17:42
  • In the end I found this one, too. Thanks for pointing it out! – rocksteady Jun 02 '16 at 17:47

1 Answers1

6

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;
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130