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

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.

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.

After latest updates described by you, I would advise to use command-line arguments instead of (or accompanied with) environment level vars. They're instance-specific, easy-to-implement etc.
And, more important, they're stateless.

Related

Setting environment variable for VS2017

I'm installing a software extension for VS2017 which requires me to create a batch file in order to set the 'TargetVisualStudioEdition' environment variable, in my case the variable should be set to Community.
What I did so far is to crate the following simple batch file:
set "%TargetVisualStudioEdition%"="Community"
Once I run it I receive:
set ""="Community"
Then, when I run the installer the installation stops because the environment variable has not been set.
Am I missing something?
don't think you need the %s wrapping the variable;
just do set VARIABLE = value
the %% is just to reference the variables.
if this variable will be shared among processes, use setx

How can I use a system environment variable with CSpyBat

I use C-SPY macros for automated testing. For this purpose I load a startup macro file and it loads additional macro files using the following code:
execUserSetup()
{
__registerMacroFile("$_ENVVAR_$\\macros-1.mac");
__registerMacroFile("$_ENVVAR_$\\macros-2.mac");
...
}
This works like a charm when I run the startup macro from within the IAR Embedded Workbench.
But what I really want to do is running the tests with CSpyBat.exe. There I specify my startup.mac using the option --macro startup.mac.
The files macros-1.mac and so on won't be found then. Instead I get error messages for the macro files (with garbage file names, see below):
ERROR: Could not open macro file: #۸0ٸ`ٸ#2#u
If I use the pathnames without the environment variables, everything is okay.
What am I doing wrong?
The answer is: The workbench supports expanding environment or project variables, but CSpyBat doesn't. So told me IAR support. Also they gave me the following pointer:
See 'C-SPY Debugging Guide' - "Using C-SPY in batch mode", "INVOCATION SYNTAX", page 492:
Note: In those cases where a filename is required—including the DLL files—you are
recommended to give a full path to the filename.

Using Environment Variables in a TFS2015 build

I am trying to set up an API-key to be a global variable that is accessible across all of my TFS2015 Builds. Since TFS2015 seems to lack this feature, I am attempting to use a system environment variable on the build server that is then referenced in the build definitions.
According to Microsoft's documentation, this should be possible. So I have set up a system variable (call it APIKey) on the build server and referenced it within the arguments of a build step using the standard syntax (i.e. "ApiKey=$(APIKey)"). However, instead of replacing the variable with the API-key in the system variable it is trying to use $(APIKey) as the value and causing the build to fail.
It also occurred to me that this custom environment variable would instead be set somewhere in the build agent folder itself but, after some poking around, I'm not sure where or how I would do that.
Are either of these things actually doable?
Following are my steps to achieve this:
Create a system variable on build agent machine:
Restart the build agent machine.
Use the variable in build definition. Here I use cmd task as an example and use the $(testvar) as its argument:
The task will read the value from system variable as following:

Global include path on Windows on VS2013

I should add a global include path to my system (i.e. it's Google Test so I need to include "C:\gtest\include")
I appended it to the PATH environment variable and restarted VS2013. However, VS2013 still complains about the missing .h files.
What's wrong with this IDE?!?
First, you definitely don't want the PATH environment variable. You do want the INCLUDE environment variable.
Second, use a statement like SET INCLUDE=C:\gtest\include;%INCLUDE% to set it.
Third, that's only going to work in a command window that you're running command line builds in.
Fourth, so try setting it on your machine using This PC's properties "Advanced System Settings" Environment Variables dialog. Remember to restart your VS2013 after setting it so it picks up the new env vars.
Fifth, why not do a SET (with no arguments) after you've set your non-working INCLUDE variable and paste the result into your question so we can all see what your environment variables look like - then we might be able to help you better.
Sixth, it's going to end up being your misunderstanding of how things work. That's clear since you think the PATH env var has something to do with VC++ searching for include files. So why the gratuitous "What's wrong with this IDE?!?" slam? It's not winning you any friends.
Seventh, see the MSDN article Setting the Path and Environment Variables for Command-Line Builds for tips. It came right up when I googled for "visual studio include environment variable", and it has help for you.

Setting a VS2010 environment variable from a batch file?

I'm using a batch file to define some variables that will be used in my program.
I want the batch file to change the environment variable and use it in my code , but it's just now working - the macro is not being changed.
to be more specific and clear :
I have a program that creates a DLL and sets it's version
In the common setting of the project - I created a new macro (Common properties->User macros) : TEST_VER = 5
now I want to add a batch file , that will run in the pre-build command and change the value of TESTER
I wrote this in the batch file:
set TEST_VER=9
and used the path of the batch in the pre-build.
BUT it doesn't recognize it.
and still uses 5 as the value
I though doing :
propeties of the project - > resourcses ->general
and add : TEST_VER=$(TEST_VER)
and still didn't work
is there a way to do it??
thanks!!
When Visual Studio starts a program, it runs that program in a new sub-process. In this case, that's a new CMD.EXE, the command prompt shell. Changes made to the environment in a sub-process, a child, have no effect on the parent. Visual Studio has its own set of environment variables which it inherited when it started. Your batch file can't change those values. You can't do what you want the way you're doing it.