Set Environment setting

I call the following function in my application, but it does not seem to 
take effect in the program until I exit and execute the program again. The 
change however is done when viewing it in Windows environment variables. But 
not instantly in the program. How do I fix this?

procedure SetUserEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    OpenKey('Environment', False);
    WriteString(name, value);
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, LParam
      (PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv);
  finally
    Free
  end
end;
0
Andy
10/13/2011 4:48:50 AM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

2 Replies
1318 Views

Similar Articles

[PageSpeed] 46

"Andy Colmes" <andycolmes@yahoo.com> wrote in message 
news:410582@forums.embarcadero.com...

> I call the following function in my application, but it does not
> seem to take effect in the program until I exit and execute the
> program again. The change however is done when viewing it
> in Windows environment variables. But not instantly in the
> program.

Environment variables are loaded when the process is first created.  They 
are not tied to the WM_SETTINGCHANGE message.

> How do I fix this?

Call the Win32 API SetEnvironmentVariable() function to update the calling 
process, eg:

{code:delphi}
procedure SetUserEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    if not OpenKey('Environment', False) then Exit;
    WriteString(name, value);
  finally
    Free;
  end;
  SetEnvironmentVariable(PChar(name), PChar(value));
  SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 
LParam(PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv);
end;
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
10/13/2011 5:28:06 PM
Thanks very much Remy. That helped.

Cheers

Andy

"Remy Lebeau (TeamB)"  wrote in message 
news:410756@forums.embarcadero.com...

"Andy Colmes" <andycolmes@yahoo.com> wrote in message
news:410582@forums.embarcadero.com...

> I call the following function in my application, but it does not
> seem to take effect in the program until I exit and execute the
> program again. The change however is done when viewing it
> in Windows environment variables. But not instantly in the
> program.

Environment variables are loaded when the process is first created.  They
are not tied to the WM_SETTINGCHANGE message.

> How do I fix this?

Call the Win32 API SetEnvironmentVariable() function to update the calling
process, eg:

{code:delphi}
procedure SetUserEnvironmentVariable(const name, value: string);
var
  rv: DWORD;
begin
  with TRegistry.Create do
  try
    RootKey := HKEY_CURRENT_USER;
    if not OpenKey('Environment', False) then Exit;
    WriteString(name, value);
  finally
    Free;
  end;
  SetEnvironmentVariable(PChar(name), PChar(value));
  SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
LParam(PChar('Environment')), SMTO_ABORTIFHUNG, 5000, rv);
end;
{code}

-- 
Remy Lebeau (TeamB)
0
Andy
10/13/2011 5:57:20 PM
Reply: