[fpc-pascal] SysUtils.GetEnvironmentVariable

Chad Berchek ad100 at vobarian.com
Fri Mar 30 02:41:26 CEST 2012


Works fine for me on Windows 7. Here's the Windows version details:

Windows 7 Pro, 64-bit, with Service Pack 1. The "ver" command shows 
6.1.7601.

I tested 3 cases:
1. Setting the environment variable using the "set" command in a command 
line prompt then immediately running the test program.
2. Setting a user-specific environment variable in the System control 
panel GUI.
3. Setting a machine-wide environment variable in the System control 
panel GUI.

Note: One thing to keep in mind is that programs get their copy of the 
environment when they start. So if you add or change a variable after a 
program has started, that program will likely not see the change until 
it is restarted, and nor would any child processes it starts. For 
example if you open a command line and THEN set an environment variable 
through another program (like the control panel GUI), it will not be 
visible in that command line or any programs you run from it. You'd have 
to start a new command line after you add/change your variable. I 
believe this has been the way programs work since the days of DOS. I.e., 
they get their copy of the environment block when they first start.

For reference here is the test program, compiled with FPC 2.4.2 on Win 
XP Pro 64-bit (as I don't actually have FPC on my Win 7 machine). Here 
is the version output line from the compiler:

Free Pascal Compiler version 2.4.2 [2011/03/08] for i386

and the program:

-----
program getenvtest;

{$mode objfpc}{$H+}

uses
   SysUtils;

begin
   WriteLn(GetEnvironmentVariable('TestVar'));
end.
-----


On 3/28/2012 4:18 PM, 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;
>
> Marcos Douglas
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>




More information about the fpc-pascal mailing list