Page 1 of 1

envget()

PostPosted: Thu Oct 24, 2019 4:48 pm
by Joseph Baron
I have a need to use DCAL envget(), which seems simple enough, except I have not been able to get it to return any values.

Does anyone know the trick to get this to return environment values?

Thanks in advance!

Re: envget()

PostPosted: Fri Oct 25, 2019 3:48 pm
by dhs
Hi Joseph,

Have just tried envget() myself and get the same result as you. Not particularly surprised by this.

As the DCAL function does not work my workaround would be to write a small Delphi program to get the variable and to call that program from the DCAL macro. Not a particularly elegant approach, but it can provide the required information. I have written a quick example of this which appears to work ok (although you may wish to implement some error checking etc if you choose to implement this approach).

My Delphi Code:
Code: Select allprogram EnvGet;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

var
  env : string;
  envFile : TextFile;
begin
   env := GetEnvironmentVariable (ParamStr(1));   // get env variable using first command line parameter
   AssignFile(envFile, 'Env.txt');
   ReWrite(envFile);
   WriteLn(envFile, env);     // write env variable value to Env.txt file
   CloseFile(envFile);
end.

My code for a DCAL macro to call the above program:
Code: Select allPROGRAM GetEnv;

PROCEDURE execAndWait ( prog : str255; Visibility: integer; returnCode : OUT integer); BUILTIN 632;

VAR
    s : str255;
    i : integer;
    fl : file;

BEGIN
   REPEAT
       WrtMsg ('Env Variable Name:');
       s :=  '';
       GetStr (s, 255);
       if strlen(s) > 0 then
           strins (s, 'EnvGet.exe ', 1);    ! s now has 'EnvGet.exe' followed by a space followed by
                                            ! env variable name  ... env variable is effectively the
                                            ! first command line parameter
           ExecAndWait (s, 0, i);    ! call EnvGet.exe
           i := f_open (fl, 'Env.txt', true, fmode_read);    ! EnvGet.exe writes result to Env.txt file, so
                                                             ! read that file to get result
           i := f_rdstr (fl, s);
           i := f_close (fl);
           WrtMsg ('Env Variable Value:');
           getstr (s, 255);
       end;
   UNTIL strlen (s) = 0;      ! keep processing until user leaves variable name blank
END GetEnv.



Have attached the compiled versions of the above for anybody that is interested.

Re: envget()

PostPosted: Fri Oct 25, 2019 4:27 pm
by Joseph Baron
Wow thank you very much David. It's time I start learning this D4D stuff, thank you so much for the help.

Re: envget()

PostPosted: Sat Nov 09, 2019 3:57 pm
by dhs
Have made a couple of refinements to my Delphi envget program:

It will now accept a second parameter to specify the destination file (which could include a fully qualified path if required).
I'm a bit hesitant in recommending the use of this second parameter as I don't think the method of adding parameters using ExecAndWait will handle spaces very well (if at all), but it should handle file names without any spaces without problem.

The other refinement is to ensure that the Env.txt file is written to the same directory where the EnvGet.exe resides (in the event that parameter 2 is not specified).

Here is the source of the new version:
Code: Select allprogram EnvGet;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

var
  env : string;
  envFile : TextFile;
  envFileName : string;
begin
   env := GetEnvironmentVariable (ParamStr(1));   // get env variable using first command line parameter
   if length (ParamStr(2)) > 0 then
     envFileName := ParamStr(2)      // use param 2 as output file if it exists
   else
    envFileName := ExtractFilePath(ParamStr(0)) + 'Env.txt';      // write env.txt to thefile path of exe
   AssignFile (envFile, envFileName);
   ReWrite(envFile);
   WriteLn(envFile, env);     // write env variable value to output file
   CloseFile(envFile);
end.


Compiled version is attached. You are free to use this if you wish (as with all my offerings there is absolutely no obligation but a contribution towards cost of development would be appreciated if you find it useful)