title: set vcbuild environment through msbuild
body: <p>I have an msbuild project file that is used for building several C++ projects (*.vcproj). Here's a part of a typical project file, in which global environment variables for the build are overridden as properties:</p>
  
  <pre><code>&lt;PropertyGroup&gt;
    &lt;MYBINDIR&gt;d:\buildsysroot\bin&lt;/MYBINDIR&gt;
    &lt;MYLIBDIR&gt;d:\buildsysroot\lib&lt;/MYLIBDIR&gt;
    ...
  &lt;/PrpertyGroup&gt;
  
  &lt;ItemGroup&gt;
    &lt;MyItems Include="d:\buildsysroot\myproject\myproject.vcproj"/&gt;
  &lt;/ItemGroup&gt;
  
  &lt;Target Name="Release_x64"&gt;
    &lt;VCBuild Projects="@(MyItems)" Configuration="Release_dll|x64"/&gt;
  &lt;/Target&gt;
  </code></pre>
  
  <p>These environment variables are used by all vcproj files, through property sheets.
  Here's an excerpt of a vcproj:</p>
  
  <pre><code>&lt;Configuration
    Name="Release_dll|x64"
    ConfigurationType="1"
    InheritedPropertySheets="$(BUILDPROPSDIR)\defaults.vsprops;$(BUILDPROPSDIR)\build_dll.vsprops"
  &gt;
  </code></pre>
  
  <p>And this is how the property sheets look:</p>
  
  <pre><code>&lt;VisualStudioPropertySheet
    Name="defaults"
    OutputDirectory="$(MYBINDIR)\_$(ConfigurationName)_$(PlatformName)"
    IntermediateDirectory="$(MYBUILDDIR)_$(ProjectName)_$(ConfigurationName)_$(PlatformName)"
    &gt;
    &lt;Tool
      Name="VCLinkerTool"
      AdditionalLibraryDirectories="&amp;quot;$(MYLIBDIR)&amp;quot;"
    /&gt;
  &lt;VisualStudioPropertySheet/&gt;
  </code></pre>
  
  <p>The problem is: when building, the value for the environment variables declared in the msbuild file (MYBINDIR, MYLIBDIR) do not make it into the property sheets, but seem to be local to the msbuild file only: within the msbuild project MYBINDIR expands to "d:\buildsysroot\bin", but within the vc project MYBINDIR expands to "d:\projects\bin", which is the value that I set globally (eg via MyComputer->Advanced->EnvironmentVariables) <em>and</em> which must stay there because it's how my main development environment is set up.</p>
  
  <p>How do I pass the environment set in the msbuild file to the VCBuild task?</p>
  
  <p>I know that this problem could be solved by using a batch file and declaring the environment there using 'set', and not in the msbuild file; that's how I used to do it, but I prefer the method above as it's just one call to msbuild with one argument and that's it.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I needed a solution quickly and came up with a custom task that sets the environment in the same way a batch file would do, but with the bonus of doing it within msbuild..  The tasks's Execute() method simply does Environment.SetEnvironmentVariable( var, val ).
  This still requires setting the proces' environment, I'd be glad to hear if there is another way!</p>