How to: Manipulate Projects by Using the Visual C++ Project Model

The following procedure shows how to find information about Visual C++ projects from some of the project model objects available through Microsoft.VisualStudio.ProjectEngine.dll. This assembly exposes the functionality of the compiler, linker, and other build tools, as well as to the functionality of the Property Pages dialog box for a Visual C++ project.

You can integrate the code in the following procedures into a VSPackage named TestVCProjectPackage that has a menu command named My Command name. For information about how to do this, see Walkthrough: Creating a Menu Command By Using the Visual Studio Package Template.

To use VCProjectEngine to manipulate Visual C++ projects

  1. Add a reference to Microsoft.VisualStudio.VCCodeModel (on the Extensions tab) and System.Windows.Forms (on the Framework tab.

  2. Add the following using statements to the TestVCProjectPackagePackage.cs file:

    using EnvDTE;
    using EnvDTE80;
    using EnvDTE90;
    using EnvDTE100;
    using Microsoft.VisualStudio.VCCodeModel;
    using System.Windows.Forms;
    
  3. Remove the existing code from the MenuItemCallback method. Add code to this method that gets the application object (in this case DTE2:

    private void MenuItemCallback(object sender, EventArgs e)
    {
        DTE2 dte2 = (DTE2)GetService(typeof(DTE));
    }
    
  4. In the same method, get the open projects. Make sure that a project is actually open.

    private void MenuItemCallback(object sender, EventArgs e) 
    {
        DTE2 dte2 = (DTE2)GetService(typeof(DTE));
    
        VCProject prj; 
        Projects projColl = dte2.Solution.Projects; 
    
        if (projColl.Count == 0) 
        {
            MessageBox.Show("You must have a project open in the experimental instance."); 
            return; 
        }
        if (projColl.Count > 0) 
        {
            // to be filled in later
        }
    }
    
  5. Get the open project and inspect the project configurations.

    private void MenuItemCallback(object sender, EventArgs e)
    {
        DTE2 dte2 = (DTE2)GetService(typeof(DTE));
    
        VCProject prj;
        VCAssemblyReference vcar;
        int idx;
        IVCCollection mycollection, mycollection2;
        VCConfiguration cfg;
        String sTest = "Configuration Information:" + "\n";
        VCFile file;
        try
        {   
            Projects projColl = dte2.Solution.Projects;
    
            if (projColl.Count == 0)
            {
                MessageBox.Show("You must have a project open in the experimental instance.");
                return;
            }
            if (projColl.Count > 0)
            {
                prj = (VCProject)dte2.Solution.Projects.Item(1).Object;
                if (prj.CanAddAssemblyReference(@"%VSINSTALLDIR%\Common7\IDE\PublicAssemblies\VSLangProj.dll"))
                    {  
                        vcar = (VCAssemblyReference)prj.AddAssemblyReference(@"%VSINSTALLDIR%\Common7\IDE\PublicAssemblies\VSLangProj.dll");
                        MessageBox.Show("The assembly named" + vcar.AssemblyName + " was added.");
                    }
                mycollection = (IVCCollection)prj.Configurations;
                MessageBox.Show("Number of configurations in the project : " + mycollection.Count);
                for (idx = 1; idx <= mycollection.Count; idx++)
                {
                    cfg = (VCConfiguration)mycollection.Item(idx);
                    sTest = sTest + "Configuration name: " + cfg.Name + "\n" 
                     + "Platform: " + "\n" + cfg.Platform.ToString() + "\n" 
                     + "Program database: \n" + cfg.ProgramDatabase + "\n";
                }
                MessageBox.Show(sTest);
                mycollection2 = (IVCCollection)prj.Files;
                MessageBox.Show("Number of files in the project: " + mycollection2.Count);
            }
        }
        catch (SystemException ex)
        {
            MessageBox.Show("ERROR: " + ex.ToString());
        }
    }
    
  6. Build the solution and start debugging. A second instance of Visual Studio appears. This instance is called the experimental instance.

  7. Open a Visual C++ project in the experimental instance.

  8. On the Tools menu, click My Command name.

    The command code adds a reference to VSLangProj.dll and displays information about it by using the VCAssemblyReference object. It also enumerates and displays all the configuration names by using the VCConfiguration object. VCFile is used to display the file extensions that are present in the Visual C++ project. The VCProject and IVCCollection objects are used to declare the project as a Visual C++ project and to enumerate the configuration and file collections.

See Also

Concepts

Visual C++ Project Model

Visual C++ Code Model

Other Resources

Visual C++ Extensibility Object Model

Visual C++ Wizard Model