How to determine if an EnvDTE.Project represents a C / C ++ project?

I have an instance of Project and I don’t understand how to find out the type of project / language. In particular, I need to check out a C / C ++ project.

docs seem to be missing.

Earlier, another person added the following bit of magic to my open source VS extension, and it worked in VS2013-2015:

 private static bool isVisualCppProject(object project) { Type projectObjectType = project.GetType(); var projectInterface = projectObjectType.GetInterface("Microsoft.VisualStudio.VCProjectEngine.VCProject"); return projectInterface != null; } ... isVisualCppProject(project.Object); 

But it no longer works in VS 2017 RC. And I would gladly get rid of this magic of reflection of runtime and not type object and dynamic instead of Project - because of this, the code already becomes unattainable.

+6
source share
2 answers

You can determine the type of project using the name of the project extension and the following demo for reference.

  DTE2 dte = (DTE2)this.ServiceProvider.GetService(typeof(DTE)); Solution2 solution = (Solution2)dte.Solution; foreach (Project p in solution.Projects) { string[] projectName = p.FullName.Split('.'); if (projectName.Length > 1) { string fileExt = projectName[projectName.Length-1]; if (fileExt == "vcxproj") { //is c/c++ porject } } } 
+2
source

The EnvDTE.Project.Kind is the first place to search, as indicated in the comments above. See HOWTO: Guess the Visual Studio Project Type with an Add-in or Macro

There is a list of some well-known GUIDs, see INFO: A List of Famous Guids Project Types .

You might be interested in {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}. But a project can have different subtypes, in which case several GUIDs apply to it. For example, MVC + C #. See HOWTO: get the design taste (subtype) of a Visual Studio project from an add-in

EnvDTE.Project.Kind contains the main type of the project and it is not guaranteed that, for example, C ++ GUID will be stored in this property. The safest way is to get all the GUIDs that apply to the project. Use the GetProjectTypeGuids method from the last link. You will get a list of all GUIDs, and then you just check to see if it contains {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}.

+1
source

Source: https://habr.com/ru/post/1013213/


All Articles