locked
VC++ project: How can I get project properties from VS add-in? What class gives access to what is called Properties in IDE? RRS feed

  • Question

  • I'm working on an extension that targets C++ projects. It needs to retrieve a list of IncludePaths for a project. In VS IDE, it's menu -> Project -> Properties -> Configuration properties -> C++ -> General -> Additional Include Directories. That's what I need to get programmatically in my extension.

    I have a corresponding VCProject instance, I also have a VCConfiguration instance. Judging from the Automation Model Overview chart, the project and configuration both have a collection of properties. However, they don't seem to available. Neither VCConfiguration nor VCProject classes have any property collection, not even when I inspect the contents of VCConfiguration and VCProject objects at runtime.

    MSDN docs also don't provide any insights. VCConfiguration interface has a property PropertySheets, but after examining it at runtime with the help of debugger I have determined it's not what I need.

    P. S. If I could just get the value of Command Line property (Project -> Properties -> Configuration properties -> C++ -> Command Line), the list of arguments compiler will be called with for a given project - that's also fine by me, one can parse that string to get all include paths.





    • Edited by Alexium Wednesday, October 2, 2013 12:34 PM
    Wednesday, October 2, 2013 12:08 PM

Answers

  • Hi,

    There is the general automation model (EnvDTE), which is tricky, and it has EnvDTE.Project.ConfigurationManager.XXX methods and properties.

    And there is the VC++ automation model (Microsoft.VisualStudio.VCProjectXXX), which is even more tricky. It took me a while to get this which I suppose is what you are looking for. This macro (VB.NET) shows the AdditionalIncludeDirectories for each configuration of the first project of the solution:


        Sub MyMacro()
    
           GetAdditionalIncludeDirectories(DTE.Solution.Projects.Item(1))
    
        End Sub
    
        Sub GetAdditionalIncludeDirectories(ByVal prj As EnvDTE.Project)
    
            Dim proj As Microsoft.VisualStudio.VCProjectEngine.VCProject
            Dim compilerTool As Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool
            Dim toolsCollection As Microsoft.VisualStudio.VCProjectEngine.IVCCollection
            Dim configurationsCollection As Microsoft.VisualStudio.VCProjectEngine.IVCCollection
    
            proj = CType(prj.Object, Microsoft.VisualStudio.VCProjectEngine.VCProject)
    
            configurationsCollection = CType(proj.Configurations, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
            For Each configuration As Microsoft.VisualStudio.VCProjectEngine.VCConfiguration In configurationsCollection
    
                toolsCollection = DirectCast(configuration.Tools, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
                For Each toolObject As Object In toolsCollection
    
                    If TypeOf toolObject Is Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool Then
    
                        compilerTool = CType(toolObject, Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
    
                        System.Windows.Forms.MessageBox.Show(configuration.Name & ": " & compilerTool.AdditionalIncludeDirectories)
    
                        Exit For
    
                    End If
    
                Next
    
            Next
    
        End Sub
    



    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/

    • Proposed as answer by Carlos J. Quintero Wednesday, October 2, 2013 3:34 PM
    • Marked as answer by Alexium Wednesday, October 2, 2013 4:17 PM
    Wednesday, October 2, 2013 3:15 PM
  • Try the code below.

        Sub MyMacro()
    
            GetIncludeDirectories(DTE.Solution.Projects.Item(1))
    
        End Sub
    
        Sub GetIncludeDirectories(ByVal prj As EnvDTE.Project)
    
            Dim proj As Microsoft.VisualStudio.VCProjectEngine.VCProject
            Dim compilerTool As Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool
            Dim platform As Microsoft.VisualStudio.VCProjectEngine.VCPlatform
            Dim configurationsCollection As Microsoft.VisualStudio.VCProjectEngine.IVCCollection
    
            proj = CType(prj.Object, Microsoft.VisualStudio.VCProjectEngine.VCProject)
    
            configurationsCollection = CType(proj.Configurations, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
            For Each configuration As Microsoft.VisualStudio.VCProjectEngine.VCConfiguration In configurationsCollection
    
                platform = DirectCast(configuration.Platform, Microsoft.VisualStudio.VCProjectEngine.VCPlatform)
    
                System.Windows.Forms.MessageBox.Show(configuration.Name & ": " & platform.IncludeDirectories)
    
            Next
    
        End Sub
    


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/

    • Proposed as answer by Carlos J. Quintero Wednesday, October 2, 2013 5:57 PM
    • Marked as answer by Alexium Wednesday, October 2, 2013 7:58 PM
    Wednesday, October 2, 2013 5:28 PM

All replies

  • Hi,

    There is the general automation model (EnvDTE), which is tricky, and it has EnvDTE.Project.ConfigurationManager.XXX methods and properties.

    And there is the VC++ automation model (Microsoft.VisualStudio.VCProjectXXX), which is even more tricky. It took me a while to get this which I suppose is what you are looking for. This macro (VB.NET) shows the AdditionalIncludeDirectories for each configuration of the first project of the solution:


        Sub MyMacro()
    
           GetAdditionalIncludeDirectories(DTE.Solution.Projects.Item(1))
    
        End Sub
    
        Sub GetAdditionalIncludeDirectories(ByVal prj As EnvDTE.Project)
    
            Dim proj As Microsoft.VisualStudio.VCProjectEngine.VCProject
            Dim compilerTool As Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool
            Dim toolsCollection As Microsoft.VisualStudio.VCProjectEngine.IVCCollection
            Dim configurationsCollection As Microsoft.VisualStudio.VCProjectEngine.IVCCollection
    
            proj = CType(prj.Object, Microsoft.VisualStudio.VCProjectEngine.VCProject)
    
            configurationsCollection = CType(proj.Configurations, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
            For Each configuration As Microsoft.VisualStudio.VCProjectEngine.VCConfiguration In configurationsCollection
    
                toolsCollection = DirectCast(configuration.Tools, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
                For Each toolObject As Object In toolsCollection
    
                    If TypeOf toolObject Is Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool Then
    
                        compilerTool = CType(toolObject, Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
    
                        System.Windows.Forms.MessageBox.Show(configuration.Name & ": " & compilerTool.AdditionalIncludeDirectories)
    
                        Exit For
    
                    End If
    
                Next
    
            Next
    
        End Sub
    



    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/

    • Proposed as answer by Carlos J. Quintero Wednesday, October 2, 2013 3:34 PM
    • Marked as answer by Alexium Wednesday, October 2, 2013 4:17 PM
    Wednesday, October 2, 2013 3:15 PM
  • Thank you very much, that works! No way I could have figured it out myself. 

    The only issue I have left is where / how to get the standard include paths (the ones that are implicitly assumed, like Windows SDK and standard library). Any hints on that?

    • Edited by Alexium Wednesday, October 2, 2013 4:45 PM
    Wednesday, October 2, 2013 4:10 PM
  • Try the code below.

        Sub MyMacro()
    
            GetIncludeDirectories(DTE.Solution.Projects.Item(1))
    
        End Sub
    
        Sub GetIncludeDirectories(ByVal prj As EnvDTE.Project)
    
            Dim proj As Microsoft.VisualStudio.VCProjectEngine.VCProject
            Dim compilerTool As Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool
            Dim platform As Microsoft.VisualStudio.VCProjectEngine.VCPlatform
            Dim configurationsCollection As Microsoft.VisualStudio.VCProjectEngine.IVCCollection
    
            proj = CType(prj.Object, Microsoft.VisualStudio.VCProjectEngine.VCProject)
    
            configurationsCollection = CType(proj.Configurations, Microsoft.VisualStudio.VCProjectEngine.IVCCollection)
    
            For Each configuration As Microsoft.VisualStudio.VCProjectEngine.VCConfiguration In configurationsCollection
    
                platform = DirectCast(configuration.Platform, Microsoft.VisualStudio.VCProjectEngine.VCPlatform)
    
                System.Windows.Forms.MessageBox.Show(configuration.Name & ": " & platform.IncludeDirectories)
    
            Next
    
        End Sub
    


    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/

    • Proposed as answer by Carlos J. Quintero Wednesday, October 2, 2013 5:57 PM
    • Marked as answer by Alexium Wednesday, October 2, 2013 7:58 PM
    Wednesday, October 2, 2013 5:28 PM
  • Much appreciated, thanks for your time!
    Wednesday, October 2, 2013 7:59 PM