Board index » delphi » Getting value of environment variable from delphi, value of PATH

Getting value of environment variable from delphi, value of PATH

In an Delphi application I would like to get the value of my
environment variable, in this case the value of my PATH variable.
Are there any functions in Delphi I can use.

Thanks in advance

 

Re:Getting value of environment variable from delphi, value of PATH


Use the GetEnvVar() function, located in the WinDOS library.

Marcus Chapman
------------------------------------------------
The information I post is wholly my own, and not
necessarily the view of my employer.
------------------------------------------------

knut.terje...@sybase.no (Knut Erik Terjesen) wrote:

Quote
>In an Delphi application I would like to get the value of my
>environment variable, in this case the value of my PATH variable.
>Are there any functions in Delphi I can use.

>Thanks in advance

Re:Getting value of environment variable from delphi, value of PATH


Marcus Chapman <mchap...@mstarlabs.com> writes

Quote
> Use the GetEnvVar() function, located in the WinDOS library.

> Marcus Chapman
> ------------------------------------------------
> The information I post is wholly my own, and not
> necessarily the view of my employer.
> ------------------------------------------------

> knut.terje...@sybase.no (Knut Erik Terjesen) wrote:
> >In an Delphi application I would like to get the value of my
> >environment variable, in this case the value of my PATH variable.
> >Are there any functions in Delphi I can use.

> >Thanks in advance

What is the WinDOS library and where do I get it?

John

-The opinions expressed here do not reflect those of my employer.
-Check out a Poi Dog Pondering on The Sea and Cake.

Re:Getting value of environment variable from delphi, value of PATH


Marcus Chapman wrote in a message to All:

MC> Use the GetEnvVar() function, located in the WinDOS library.

I've thought about wrapping this up in a class called TDosStuff, with a
property named Environment that calls the above function implicitly .... Never
had the time though... Should also include properties named Temp, Path and so
on, that calls Environment['PATH'] and so on..

Best regards,
   Christian // ctib...@silver.ct.se
             // CServe 100777,2775

... Celery raw develops a jaw. But stewed, is quietly chewed

Re:Getting value of environment variable from delphi, value of PATH


Look in the Delphi Knowledge Base, code is included!
You can find it at
  http"//www.teleport/com/~grump/delphi.htm

HTH,
Kirk

In article <1996Jan5.170607.4...@il.us.swissbank.com>,
deva...@il.us.swissbank.com says...

Quote

>Marcus Chapman <mchap...@mstarlabs.com> writes
>> Use the GetEnvVar() function, located in the WinDOS library.

>> Marcus Chapman
>> ------------------------------------------------
>> The information I post is wholly my own, and not
>> necessarily the view of my employer.
>> ------------------------------------------------

>> knut.terje...@sybase.no (Knut Erik Terjesen) wrote:
>> >In an Delphi application I would like to get the value of my
>> >environment variable, in this case the value of my PATH variable.
>> >Are there any functions in Delphi I can use.

>> >Thanks in advance

>What is the WinDOS library and where do I get it?

>John

>-The opinions expressed here do not reflect those of my employer.
>-Check out a Poi Dog Pondering on The Sea and Cake.

Re:Getting value of environment variable from delphi, value of PATH


Quote
>> Use the GetEnvVar() function, located in the WinDOS library.<<<

You'll run into problems using the WinDos unit if you do any date or time
processing.  The problem occurs because the WinDos unit is a carry over
from BP7 and the TDateTime type was defined in that unit.  In Delphi,
TDateTime is defined in the System unit and more importantly is a
different type.  

A better approach is to use the following function which does not rely on
WinDos and uses Pascal (i.e. Delphi) strings rather than PChars.

Later,
Ray
---------------------
unit DlphiDos;

interface

  function GetEnvStr( VarName : string ) : string;

implementation

  uses
    WinProcs, SysUtils;

  {=============================================================}
  {= GetEnvStr - Get Dos Environment Variable Setting          =}
  {=                                                           =}
  {= This function is a modified version of the GetEnvVar      =}
  {= function that appears in the WinDos unit that comes       =}
  {= with Delphi.  This function's interface uses Pascal       =}
  {= strings instead of null-terminated strings.               =}
  {=============================================================}

  function GetEnvStr( VarName : string ) : string;
  var
    Len     : Word;
    EnvStz  : PChar;
    NameStz : array[ 0..180 ] of Char;
  begin
    StrPCopy( NameStz, VarName );     { Covert VarName to PChar }
    Len := StrLen( NameStz );
    EnvStz := GetDosEnvironment;      { EnvStz holds entire env }

    while EnvStz^ <> #0 do
    begin                  { Pick off Variable Name and Compare }
      if ( StrLIComp( EnvStz, NameStz, Len ) = 0 ) and
         ( EnvStz[ Len ] = '=' ) then
      begin          { Convert to Pascal string before returing }
        Result := StrPas( EnvStz + Len + 1 );
        Exit;
      end;
      Inc( EnvStz, StrLen( EnvStz ) + 1 );   { Jump to Next Var }
    end;
    Result := '';
  end;

end.

Re:Getting value of environment variable from delphi, value of PATH


Quote
deva...@il.us.swissbank.com (John DeValk) wrote:
>Marcus Chapman <mchap...@mstarlabs.com> writes
>> Use the GetEnvVar() function, located in the WinDOS library.

>> Marcus Chapman
>> ------------------------------------------------
>> The information I post is wholly my own, and not
>> necessarily the view of my employer.
>> ------------------------------------------------

>> knut.terje...@sybase.no (Knut Erik Terjesen) wrote:
>> >In an Delphi application I would like to get the value of my
>> >environment variable, in this case the value of my PATH variable.
>> >Are there any functions in Delphi I can use.

>> >Thanks in advance

>What is the WinDOS library and where do I get it?

>John

>-The opinions expressed here do not reflect those of my employer.
>-Check out a Poi Dog Pondering on The Sea and Cake.

John,
  I found the WinDOS library in my Delphi10\Source directory (where it is
in your, I can't be sure).  I'm fairly sure that it comes with Delphi,
but I didn't install our copy.  Check around your source directories.  
(the actual .PAS file is located in my \Source\RTL70 directory).  

Marcus

Re:Getting value of environment variable from delphi, value of PATH


Check out the Delphi Knowledge Base, sample code and everything!
Address at in my signature!

HTH,
Kirk

In article <4cro99$...@news.halcyon.com>, mchap...@mstarlabs.com says...

Quote

>deva...@il.us.swissbank.com (John DeValk) wrote:
>>Marcus Chapman <mchap...@mstarlabs.com> writes
>>> Use the GetEnvVar() function, located in the WinDOS library.

>>> Marcus Chapman
>>> ------------------------------------------------
>>> The information I post is wholly my own, and not
>>> necessarily the view of my employer.
>>> ------------------------------------------------

>>> knut.terje...@sybase.no (Knut Erik Terjesen) wrote:
>>> >In an Delphi application I would like to get the value of my
>>> >environment variable, in this case the value of my PATH variable.
>>> >Are there any functions in Delphi I can use.

>>> >Thanks in advance

>>What is the WinDOS library and where do I get it?

>>John

>>-The opinions expressed here do not reflect those of my employer.
>>-Check out a Poi Dog Pondering on The Sea and Cake.

>John,
>  I found the WinDOS library in my Delphi10\Source directory (where it is
>in your, I can't be sure).  I'm fairly sure that it comes with Delphi,
>but I didn't install our copy.  Check around your source directories.  
>(the actual .PAS file is located in my \Source\RTL70 directory).  

>Marcus

--
___________________________________________________________________________
Kirk Wolak
KWo...@mail.cbf.com
//
// Const ViewsExpressed = "My own and not those of the company I work for!";
//
Look at the Delphi Knowledge Base for answers to your questions...
  http://www.teleport.com/~grump/delphi.htm

Re:Getting value of environment variable from delphi, value of PATH


In article <1996Jan5.170607.4...@il.us.swissbank.com> deva...@il.us.swissbank.com (John DeValk) writes:

Quote
>From: deva...@il.us.swissbank.com (John DeValk)
>Subject: Re: Getting value of environment variable from delphi, value of PATH
>Date: Fri, 5 Jan 1996 17:06:07 GMT
>Marcus Chapman <mchap...@mstarlabs.com> writes
>> Use the GetEnvVar() function, located in the WinDOS library.

>> Marcus Chapman
>> ------------------------------------------------
>> The information I post is wholly my own, and not
>> necessarily the view of my employer.
>> ------------------------------------------------

>> knut.terje...@sybase.no (Knut Erik Terjesen) wrote:
>> >In an Delphi application I would like to get the value of my
>> >environment variable, in this case the value of my PATH variable.
>> >Are there any functions in Delphi I can use.

>> >Thanks in advance

>What is the WinDOS library and where do I get it?
>John
>-The opinions expressed here do not reflect those of my employer.
>-Check out a Poi Dog Pondering on The Sea and Cake.

This is the GetEnvVar from WinDos:

function GetEnvVar(VarName: PChar): PChar;
var
  L: Word;
  P: PChar;
begin
  L := StrLen(VarName);
  P := GetDosEnvironment;
  while P^ <> #0 do
  begin
    if (StrLIComp(P, VarName, L) = 0) and (P[L] = '=') then
    begin
      GetEnvVar := P + L + 1;
      Exit;
    end;
    Inc(P, StrLen(P) + 1);
  end;
  GetEnvVar := nil;
end;

Use like this:

var   DosPath : string;
...
  DosPath := StrPas(GetEnvVar('PATH'));

Tibor Liska

Other Threads