Can one cast an EnvDTE.Project into a VCProject
Asked Answered
Z

2

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.

Zebra answered 29/4, 2015 at 18:11 Comment(8)
Have you debugged and looked at what the actual type of project.Object is?Leslileslie
@RonBeyer It's a COM Object but its identity appears to be null... is that normal?Zebra
@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.Zebra
What are the projects in the solution? Are they C++ or C# projects?Leslileslie
@RonBeyer C# projects. I've also tried with a small VB solution but I get the same results.Zebra
VCProject is for C++ projects, which can explain why its null, see msdn.microsoft.com/en-us/library/…Leslileslie
See msdn.microsoft.com/en-us/library/vslangproj.vsproject.aspx for VSProjectLeslileslie
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.Leslileslie
L
4

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).

Leslileslie answered 29/4, 2015 at 18:54 Comment(2)
I'm missing here something - I currently see there are only 3 versions of VSLangProj: VSLangProj, VSLangProj2, VSLangProj80. Ref: msdn.microsoft.com/en-us/library/vslangproj.aspxVerleneverlie
@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.Leslileslie
H
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;
Hackamore answered 17/5, 2017 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.