2

I have seen two posts so far that concern my question. I am wondering how can one cast an EnvDTE.Project into a VCProject.

In this post, fun4jimmy's answer does that exactly in the following line of code (taken from his answer) :

VCProject vcProject = project.Object as VCProject;

I have tried doing the same thing in my solution :

using EnvDTE;
using Microsoft.VisualStudio.VCProjectEngine;
[...]
private List<string> BuildAssembliesAndReturnTheirName(EnvDTE80.DTE2 dte)
{
    Solution sln = dte.Solution;

    bool isDirty = false;
    foreach (Project project in sln.Projects)
    {
        VCProject vcProject = project.Object as VCProject;
        Configuration activeConfiguration = project.ConfigurationManager.ActiveConfiguration;
        foreach (VCConfiguration vcConfiguration in vcProject.Configurations)
        {
            //business logic
        }
    }
[...]

A solution is opened in VS. The solution contains a few C# projects. Everything seems to be in order for this code to execute until I reach

foreach (VCConfiguration vcConfiguration in vcProject.Configurations) 

only to realise that this cast

VCProject vcProject = project.Object as VCProject;

returns null.

Can anyone tell me why that is? I've seen this post in which hveiras suggests

There is a VCCodeModel.dll for each VS version.

If that's the case for VCProjectEngine.dll as well, how can I fix my issue?

I have changed my reference to VCProjectEngine.dll so that it uses the one for Visual Studio 2012 (what I'm working with) but vcProject remains null.

Community
  • 1
  • 1
Kraven
  • 233
  • 2
  • 11
  • 1
    Have you debugged and looked at what the actual type of `project.Object` is? – Ron Beyer Apr 29 '15 at 18:16
  • @RonBeyer It's a COM Object but its identity appears to be null... is that normal? – Kraven Apr 29 '15 at 18:21
  • @RonBeyer The reason I'm asking is because I'm not doing any operations on the projects themselves. I'm only looping through the projects to obtain some info on them. I know my asking about its identity being null might be abit odd, but I'm looping through projects after the loop I've posted to obtain project's properties such as FullName and Properties("OutputFileName") which work fine if I skip my loop. – Kraven Apr 29 '15 at 18:28
  • What are the projects in the solution? Are they C++ or C# projects? – Ron Beyer Apr 29 '15 at 18:31
  • @RonBeyer C# projects. I've also tried with a small VB solution but I get the same results. – Kraven Apr 29 '15 at 18:41
  • 1
    VCProject is for C++ projects, which can explain why its null, see https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.aspx – Ron Beyer Apr 29 '15 at 18:42
  • See https://msdn.microsoft.com/en-us/library/vslangproj.vsproject.aspx for VSProject – Ron Beyer Apr 29 '15 at 18:43
  • Yes, can't cast a C#/VB project to a VCProject, so if you want to identify properties/items on those you'll have to switch over. – Ron Beyer Apr 29 '15 at 18:51

2 Answers2

3

VCProject is for C++ projects, in order to use a similar interface with C#/VB project you'll have to use VSProject.

There are a number of VSLangProj overloads/extensions and you'll have to find the one that is specific to the version you need to use. See: https://msdn.microsoft.com/en-us/library/1xt0ezx9.aspx for all the VSLangProj interfaces from 2 through 100 (I think thats Version 2 through Version 10).

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • I'm missing here something - I currently see there are only 3 versions of VSLangProj: VSLangProj, VSLangProj2, VSLangProj80. Ref: https://msdn.microsoft.com/en-us/library/vslangproj.aspx – Ori Nachum Feb 05 '17 at 09:26
  • 1
    @OriNachum The answer is 2 years old and is for VS2012, if you are working with a newer VS2015 solution the format may have changed. – Ron Beyer Feb 05 '17 at 13:06
3

You can't cast the Project object itself, because there's no inheritance relationship.
But you can use the inner object:

VCProject vcProject = project.Object as VCProject;
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185