Failed to transfer DTE, project or solution for VCProject and VCCodeModel

I am trying to get some information about c + = programs through code. I had some success with EnvDTE, now I need to use VCProject and VCCodeModel and I am facing casting problems (hope this is all ...)

In the working class, I have a DTE "application" passed from Connect.

I have:

 EnvDTE.Project project = application.SelectedItems.Item(1).Project; EnvDTE.Solution sol = (EnvDTE.Solution)application.Solution; 

I would like to use the β€œproject” and not the first project in the solution, like the examples I found on the Internet β€” as shown below β€” but basically I would like to have something that works in the first place.

For VCProject, I tried (from the Microsoft website and all other web examples):

 VCProject vcProject = (VCProject)application.Solution.Projects.Item(1).Object; MessageBox.Show(vcProject.ProjectDirectory); 

or simply

 VCProject vcProject = (VCProject)project.Object; 

For VCCodeModel, I translated into C # http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vccodemodel.vccodeinclude.aspx :

 public void GetAllIncludes() { VCCodeModel vcCM = (VCCodeModel)application.Solution.Item(1).CodeModel; foreach (VCCodeInclude vcInclude in vcCM.Includes) { MessageBox.Show(vcInclude.DisplayName); } } 

Both give an exception:

 "unable to cast com object of type 'system.__comobject' to interface type Microsoft.VisualStudio.VCCodeModel" "unable to cast com object of type 'Microsoft.VisualStudio.Project.VisualC.VCProjectEngine.VCProjectShim' to type Microsoft.VisualStudio.Project.VisualC.VCProjectEngine.VCProject" 

How can I customize this? It is preferable to use a "project" ... or application.SelectedItems ... Is this possible?

Can someone please give me an idea? Thanks.

+6
source share
1 answer

This issue occurs when you try to use the CodeModel for a different version of VCCodeModel. There is VCCodeModel.dll for each version of VS.

+1
source

All Articles