Setting environment variable for VS2017 - visual-studio-2017

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

Related

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.
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.

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:

How to create a python 2.7 environment variable?

I have multiple versions of python installed so I wanted to create a command for each of them. I created the variable "python27" as both a user and system variable with the path "C:\Python27" and also tried "C:\Python27\python.exe". In both cases cmd says 'python27' is not recognized as an internal or external command.
My batch file is simply "python27 path_to_py_file".
You need to create a batch file for this. For example:
#C:\Python27\python.exe %*
Save this as python27.bat in a directory referenced by the PATH environment variable and you are good to go.

Use environment variable as compile time constant in C++

As part of a build process, I need to take an environment variable defined by a batch script and use it as a constant within the code at compile time.
For example, say I have defined an environment variable named BUILD_VERSION and set it to 1.0.0, when compiled I want 1.0.0 to be baked into my code. EG:
Batch file:
set BUILD_VERSION = 1.0.0
; call vs compiler
C++ File:
const std::string build_version = BUILD_VERSION // Which will result in "1.0.0".
How would I go about doing this?
In the end I followed txchelp advice and added a /D flag into the Command Line -> Additional Options section of the project properties to declare the environment variable as a preprocessor definition.
It looked something like this:
Then in the batch script that started the build:
set SVN_BUILD_VERSION=1.0.0
And finally to extract it as a string within the source code:
#define STRINGIZER(arg) #arg
#define STR_VALUE(arg) STRINGIZER(arg)
#define BUILD_VERSION_STRING STR_VALUE(BUILD_VERSION)
// ...
const std::string version = BUILD_VERSION_STRING; // Results in "1.0.0".
You can use a prebuild step (I suppose you are on Visual Studio) which will run script which will get this environment variable value, parse C++ source file and change the value
"1.0.0.0" to "1.0.0.1".
Such substitution can be conveniently done by awk.
A VERSION_INFO resource could be a good way go.
The version info so embedded can be inspected by right-clicking the executable and checking its properties.
To do that at the command line:
Redirect output from a batch file to an [.rc] file defining the resource.
Compile the resource using rc.exe.
Embed the resulting .res file by simply passing it to the linker.
Within Visual Studio it might be more complicated.

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.