🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16450

Re: Resolving Environment Variables

#16450
From: Giuliano
Date: Tue, 13 May 2003 15:17
53 lines
2431 bytes
On Tue, 13 May 2003 09:36:51 -0500, "Bruce Henderson"
<Bruce@CreativeControlSystems.com> wrote:

>What is the best way to resolve variables like %SystemRoot%, %HomeDrive%,
>%Temp%, %UserName%, etc?
>
>Is there an API to do this?  Do I have to pull them out of the Registry?
>What about different version of Windows?
>

Hi,

    you can use the Windows' API GetEnvironmentVariable.

For example, you can write an utility function like the following:

AnsiString GetEnvVar( AnsiString VarName )
{
    const int Len = GetEnvironmentVariable( VarName.c_str(), 0, 0 );
    if ( Len ) {
        class TBuffer {
        public:
            TBuffer( int Len ) : Buffer_( new char[Len] ) {}
            ~TBuffer() throw() { delete [] Buffer_; }
            char* const operator()( void ) const { return Buffer_; }
        private:
            TBuffer( const TBuffer& );
            TBuffer& operator=( const TBuffer& );
            char* const Buffer_;
        } Buffer( Len );

        GetEnvironmentVariable( VarName.c_str(), Buffer(), Len );
        return Buffer();
    }
    return AnsiString();
}

then using it in the following manner:

    ShowMessage( GetEnvVar( "SystemRoot" ) );

Note, you have to strip out the % signs for variable like %SystemRoot%,
%HomeDrive%, %Temp%, %UserName%, etc.

There's also a GetEnvironmentVariable at VCL level

AnsiString__fastcall GetEnvironmentVariable(const AnsiString Name);

but, AFAIK, you can't link to it with BCB6.

Hope this helps.

Giuliano
Technical Details
Message-ID: <9e22cvsvi3d21vqgf4st59e9u1cr5r4luc@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3ec1031f@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,e59ebca9d300d0e9
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-13 08:26:50 PST
From: Giuliano <gcardinali@remove.adhoc.net>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Resolving Environment Variables
Date: Tue, 13 May 2003 17:17:34 +0200
Message-ID: <9e22cvsvi3d21vqgf4st59e9u1cr5r4luc@4ax.com>
References: <3ec1031f@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.91/32.564
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 213.152.192.189
X-Trace: newsgroups.borland.com 1052839311 213.152.192.189 (13 May 2003 08:21:51 -0700)
Lines: 53
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail