diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp
index 95effec40c015ff77671fd19458ab16f5afd32bb..1fee41925293393c20bdb255209a6c0e94624374 100644
--- a/src/libs/cplusplus/pp-engine.cpp
+++ b/src/libs/cplusplus/pp-engine.cpp
@@ -1,5 +1,4 @@
-/**************************************************************************
-**
+/**
 ** This file is part of Qt Creator
 **
 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
@@ -51,6 +50,7 @@
 #include <Lexer.h>
 #include <Token.h>
 #include <Literals.h>
+#include <cctype>
 
 #include <QtDebug>
 #include <algorithm>
diff --git a/src/libs/utils/process_stub_win.c b/src/libs/utils/process_stub_win.c
index 3d99a6853957de81341d000d2cf9912748d30405..0ca484eb751324712e417cd0a87d7e6aa4263736 100644
--- a/src/libs/utils/process_stub_win.c
+++ b/src/libs/utils/process_stub_win.c
@@ -34,6 +34,7 @@
 #define _WIN32_WINNT 0x0501 /* WinXP, needed for DebugActiveProcessStop() */
 
 #include <windows.h>
+#include <shellapi.h>
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
diff --git a/src/plugins/projectexplorer/environment.cpp b/src/plugins/projectexplorer/environment.cpp
index 0c2e74f67e4d6c3d13f7fa81b5866b7e46884903..541445cf99ed92d855fa307d8a1d416bf4c9100f 100644
--- a/src/plugins/projectexplorer/environment.cpp
+++ b/src/plugins/projectexplorer/environment.cpp
@@ -288,6 +288,16 @@ void Environment::modify(const QList<EnvironmentItem> & list)
     *this = resultEnvironment;
 }
 
+bool Environment::operator!=(const Environment &other)
+{
+    return !(*this == other);
+}
+
+bool Environment::operator==(const Environment &other)
+{
+    return m_values == other.m_values;
+}
+
 QStringList Environment::parseCombinedArgString(const QString &program)
 {
     QStringList args;
@@ -341,3 +351,5 @@ QString Environment::joinArgumentList(const QStringList &arguments)
     return result;
 }
 
+
+
diff --git a/src/plugins/projectexplorer/environment.h b/src/plugins/projectexplorer/environment.h
index 91cd958d4b0bfabbae5aa8d9e686218b910bf00e..367aee5a8022f7324e7293a70ed12fc36f88aada 100644
--- a/src/plugins/projectexplorer/environment.h
+++ b/src/plugins/projectexplorer/environment.h
@@ -89,6 +89,8 @@ public:
     static QStringList parseCombinedArgString(const QString &program);
     static QString joinArgumentList(const QStringList &arguments);
 
+    bool operator!=(const Environment &other);
+    bool operator==(const Environment &other);
 private:
     QMap<QString, QString> m_values;
 };
diff --git a/src/plugins/projectexplorer/toolchain.cpp b/src/plugins/projectexplorer/toolchain.cpp
index 7c080aad8e2f7ba54fbb0345222927e695e56dba..e827a5a3d425de346b33d3e4f760be1993a0d7aa 100644
--- a/src/plugins/projectexplorer/toolchain.cpp
+++ b/src/plugins/projectexplorer/toolchain.cpp
@@ -246,13 +246,13 @@ QList<HeaderPath> MSVCToolChain::systemHeaderPaths()
 
 void MSVCToolChain::addToEnvironment(ProjectExplorer::Environment &env)
 {
-    if (!m_valuesSet) {
+    if (!m_valuesSet || env != m_lastEnvironment) {
+        m_lastEnvironment = env;
         QSettings registry("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7",
                        QSettings::NativeFormat);
         if (m_name.isEmpty())
             return;
         QString path = registry.value(m_name).toString();
-        ProjectExplorer::Environment oldEnv(env);
         QString desc;
         QString varsbat = path + "Common7\\Tools\\vsvars32.bat";
         if (QFileInfo(varsbat).exists()) {
@@ -265,7 +265,8 @@ void MSVCToolChain::addToEnvironment(ProjectExplorer::Environment &env)
             tf.flush();
             tf.waitForBytesWritten(30000);
 
-            QProcess run; // TODO run in the environment we want to add to...
+            QProcess run;
+            run.setEnvironment(env.toStringList());
             QString cmdPath = env.searchInPath("cmd");
             run.start(cmdPath, QStringList()<<"/c"<<filename);
             run.waitForFinished();
@@ -281,9 +282,7 @@ void MSVCToolChain::addToEnvironment(ProjectExplorer::Environment &env)
                     if (regexp.exactMatch(line2)) {
                         QString variable = regexp.cap(1);
                         QString value = regexp.cap(2);
-                        value.replace('%' + variable + '%', oldEnv.value(variable));
                         m_values.append(QPair<QString, QString>(variable, value));
-
                     }
                 }
                 vars.close();
@@ -293,14 +292,11 @@ void MSVCToolChain::addToEnvironment(ProjectExplorer::Environment &env)
         m_valuesSet = true;
     }
 
-    //qDebug()<<"MSVC Environment:";
     QList< QPair<QString, QString> >::const_iterator it, end;
     end = m_values.constEnd();
     for (it = m_values.constBegin(); it != end; ++it) {
         env.set((*it).first, (*it).second);
-        //qDebug()<<"variable:"<<(*it).first<<"value:"<<(*it).second;
     }
-
 }
 
 QString MSVCToolChain::makeCommand() const
diff --git a/src/plugins/projectexplorer/toolchain.h b/src/plugins/projectexplorer/toolchain.h
index a977c125bf3d3e1e255d1efc2449f9d593d2a886..813092d7bc1e48c3ba5e22a64120a0a45907850d 100644
--- a/src/plugins/projectexplorer/toolchain.h
+++ b/src/plugins/projectexplorer/toolchain.h
@@ -119,6 +119,7 @@ protected:
 private:
     mutable QList<QPair<QString, QString> > m_values;
     mutable bool m_valuesSet;
+    mutable ProjectExplorer::Environment m_lastEnvironment;
 };
 
 // TODO some stuff needs to be moved into here
diff --git a/src/plugins/qt4projectmanager/qtversionmanager.cpp b/src/plugins/qt4projectmanager/qtversionmanager.cpp
index 013e7b1ca749934bf09ac04c155bfe64205e2413..c6024a869c38178b0d4dd83f7a13e6a9b50803c5 100644
--- a/src/plugins/qt4projectmanager/qtversionmanager.cpp
+++ b/src/plugins/qt4projectmanager/qtversionmanager.cpp
@@ -1235,7 +1235,6 @@ void QtVersion::addToEnvironment(Environment &env)
     // add libdir, includedir and bindir
     // or add Mingw dirs
     // or do nothing on other
-
 }
 
 int QtVersion::uniqueId() const