[fpc-pascal] SysUtils.GetEnvironmentVariable

Sven Barth pascaldragon at googlemail.com
Thu Mar 29 09:17:55 CEST 2012


Am 29.03.2012 05:31, schrieb Marcos Douglas:
> On Wed, Mar 28, 2012 at 6:40 PM, Michael Van Canneyt
> <michael at freepascal.org>  wrote:
>>
>>
>>
>> On Wed, 28 Mar 2012, Marcos Douglas wrote:
>>
>>> Hi,
>>>
>>> I think SysUtils.GetEnvironmentVariable has a problem.
>>> I tried to use GetEnvironmentVariable on WinXP and Win7. Sometimes
>>> works on WinXP, others not. But never worked on Win7.
>>>
>>> I asked some friends to test on Linux and did not work too.
>>>
>>> My FPC is 2.6.1 rev 20648 on WinXP.
>>>
>>> The test:
>>> 1- Create a Environment Variable (eg: FOO).
>>> 2- Try this:
>>>
>>> procedure TForm1.Button1Click(Sender: TObject);
>>> begin
>>>   ShowMessage(SysUtils.GetEnvironmentVariable('FOO'));
>>> end;
>>>
>>
>> Just tested.
>>
>> Works fine on linux, provided the environment variable is defined BEFORE
>> the program is started, and in the terminal that starts the program.
>> (perfectly normal on linux)
>>
>> So if you run the program under the IDE, the environment variable must be
>> defined in the session that starts the IDE, BEFORE the ide is started.
>>
>> Cannot comment on Windows, but I would be very surprised if it does not
>> work, since many of our programs rely on it.
>>
>> There again, be careful WHEN you define the environment variable. Before
>> or after starting the application.
>
> I know that I have to define the environment variable before or after
> starting the application or IDE.
> As I said, works in WinXP... but I had a little problem with a client
> using Win7 so, I talked about this on lazarus-br list and asked to
> somebody do the test on Win7 and they said the test did not work so, I
> wrote here.

You could use Process Explorer to inspect the Environment of your process.

That being said I just tested the following program:

=== source begin ===

program envtest;

uses
   SysUtils;

var
   i: LongInt;
begin
   if ParamCount = 0 then
     Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' ENVVAR [ENVVAR 
[...]]')
   else begin
     for i := 1 to ParamCount do
	  Writeln(ParamStr(i), ' = ', GetEnvironmentVariable(ParamStr(i)));
   end;
end.

=== source end ===

I tested it in a CMD session on a Windows 7:

=== output begin ===

P:\tests\oneshots>set FOO=bar

P:\tests\oneshots>.\envtest.exe FOO
FOO = bar

=== output end ===

Please note that in "cmd" the following are different:

set FOO=bar
   => "FOO" -> "bar"
set FOO = bar
   => "FOO " -> " bar"
set FOO= bar
   => "FOO" -> " bar"
set FOO =bar
   => "FOO " -> "bar"

Regards,
Sven



More information about the fpc-pascal mailing list