Recent

Author Topic: Problems using GetEnvironmentVariable  (Read 10330 times)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Problems using GetEnvironmentVariable
« on: June 18, 2017, 11:51:13 am »
Hello, I am trying to use the SysUtils function GetEnvironmentVariable in an LCL GUI Application, but the function always returns #0. Yet, if I use the function in a simple console application, it returns the correct value. Can anyone help?
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Problems using GetEnvironmentVariable
« Reply #1 on: June 18, 2017, 12:02:31 pm »
Can anyone help?
Not without example code.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problems using GetEnvironmentVariable
« Reply #2 on: June 18, 2017, 12:16:12 pm »
Okay...

This works:

Code: Pascal  [Select][+][-]
  1. program envvar_console;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes,
  10.   SysUtils
  11.   { you can add units after this };
  12.  
  13. var
  14.   v: string;
  15. begin
  16.   v := GetEnvironmentVariable('GOPATH');
  17.   WriteLn(v);
  18. end.
  19.  
           

and this doesn't:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.   public
  20.     { public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   S: string;
  35. begin
  36.   S := SysUtils.GetEnvironmentVariable('GOPATH');
  37.   Label1.Caption := S;
  38. end;
  39.  
  40. end.                                    
  41.  

The project options are set to default in both cases.

Thanks in advance for any help.
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Problems using GetEnvironmentVariable
« Reply #3 on: June 18, 2017, 12:29:37 pm »
The code looks correct. It means the actual environment is different. Maybe it depends on how you start the application. From console / desktop etc.
You did not mention your OS but I assume Windows.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Problems using GetEnvironmentVariable
« Reply #4 on: June 18, 2017, 12:33:18 pm »
The code looks correct. It means the actual environment is different. Maybe it depends on how you start the application. From console / desktop etc.
You did not mention your OS but I assume Windows.
The question is in the subforum "Mac OS X", therefore I think it refers to macOS.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Problems using GetEnvironmentVariable
« Reply #5 on: June 18, 2017, 12:35:54 pm »
Hello, I am trying to use the SysUtils function GetEnvironmentVariable in an LCL GUI Application, but the function always returns #0. Yet, if I use the function in a simple console application, it returns the correct value. Can anyone help?

Are you sure that the GOPATH environment variable is globally set?

If I use your code with other environment variables, e. g. USER, it works correctly, even in an LCL app.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problems using GetEnvironmentVariable
« Reply #6 on: June 18, 2017, 12:36:30 pm »
I'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra

I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus.
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Problems using GetEnvironmentVariable
« Reply #7 on: June 18, 2017, 12:37:17 pm »
I'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra

I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus.

Did you try the code with the USER environment variable?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problems using GetEnvironmentVariable
« Reply #8 on: June 18, 2017, 12:38:17 pm »
The code looks correct. It means the actual environment is different. Maybe it depends on how you start the application. From console / desktop etc.
You did not mention your OS but I assume Windows.
The question is in the subforum "Mac OS X", therefore I think it refers to macOS.

Well spotted  :D
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problems using GetEnvironmentVariable
« Reply #9 on: June 18, 2017, 12:45:18 pm »
I'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra

I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus.

Did you try the code with the USER environment variable?

Hmmm... I've just tried with the USER environment variable and that works in both cases. There must be something odd about the GOPATH variable.
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4468
  • I like bugs.
Re: Problems using GetEnvironmentVariable
« Reply #10 on: June 18, 2017, 12:47:39 pm »
The question is in the subforum "Mac OS X", therefore I think it refers to macOS.
Ooops, sorry. My bad.
I don't know much about macOS.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Problems using GetEnvironmentVariable
« Reply #11 on: June 18, 2017, 12:53:44 pm »
Just for fun, I have made a mini project that shows the contents of arbitrary environment variables as read by the LCL. It works well on macOS (and probably any other platform). See attachment.

Unlike your code, it uses the function GetEnvironmentVariableUTF8 from the LazUTF8 unit. You have to add LazUTF8 in the uses section of your code.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problems using GetEnvironmentVariable
« Reply #12 on: June 18, 2017, 01:02:08 pm »
I'm running Lazarus 1.6.4 + FPC 3.0.2 on macOS 10.12.5 Sierra

I'm building both apps within Lazarus. The command line app works when run both within Lazarus and from the command line. I have only tried the GUI app from within Lazarus.

Did you try the code with the USER environment variable?

Hmmm... I've just tried with the USER environment variable and that works in both cases. There must be something odd about the GOPATH variable.

The GOPATH environment variable is defined in the fish.config file for the Fish shell that I am using for Golang development.

It is defined with the following line:

set -x GOPATH $HOME/Code/go

This all happens at the shell level. It seems odd that the variable is available in the console app but not the GUI app. I would have thought it would be an "All or nothing" type situation. Presumably, the GetEnvironmentVariable function is behaving differently in the different environments. The situation is made more difficult by the problems inherent in debugging in Lazarus on the Apple Mac, although I know that Xcode can be used - it's just a bit fiddly!
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

carl_caulkett

  • Sr. Member
  • ****
  • Posts: 306
Re: Problems using GetEnvironmentVariable
« Reply #13 on: June 18, 2017, 01:12:45 pm »
Just for fun, I have made a mini project that shows the contents of arbitrary environment variables as read by the LCL. It works well on macOS (and probably any other platform). See attachment.

Unlike your code, it uses the function GetEnvironmentVariableUTF8 from the LazUTF8 unit. You have to add LazUTF8 in the uses section of your code.

I've tried your project and while it works for environment variables such as USER and PATH, it doesn't work for GOPATH. Presumably, the GOPATH variable is not a "Proper" environment variable, though what does actually constitute a "Proper" environment variable is beyond me.

I may have to risk humiliation and consult the almighty beings of StackOverflow in order to find out, though I was hoping to keep things within the warm and cosy confines of the Lazarus forums!
"It builds... ship it!"

Mac Mini M1
macOS 13.6 Ventura
Lazarus 2.2.6 (release version)
FPC 3.2.2 (release version)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Problems using GetEnvironmentVariable
« Reply #14 on: June 18, 2017, 01:28:42 pm »
The difference may result from the fact that you launch CLI applications made with FPC from the shell, but GUI applications based on the LCL from the Finder.

For permanent storage of Environment variables, macOS uses two approaches: Environment variables for use by the shell and CLI programs are stored in ~/.bash_profile (you may want to type more ~/.bash_profile to display the current contents of this file on your computer), while environment variables for GUI applications are stored in ~/.MacOSX/environment.plist (for Mac OS X Lion and Mountain Lion only) or in /etc/launchd.conf (for newer versions of macOS).

Additionally, the syntax is slightly different: In ~/.bash_profile the environment variables are set with

Code: Bash  [Select][+][-]
  1. export PATH=$PATH:/Library/PostgreSQL/8.4/bin

but with

Code: Bash  [Select][+][-]
  1. setenv JRUBY_HOME /Applications/Dev/jruby

in /etc/launchd.conf.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018