Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3357640
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:39:25+00:00 2026-05-18T02:39:25+00:00

(This is not so much a problem as an exercise in pedantry, so here

  • 0

(This is not so much a problem as an exercise in pedantry, so here goes.)

I’ve made a nice little program that is native to my linux OS, but I’m thinking it’s useful enough to exist on my Windows machine too. Thus, I’d like to access Windows’ environment variables, and MSDN cites an example like this:

const DWORD buff_size = 50;
LPTSTR buff = new TCHAR[buff_size];

const DWORD var_size = GetEnvironmentVariable("HOME",buff,buff_size);

if (var_size==0) { /* fine, some failure or no HOME */ }
else if (var_size>buff_size) {

    // OK, so 50 isn't big enough.
    if (buff) delete [] buff;
    buff = new TCHAR[var_size];
    
    const DWORD new_size = GetEnvironmentVariable("HOME",buff,var_size);

    if (new_size==0 || new_size>var_size) { /* *Sigh* */ }
    else { /* great, we're done */ }
}
else { /* in one go! */ }

This is not nearly as nice (to me) as using getenv and just checking for a null pointer. I’d also prefer not to dynamically allocate memory since I’m just trying to make the program run on Windows as well as on my linux OS, which means that this MS code has to play nicely with nix code. More specifically:

template <class T> // let the compiler sort out between char* and TCHAR*
inline bool get_home(T& val) { // return true if OK, false otherwise
#if defined (__linux) || (__unix)
    val = getenv("HOME");
    if (val) return true;
    else return false;
#elif defined (WINDOWS) || defined (_WIN32) || defined (WIN32)
    // something like the MS Code above
#else
    // probably I'll just return false here.
#endif
}

So, I’d have to allocate on the heap universally or do a #ifdef in the calling functions to free the memory. Not very pretty.

Of course, I could have just allocated ‘buff’ on the stack in the first place, but then I’d have to create a new TCHAR[] if ‘buff_size’ was not large enough on my first call to GetEnvironmentVariable. Better, but what if I was a pedant and didn’t want to go around creating superfluous arrays? Any ideas on something more aesthetically pleasing?

I’m not that knowledgeable, so would anyone begrudge me deliberately forcing GetEnvironmentVariable to fail in order to get a string size? Does anyone see a problem with:

const DWORD buff_size = GetEnvironmentVariable("HOME",0,0);
TCHAR buff[buff_size];
const DWORD ret = GetEnvironmentVariable("HOME",buff,buff_size);
// ...

Any other ideas or any suggestions? (Or corrections to glaring mistakes?)

UPDATE:
Lots of useful information below. I think the best bet for what I’m trying to do is to use a static char[] like:

inline const char* get_home(void) { // inline not required, but what the hell.
#if defined (__linux) || (__unix)
    return getenv("HOME");
#elif defined (WINDOWS) || defined (WIN32) || defined (_WIN32)
    static char buff[MAX_PATH];
    const DWORD ret = GetEnvironmentVariableA("USERPROFILE",buff,MAX_PATH);
    if (ret==0 || ret>MAX_PATH)
        return 0;
    else
        return buff;
 #else
        return 0;
 #endif
 }

Perhaps it’s not the most elegant way of doing it, but it’s probably the easiest way to sync up what I want to do between *nix and Windows. (I’ll also worry about Unicode support later.)

Thank you to everybody who has helped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:39:26+00:00Added an answer on May 18, 2026 at 2:39 am
    DWORD bufferSize = 65535; //Limit according to http://msdn.microsoft.com/en-us/library/ms683188.aspx
    std::wstring buff;
    buff.resize(bufferSize);
    bufferSize = GetEnvironmentVariableW(L"Name", &buff[0], bufferSize);
    if (!bufferSize)
        //error
    buff.resize(bufferSize);
    

    Of course, if you want ASCII, replace wstring with string and GetEnvironmentVariableW with GetEnvironmentVariableA.

    EDIT: You could also create getenv yourself. This works because

    The same memory location may be used in subsequent calls to getenv, overwriting the previous content.

    const char * WinGetEnv(const char * name)
    {
        const DWORD buffSize = 65535;
        static char buffer[buffSize];
        if (GetEnvironmentVariableA(name, buffer, buffSize))
        {
            return buffer;
        }
        else
        {
            return 0;
        }
    }
    

    Of course, it would probably be a good idea to use the wide character versions of all of this if you want to maintain unicode support.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The current top-voted to this question states: Another one that's not so much a
This question is not so much programming related as it is deployment related. I
This is something I'm not much consistent about and always curious about what other
Just could not get this one and googling did not help much either.. First
I'm sorry for this very newbish question, I'm not much given into web development.
I know this isn't a unique issue but I've not had much luck finding
Not certain if this will get much response due to the newness of Windows
I'm not sure if this has been asked or not yet, but how much
Sorry for this not being a real question, but Sometime back i remember seeing
Under what circumstances would this or would this not be safe? I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.