Simple example;
program SetEnv;
{$APPTYPE CONSOLE}
uses
Windows;
procedure SetEnvVariable(const AName, AValue: string);
begin
if Trim(AValue) = '' then
SetEnvironmentVariable(PChar(AName), nil)
else
SetEnvironmentVariable(PChar(AName), PChar(AValue));
end;
begin
SetEnvVariable('foobar', 'abcdef');
end;
Compile and run program:
C:\>SetEnv <Enter>
C:\>set foobar <Enter>
foobar=abcdes <= This is the result of "set foobar" command
Result of runing program SetEnv is same as if you execute
"set foobar=abcdef" (without ") on command prompt!!
Best regards
Branko
----- Original Message -----
From: tim11g
To: [email protected]
Sent: Saturday, August 16, 2008 7:16 PM
Subject: [delphi-en] Re: Set Environment Variable from Delphi Console App
Branko,
Thanks for the reply. A call to the SetEnvirontVariable function is
part of the SetGlobalEnvironment function I got from the link to
swissdelphicenter. Just to be sure, I made the simplest program using
only your function:
{$APPTYPE CONSOLE}
program Date2Env; {Set todays date as a string into environment}
uses
windows,messages,sysutils,registry;
var
env_var : string[40];
datestr : string[10];
procedure SetEnvVariable(const AName, AValue: string);
begin
if Trim(AValue) = '' then
SetEnvironmentVariable(PChar(AName), nil)
else
SetEnvironmentVariable(PChar(AName), PChar(AValue));
end;
begin
env_var := Paramstr(1);
if (paramcount < 1 ) then begin
writeln('Usage: Date2Env EnvVar');
halt(1);
end;
DateStr := FormatDateTime('yymmdd',Date);
writeln('Writing ',DateStr,' to env variable: "', env_var,'"');
SetEnvVariable(env_var,DateStr);
end.
Then I created a simple batch file to test it:
set f
set foobar=abcdef
set f
set foobar=
set f
Date2Env foobar
set f
Here are the results:
C:\Data\DELPHI\ConsUtil>testdate2env.bat
C:\Data\DELPHI\ConsUtil>set f
FP_NO_HOST_CHECK=NO
C:\Data\DELPHI\ConsUtil>set foobar=abcdef
C:\Data\DELPHI\ConsUtil>set f
foobar=abcdef
FP_NO_HOST_CHECK=NO
C:\Data\DELPHI\ConsUtil>set foobar=
C:\Data\DELPHI\ConsUtil>set f
FP_NO_HOST_CHECK=NO
C:\Data\DELPHI\ConsUtil>Date2Env foobar
Writing 080816 to env variable: "foobar"
C:\Data\DELPHI\ConsUtil>set f
FP_NO_HOST_CHECK=NO
The set foobar=abcdef command in the batch file does set the variable.
My program does not set the environment variable. The SET f displays
all environment variables starting with "f".
I think the problem is that Date2Env gets a private set of environment
variables when it runs. That is where the function
SetEnvironmentVariable writes to. When Date2Env ends, that private
environment is eliminated. That is why I was trying to write to the
Global Environment, which persists after a program runs. But even with
the SendMessage, which is supposed to update the Windows environment
so running processes are aware of the change, it still doesn't work
for a batch file for some reason.
Thanks,
Tim.
--- In [email protected], "Branko BURDIAN" <[EMAIL PROTECTED]>
wrote:
>
> 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
>
__________ 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]