How to determine whether or not EnvDTE.Project object represents a C/C++ project?
Asked Answered
P

2

6

I have a Project instance, and I don't understand how to find out the project type/language. Specifically, I need to check for a C/C++ project.

The docs seem lacking.

Previously, 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 runtime reflection magic and un-typed object and dynamic instead of Project - the code is already getting unmaintainable because of this.

Preliminaries answered 16/12, 2016 at 8:44 Comment(4)
There is Kind property(msdn.microsoft.com/en-us/library/envdte.project.kind.aspx) for envdte.project. It is GUID. There are predefined Project Kinds in PrjKind (msdn.microsoft.com/en-us/library/vslangproj.prjkind.aspx) but it seems there is no property for c/c++. I guess you can get that GUID and it will be same for all c/c++ projects.Hebetude
@AramKocharyan: I did see Kind, but there's no list of possible "kinds", nor any guarantee that it is indeed static across various machines, installations and VS versions...Preliminaries
@Violet-Giraffe: Do you resolve the issue? if the issue still exists, please feel free let me know.Clavicytherium
@ColeWu-MSFT: I believe your answer will serve, but I want to actually implement it before I accept the answer. And that requires a fair bit of careful refactoring of my extension.Preliminaries
C
2

you can determine project type via project extension name, and the following demo for your 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
                    }
                }
}
Clavicytherium answered 19/12, 2016 at 7:0 Comment(0)
G
2

The EnvDTE.Project.Kind is the first place to look, as mentioned in the comments above. See HOWTO: Guess the type of a Visual Studio project from an add-in or macro

There is a list of some well-known GUIDs, see INFO: List of known project type Guids.

You are probably interested in {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}. But a project may have various sub-types, in which case multiple GUIDs are applied to it. For example MVC + C#. See HOWTO: Get the project flavor (subtype) of a Visual Studio project from an add-in

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

Goahead answered 19/12, 2016 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.