Board index » delphi » Getting Environment Variables in Delphi 1

Getting Environment Variables in Delphi 1

How do I get environmental variable values in Delphi 1?  I have a function I
use in Delphi 3 that uses ExpandEnvironmentStrings to expand environment
variables, but according to Windows.pas, this is a Kernal32 function.  I
can't seem to find that same function in Delphi 1.

Jesse

 

Re:Getting Environment Variables in Delphi 1


In article <6ig8m0$b4...@news2.ispnews.com>, "Jesse Castleberry"

Quote
<D...@iThink.net> writes:
>How do I get environmental variable values in Delphi 1?  I have a function I
>use in Delphi 3 that uses ExpandEnvironmentStrings to expand environment
>variables, but according to Windows.pas, this is a Kernal32 function.  I
>can't seem to find that same function in Delphi 1.

Look up GetEnvironmentVariable API call in Win32S.hlp.

Alan Lloyd
alangll...@aol.com

Re:Getting Environment Variables in Delphi 1


Quote
>Look up GetEnvironmentVariable API call in Win32S.hlp.

Will this work in Delphi 1 in Windows 3.1?

Jesse

Re:Getting Environment Variables in Delphi 1


Quote
Jesse Castleberry <D...@iThink.net> wrote:
>>Look up GetEnvironmentVariable API call in Win32S.hlp.
> Will this work in Delphi 1 in Windows 3.1?

Of course not. Here is some code for Delphi 1 only:

function GetEnvVar(const s:string):string;
var
  L: Word;
  P: PChar;
begin
  L := length(s);
  P := GetDosEnvironment;
  while P^ <> #0 do begin
    if (StrLIComp(P, PChar(@s[1]), L) = 0) and (P[L] = '=') then begin
      GetEnvVar := StrPas(P + L + 1);
      EXIT;
      end;
    Inc(P, StrLen(P) + 1);
    end;
  GetEnvVar := '';
  end;

Bye,
   Andy

--
SCP GmbH                 |  Andreas Hoerstemeier
 Auf der Huels 120       |  Software Developer
 52068 Aachen            |    email: a...@scp.de            (work)
 Germany                 |           a...@hoerstemeier.de (private)
phone: +49-241-96830-0   |    web:   http://www.westend.de/~hoerstemeier
fax:   +49-241-96830-50  |    ICQ:   1661968
web:   http://www.scp.de |

Re:Getting Environment Variables in Delphi 1


In article <6ig8m0$b4...@news2.ispnews.com>, Jesse Castleberry
<D...@iThink.net> writes

Quote
>How do I get environmental variable values in Delphi 1?  I have a function I
>use in Delphi 3 that uses ExpandEnvironmentStrings to expand environment
>variables, but according to Windows.pas, this is a Kernal32 function.  I
>can't seem to find that same function in Delphi 1.

{*************************}
{        GetEnvVar        }
{*************************}

{ Delphi-1 function which retrieves the value of a DOS
  environment variable }

function GetEnvVar(const VarStr: string): string;
var
    PEnv, PVarStr, PEquals: PChar;
begin
    Result := '';

    GetMem(PVarStr, Length(VarStr) + 1);

    try
        { Copy pascal-style string into a null-terminated one }
        StrPCopy(PVarStr, VarStr);

        { Get the DOS environment strings }
        PEnv := GetDOSEnvironment;

        { Step through each individual environment string and look for
          the one which is required }
        while (PEnv^ <> #0) do
        begin
            { Find the position of '=' in the string }
            PEquals := StrScan(PEnv, '=');

            if (PEquals <> nil) then
            begin
                if (StrLIComp(PEnv, PVarStr, (PEquals - PEnv)) = 0) then
                begin
                    { Found it! }
                    Inc(PEquals, 1);
                    Result := StrPas(PEquals);
                    Break;
                end; { end if }

            end; { end if }

            Inc(PEnv, StrLen(PEnv) + 1);
        end; { end while }

    finally
        FreeMem(PVarStr, Length(VarStr) + 1);
    end; { end try }

end;

Hope the above helps.
--
Steve Turner
Leeds, England

Re:Getting Environment Variables in Delphi 1


On Sat, 2 May 1998 19:01:29 -0400, "Jesse Castleberry" <D...@iThink.net>
wrote:

Quote
>How do I get environmental variable values in Delphi 1?  I have a function I
>use in Delphi 3 that uses ExpandEnvironmentStrings to expand environment
>variables, but according to Windows.pas, this is a Kernal32 function.  I
>can't seem to find that same function in Delphi 1.

Download the environ unit from http://www.tempest-sw.com/freeware/.
It is portable across all versions of Delphi.
--
Ray Lischner (http://www.tempest-sw.com/)
Author of "Hidden Paths of Delphi 3: Experts, Wizards, and the Open Tools API"

Other Threads