Sunday, March 4, 2012

Environment Variables In C++ - Stack Overflow

Accessing Environment Variables In C++ - Stack Overflow:

I'd like to have access to the $HOME environment variable in a C++ program that I'm writing. If I were writing code in C, I'd just use the getenv() function, but I was wondering if there was a better way to do it. Here's the code that I have so far:
std::string get_env_var( std::string const & key ) {                                
  char * val;                                                                       
  val = getenv( key.c_str() );                                                      
  std::string retval = "";                                                          
  if (val != NULL) {                                                                
    retval = val;                                                                   
  return retval;                                                                       }
Should I use getenv() to access environment variables in C++? Are there any problems that I'm likely to run into that I can avoid with a little bit of knowledge?
link|improve this question

I think you meant to return retval.Ferruccio Mar 10 '09 at 18:34
Other than the error that Ferruccio pointed out, this looks correct to me.grieve Mar 10 '09 at 18:39

4 Answers

up vote19down voteaccepted
There is nothing wrong with using getenv() in C++. It is defined by stdlib.h, or if you prefer the standard library implementation, you can include cstdlib and access the function via the std:: namespace (i.e., std::getenv()). Absolutely nothing wrong with this. In fact, if you are concerned about portability, either of these two versions is preferred.
If you are not concerned about portability and you are using managed C++, you can use the .NET equivalent - System::Environment::GetEnvironmentVariable(). If you want the non-.NET equivalent for Windows, you can simply use the GetEnvironmentVariable() Win32 function.
link|improve this answer

Why use GetEnvironmentVariable in Windows, from MSDN getenv:
getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.
This function can retrieve either a system environment variable or a user environment variable.
link|improve this answer
That's a curious comment about getenv() from MSDN. Any idea what it really means (ie., what and when these differences might be)?Michael Burr Mar 10 '09 at 19:26
I don't know exactly but I would recommend to use GetEnvironmentVariable on windows. It's always better to use the Win32 API.Brian R. Bondy Mar 10 '09 at 20:05
This means that environment variables set using SetEnvironmentVariable are not accessable to genenv after the process has started.Bob9630 Jan 19 at 22:51

In the c++ you have to use std::getenv and to include
link|improve this answer

I would just refactor the code a little bit:
std::string getEnvVar( std::string const & key ) const
    char * val = getenv( key.c_str() );
    return val == NULL ? std::string("") : std::string(val);
link|improve this answer

No comments:

Post a Comment