Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Read a User/System Environment Variable Using c++

How to Read a User/System Environment Variable Using c++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialworkspace
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mogaambo
    wrote on last edited by
    #1

    How to Read a User/System Environment Variable Using c++. getenv is not working.

    “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

    M M V 3 Replies Last reply
    0
    • M Mogaambo

      How to Read a User/System Environment Variable Using c++. getenv is not working.

      “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

      M Offline
      M Offline
      Murugesan G
      wrote on last edited by
      #2

      for Visual C++ try this otherwise getenv will work using namespace System; using namespace System::Collections; int main() { Console::WriteLine(); Console::WriteLine( "GetEnvironmentVariables: " ); IDictionary^ environmentVariables = Environment::GetEnvironmentVariables(); IEnumerator^ myEnum = environmentVariables->GetEnumerator(); while ( myEnum->MoveNext() ) { DictionaryEntry^ de = safe_cast<dictionaryentry^>(myEnum->Current); Console::WriteLine( " {0} = {1}", de->Key, de->Value ); } }

      S 1 Reply Last reply
      0
      • M Mogaambo

        How to Read a User/System Environment Variable Using c++. getenv is not working.

        “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

        M Offline
        M Offline
        Michael Schubert
        wrote on last edited by
        #3

        One minute of googling turned up this code:

        #include
        #include
        extern char**_environ;

        int main(int argc, char *argv[], char *envp[])
        {
        char **next = _environ;

        while (*next)
        {
        printf("%s\n", *next);
        next++;
        }
        return 0;
        }

        If you want to get the value of a specific variable you can use GetEnvironmentVariable()

        M 1 Reply Last reply
        0
        • M Mogaambo

          How to Read a User/System Environment Variable Using c++. getenv is not working.

          “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

          V Offline
          V Offline
          V K 2
          wrote on last edited by
          #4

          GetEnvironmentVariable( ) will work from VS2005 onwards. Have you tried that.

          M 1 Reply Last reply
          0
          • V V K 2

            GetEnvironmentVariable( ) will work from VS2005 onwards. Have you tried that.

            M Offline
            M Offline
            Michael Schubert
            wrote on last edited by
            #5

            VKupunaram wrote:

            from VS2005 onward

            This API has been around since Windows NT 3.1 which was released in 1993. What makes you think that it is supported only from VS2005 onwards?

            1 Reply Last reply
            0
            • M Michael Schubert

              One minute of googling turned up this code:

              #include
              #include
              extern char**_environ;

              int main(int argc, char *argv[], char *envp[])
              {
              char **next = _environ;

              while (*next)
              {
              printf("%s\n", *next);
              next++;
              }
              return 0;
              }

              If you want to get the value of a specific variable you can use GetEnvironmentVariable()

              M Offline
              M Offline
              Mogaambo
              wrote on last edited by
              #6

              I had created variable in user section , using the following code 'defining variables dim objShell, colUserEnvVars 'initializing all variables Set objShell = WScript.CreateObject("WScript.Shell") Set colUserEnvVars = objShell.Environment("System") 'Output File for registry information. colUserEnvVars("rahul") = "rahul" but i cannot read the same from c++, for this i have to restart the system, but Wscript.Echo colUserEnvVars("DeloitteAuditShare123") read the value without restarting ths system.

              “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

              1 Reply Last reply
              0
              • M Murugesan G

                for Visual C++ try this otherwise getenv will work using namespace System; using namespace System::Collections; int main() { Console::WriteLine(); Console::WriteLine( "GetEnvironmentVariables: " ); IDictionary^ environmentVariables = Environment::GetEnvironmentVariables(); IEnumerator^ myEnum = environmentVariables->GetEnumerator(); while ( myEnum->MoveNext() ) { DictionaryEntry^ de = safe_cast<dictionaryentry^>(myEnum->Current); Console::WriteLine( " {0} = {1}", de->Key, de->Value ); } }

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                That's not C++, it looks like Managed C++ (which isn't really C++ but a different language based on it). This is the wrong forum for code like that.

                Steve

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups