🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages Started by Bruce Henderson Tue, 13 May 2003 14:36
Resolving Environment Variables
#16444
Author: Bruce Henderson
Date: Tue, 13 May 2003 14:36
5 lines
1087 bytes
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?
Re: Resolving Environment Variables
#16450
Author: 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
Re: Resolving Environment Variables
#16451
Author: Bruce Henderson
Date: Tue, 13 May 2003 15:49
76 lines
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
Re: Resolving Environment Variables
#16453
Author: Remy Lebeau (TeamB)
Date: Tue, 13 May 2003 16:39
11 lines
1252 bytes
"Bruce Henderson" <Bruce@CreativeControlSystems.com> wrote in message
news:3ec1031f@newsgroups.borland.com...
> What is the best way to resolve variables like
> %SystemRoot%, %HomeDrive%, %Temp%,
> %UserName%, etc?

Use the ExpandEnvironmentStrings() function.


Gambit
Re: Resolving Environment Variables
#16461
Author: Bruce Henderson
Date: Tue, 13 May 2003 19:33
22 lines
1588 bytes
Thanks Remy!

I stumbled upon that one too...  But I also found out that if I use
ShellExecuteEx instead of ShellExecute, there is a fMask that can be set to
SEE_MASK_DOENVSUBST, that does it automatically.


"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message
news:3ec11f4d$1@newsgroups.borland.com...
>
> "Bruce Henderson" <Bruce@CreativeControlSystems.com> wrote in message
> news:3ec1031f@newsgroups.borland.com...
> > What is the best way to resolve variables like
> > %SystemRoot%, %HomeDrive%, %Temp%,
> > %UserName%, etc?
>
> Use the ExpandEnvironmentStrings() function.
>
>
> Gambit
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads