[RESOLVED] _environ
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] _environ

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,437

    [RESOLVED] _environ

    I'm working with some code that uses the environment array _environ. It contains a few sections which look like this:-

    Code:
            char** the_environ = _environ;
    
            for (size_t i = 0; the_environ[i]; ++i) {
    			
                    string estring = the_environ[i];
    			
                    // Do some other stuff
            }
    What's happening is that the loop is stepping off the end of the array. At first I thought the solution was obvious. It seemed to me that ++i should really be i++. Unfortunately, even after changing that, I'm still having problems. Let's say there are 16 elements in the environment array. It seems to me that element the_environ[16] isn't guaranteed to be NULL. For example if there were originally 17 entries and then I delete one of them, the_environ[16] seems to get left as a bad pointer.

    Is there a better way to get the number of available environment strings?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,120

    Re: _environ

    See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Code:
    LPSTR lpszVariable; 
    LPCH lpvEnv; 
     
        // Get a pointer to the environment block. 
        lpvEnv = GetEnvironmentStrings();
    
        // If the returned pointer is NULL, exit.
        if (lpvEnv == NULL)
        {
            printf("GetEnvironmentStrings failed (%d)\n", GetLastError()); 
            return 0;
        }
     
        // Variable strings are separated by NULL byte, and the block is 
        // terminated by a NULL byte. 
    
        lpszVariable = (LPSTR) lpvEnv;
    
        while (*lpszVariable)
        {
            printf(TEXT("%s\n"), lpszVariable);
            lpszVariable += lstrlen(lpszVariable) + 1;
        }
        FreeEnvironmentStrings(lpvEnv);
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. This post is public domain as specified at https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,437

    Re: _environ

    Thanks 2kaud. I should have mentioned that I need to keep the solution portable if possible - so something cross-platform would be preferable.

    [Edit...] this turned out to be due to a logic problem later in the 'for' loop (it was deleting an environment string - but not taking account of the fact that the number of environment strings was now less).
    Last edited by John E; April 2nd, 2014 at 10:19 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,806

    Re: _environ

    Quote Originally Posted by John E View Post
    Thanks 2kaud. I should have mentioned that I need to keep the solution portable if possible - so something cross-platform would be preferable.
    Then you should have posted this question in the non-Visual C++ forum.
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,552

    Re: [RESOLVED] _environ

    Code:
            for (char** the_environ = _environ; *the_environ; the_environ++) {            
                    string estring = *the_environ;            
                    // Do some other stuff
            }
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center