Board index » delphi » Getting environment variable in delphi

Getting environment variable in delphi

How do I get the value of an environment variable without using the
windows API call?

 

Re:Getting environment variable in delphi


I'm not sure if there's a "native delphi function" that will let you
retreive environment variables.
If this is the case, you would either have to use Win API, direct memory
access -- Ptr( Word( Ptr( PrefixSeg, $2C)^ ), 0 ), interrupt, or some
other hack. I'd use something simple like:

function GetEnvVar( sVarName: String ): String;
const
        cnMaxVarValueLen        = 255;
var
        ps : PChar;
begin
        ps      := StrAlloc( cnMaxVarValueLen+1 );
        GetEnvironmentVariableA( PChar( sVarName ), ps, cnMaxVarValueLen );
        result  := StrPas( ps );
        StrDispose( ps );
end;

--
Chami's Place
http://www.chamisplace.com/

Quote
> "Liran" <li...@commedia.pixel.co.il> wrote in article

<01bb6bdc.05172280$610d5...@liran.pixel.co.il>...
Quote
> How do I get the value of an environment variable without using the
> windows API call?

Re:Getting environment variable in delphi


function GetEnvVar( sVarName: String ): String;
const
        cnMaxVarValueLen        = 255;
var
        ps : PChar;
begin
       ps      := StrAlloc( cnMaxVarValueLen+1 );
       GetEnvironmentVariableA( PChar( sVarName ), ps,
cnMaxVarValueLen);
       result  := StrPas( ps );
       StrDispose( ps );
end;

Although the above code works fine, I was disappointed that it does not
work like in c++ stdin.  It is unable to retrieve env variables that are
part of the internet cgi specs.  If someone knows why this
implementation of Delphi 2.0 is unable to do this or if there is another
way of getting the information, I would be very happy to know.

Other Threads