Article View: borland.public.cppbuilder.nativeapi
Article #16451Re: Resolving Environment Variables
From: Bruce Henderson
Date: Tue, 13 May 2003 15:49
Date: Tue, 13 May 2003 15:49
76 lines
3327 bytes
3327 bytes
Thanks for the Info!
You got me thinking about why the GetEnvironmentVariable wouldn't work... I
did a little searching and found that there is a name conflict between
Microsoft API and Borland's SysUtils. In the process though, I found a
shorter version of your GetEnvVar in a post by Jeff Overcash (TeamB):
AnsiString __fastcall MyGetEnvironmentVariable(const AnsiString Name)
{
int Len = GetEnvironmentVariable(Name.c_str(), NULL, 0);
AnsiString Result;
if (Len > 0)
{
Result.SetLength(Len - 1);
GetEnvironmentVariable(Name.c_str(), Result.c_str(), Len);
}
return Result;
}
"Giuliano" <gcardinali@remove.adhoc.net> wrote in message
news:9e22cvsvi3d21vqgf4st59e9u1cr5r4luc@4ax.com...
> 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:
<3ec11442$1@newsgroups.borland.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-mailReferences:
<3ec1031f@newsgroups.borland.com> <9e22cvsvi3d21vqgf4st59e9u1cr5r4luc@4ax.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:57:15 PST From: "Bruce Henderson" <Bruce@CreativeControlSystems.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3ec1031f@newsgroups.borland.com> <9e22cvsvi3d21vqgf4st59e9u1cr5r4luc@4ax.com> Subject: Re: Resolving Environment Variables Date: Tue, 13 May 2003 10:49:59 -0500 Lines: 78 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 216.202.195.25 Message-ID: <3ec11442$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1052841026 216.202.195.25 (13 May 2003 08:50:26 -0700) 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