c++visual-studioenvironment-variablesgetenv

Visual Studio environmant variable not recognized by C++ stdlib getenv


I have a C++ project on windows, on which I use the getenv method from stdlib to get the value of an environment variable $MyVar When I set the variable MyVar in a cmd and then call my program in the command line, everything works. But when I call it from Visual Studio (2012), the program see $MyVar as unset.

To try and set it in Visual Studio I have tried to use a Property Sheet to define a macro MyVar as an environment variable (cf. image below) but the getenv method from stdlib doesn't see it.

Screenshot of the window where I tried to define the env variable in Visual Studio

Is there a way to define my environment variable in Visual Studio in a way that the getenv method from stdlib would recognize it ?

Thank you all in advance for your help

EDIT : I know that the getenv method would recognize MyVar if I defined it as a global env variable, but that is not a solution for me because I want to be able to launch several instances of my program at the same time with different values for MyVar. I need a way to define a local env variable in Visual Studio that would be recognized by my program when I launch it from Visual Studio.


Solution

  • The thing is: there are different environments, so once you create a var in one env it doesn't appear at others.

    1) Once you create cmd session, OS creates a dedicated environment for it. So, once you create a var in the prompt any app launched in this cmd would be able to see that var. Once you kill that cmd all the vars are gone too.

    2) If you need global change of environment vars, you have to visit "Advanced System Settings" dialog (Control Panel\System and Security\System) then click "Environment variables" button and do your edits. Note: you may need to restart child sessions to let them see a change in global env vars.