Uses
    Windows, ...;

...

procedure SetEnvVariable(const AName, AValue: string);
begin
  if Trim(AValue) = '' then
    SetEnvironmentVariable(PChar(AName), nil)
  else
    SetEnvironmentVariable(PChar(AName), PChar(AValue));
end;
...

Best regards
Branko

----- Original Message ----- 
  From: tim11g 
  To: delphi-en@yahoogroups.com 
  Sent: Saturday, August 16, 2008 4:40 PM
  Subject: [delphi-en] Set Environment Variable from Delphi Console App


  I am trying set environment variables that can be used in an XP
  command batch file by other programs from within my console app:

  MyDelphiApp.exe MyParameter
  AnotherApp.exe c:\somepath\%myEnvVar%\somefile.xxx

  The object is for MyDelphiApp to set environment variables such as
  "MyEnvVar" in a way that can be used by other programs in the batch,
  or on the command line such as %myEnvVar% in my example.

  I tried the technique showed here:
  http://www.swissdelphicenter.ch/torry/showcode.php?id=1394, but the
  registry-based changes do not affect a running batch file in a CMD
  window (only after reboot, or logoff/logon). In addition, the CMD
  window does not seem to respond to the SendMessage(HWND_BROADCAST....
  that is supposed to update the current environment. Placing a SET
  command before and after MyDelphiApp.exe shows no change in the
  environment. 

  If a simple SET command in a batch file can set an environment
  variable, isn't there an API from a program that can do the same
  thing? I know this was possible in the TP/DOS days - was it eliminated
  with NT/2K/XP? 

  For now, I'm using a workaround where MyDelphiApp writes a new batch
  file containing SET commands, which is then called by the main batch
  file. It's ugly, but seems to work. I would still like to understand
  how to properly write environment variables from a program. 



   

  __________ NOD32 3360 (20080815) Informacija __________

  To sporoèilo je preveril protivirusni sistem NOD32.
  http://www.nod32.com


[Non-text portions of this message have been removed]

Reply via email to