0

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 :

  1. I have a program that creates a DLL and sets it's version

  2. In the common setting of the project - I created a new macro (Common properties->User macros) : TEST_VER = 5

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

enter image description here

enter image description here

is there a way to do it??

thanks!!

user1391863
  • 137
  • 3
  • 12
  • You may need an Administrator rights to set the environment variable. – SChepurin Dec 16 '12 at 11:58
  • I've noticed (at least from your screenshots) that your batch file sets a variable called TESTER, whereas your VS2010 macro shows TEST_VER. Which one is or isn't being set? – cowboydan Dec 16 '12 at 12:00
  • my bad , I TEST_VER and TESTER are the same..just wrote here TESTER. changed it!. why Administrator rights? it's my environment variable... I did : TEST_VER=$(TEST_VER) and it doens't work as well – user1391863 Dec 16 '12 at 12:06
  • maybe try setx instead of set? – Digital Da Dec 16 '12 at 12:08
  • inside the batch file? tried... but it gives me illegal syntax.. should I use setx not in the batch?? resources? I really dont know – user1391863 Dec 16 '12 at 12:13

1 Answers1

0

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.

librik
  • 3,738
  • 1
  • 19
  • 20
  • thanks @librik. Is there a way I can do it? I want somehow to use the pre-build event to define variables ( like : #define major 6) but not using a header ? or a batch file that creates this header? thank you! – user1391863 Dec 16 '12 at 12:38
  • 1
    You can open a new command-prompt, set the environment variables in it and then launch VS from inside it. VS will pick up the environment from that command prompt. – SChepurin Dec 16 '12 at 12:54
  • Is there a way to pass values during the build and these values will be used as my #define VALUEs ? – user1391863 Dec 16 '12 at 15:22
  • You can try generating a header file which #defines your values, then #include that file in your project. – bacar Aug 01 '14 at 08:39