6

I use the following code to compile one C++ application:

Engine engine = new Engine(); engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319"; BuildPropertyGroup group = new BuildPropertyGroup(); group.SetProperty("Configuration", "Debug"); engine.BuildEnabled = true; FileLogger logger = new FileLogger(); logger.Parameters = @"logfile=C:\tmp\build.log"; engine.RegisterLogger(logger); bool success = engine.BuildProjectFile(@"E:\sv_repos\Test\Test\VS2010\Test\Test\Test.vcxproj", new string[] { "Build" }, group); engine.UnregisterAllLoggers(); if (success) MessageBox.Show("build!"); 

But I get the following error, any idea will be appreciated.

Build started 2012/01/04 03:32:16 ب.ظ. MSBUILD : error MSB4014: The build was aborted because of an internal failure. MSBUILD : error MSB4014: System.InvalidCastException: Unable to cast object of type 'System.Xml.XmlComment' to type 'System.Xml.XmlElement'. MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.ProcessProjectChildren(XmlElement projectElement, String projectDirectoryLocation, Boolean importedProject) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.ProcessImportElement(XmlElement importElement, String projectDirectoryLocation, Boolean importedProject) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.ProcessProjectChildren(XmlElement projectElement, String projectDirectoryLocation, Boolean importedProject) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.ProcessImportElement(XmlElement importElement, String projectDirectoryLocation, Boolean importedProject) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.ProcessProjectChildren(XmlElement projectElement, String projectDirectoryLocation, Boolean importedProject) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.ProcessMainProjectElement() MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.RefreshProjectIfDirty() MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.InternalLoadFromXmlDocument(XmlDocument projectXml, ProjectLoadSettings projectLoadSettings) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Project.Load(String projectFileName, BuildEventContext buildEventContext, ProjectLoadSettings projectLoadSettings) MSBUILD : error MSB4014:
at Microsoft.Build.BuildEngine.Engine.GetMatchingProject(Project existingProject, String projectFullPath, BuildPropertyGroup globalPropertiesToUse, String toolsVersion, String[] targetNames, BuildEventContext buildEventContext, Boolean toolsVersionPeekedFromProjectFile) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Engine.BuildProjectFileInternal(BuildRequest buildRequest) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Engine.EngineBuildLoop(BuildRequest terminatingBuildRequest) MSBUILD : error MSB4014: at Microsoft.Build.BuildEngine.Engine.PostProjectEvaluationRequests(Project project, String[] projectFiles, String[][] targetNames, BuildPropertyGroup[] globalPropertiesPerProject, IDictionary[] targetOutputsPerProject, BuildSettings buildFlags, String[] toolVersions)

6
  • Can you build the C++ project in Visual Studio directly? Commented Jan 4, 2012 at 12:09
  • Probably a bug in MSBuild: XmlComment is not derived from XmlElement, so it cannot be cast to XmlElement. Commented Jan 4, 2012 at 12:14
  • I'd look inside the .vcxproj file, see if there are any XML comments, and remove them. Commented Jan 4, 2012 at 12:17
  • there is no xml except the followings:<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="schemas.microsoft.com/developer/msbuild/2003"> Commented Jan 4, 2012 at 12:23
  • did you find any solution for this? I currently have the same problem when using Microsoft.Build.BuildEngine.Project.Load for a project that loads fine in VS itself. It seems the problem are the comments in "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.props" Commented Nov 20, 2012 at 13:56

1 Answer 1

5
+25
 engine.BinPath = @"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319"; 

This is a versioning problem, your project is using an old version of the MSBuild engine. Visible from the stack trace, it is using the Microsoft.Build.BuildEngine.Project class. Which has this attribute:

[ObsoleteAttribute("This class has been deprecated. Please use Microsoft.Build.Evaluation.Project from the Microsoft.Build assembly instead.")] 

MSBuild was significantly revised in .NET 4.0 as part of the major overhaul of the C++ build system. Which previously used a legacy build engine named VCBuild and a legacy project file format, it had the .vcproj filename extension. The MSBuild version you are using doesn't know anything about those changes and isn't capable of properly parsing a .vcxproj

You need to update the reference to the Microsoft.Build.Engine.dll reference assembly. The runtime version as displayed in the Properties window should show "v4.0.30319". I didn't have much luck using the Microsoft.Build.Evaluation.Project class, it is complaining about not being able to find .props files and looks in the wrong directory for them. It does however properly parse the .vcxproj file :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Hans for your clarification. I did tried using Evaluation.Project also without luck. I do encounter some other error (error MSB4127: The "SetEnv" task could not be instantiated from the assembly "...MSBuild\Microsoft.Cpp\v4.0\v120\Microsoft.Build.CppTasks.Common.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly...) which make no sense for me (I have just v120 installed) and if I build from cmd is working like a charm. Anybody succeeding in this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.