Export (0) Print
Expand All

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

The following procedures demonstrate how to utilize some of the objects available through Microsoft.VisualStudio.ProjectEngine.dll to programmatically examine information that is specific to Visual C++ projects.

The Visual C++ Project Model, which is contained in Microsoft.VisualStudio.VCProjectEngine.dll, programmatically 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.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To use VCProjectEngine to manipulate Visual C++ projects

  1. Create a Visual Studio add-in project in Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add using Microsoft.VisualStudio.VCProjectEngine; to the top of the Connect.vs file.

  4. Replace the code in the OnConnection method with the following code:

    public void OnConnection(object application,
     Extensibility.ext_ConnectMode connectMode, object addInInst,
     ref System.Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Pass the applicationObject member variable to 
        // the code example.
        VCProjectEngineExample((DTE2)_applicationObject);
    }
    
    public void VCProjectEngineExample(DTE2 dte)
    {
        VCProject prj;
        VCAssemblyReference vcar;
        int idx;
        IVCCollection mycollection, mycollection2;
        VCConfiguration cfg;
        String sTest = "Configuration Information:" + "\n";
        VCFile file;
        try
        {
            prj = (VCProject)dte.Solution.Projects.Item(1).Object;
            if (prj.CanAddAssemblyReference
    ("C:\\Program Files\\Microsoft Visual Studio 8\
    \Common7\\IDE\\PublicAssemblies\\VSLangProj.dll"))
            {
                // Make sure the path to the assembly
                // is correct. The installation root in the 
                // example is "C:", but it might be different
                // on your computer.
                vcar= (VCAssemblyReference)prj.AddAssemblyReference
    ("C:\\Program Files\\Microsoft Visual Studio 8\
    \Common7\\IDE\\PublicAssemblies\\VSLangProj.dll");
                MessageBox.Show("The assembly named" +
     vcar.AssemblyName + " was added." + "\n" 
    + "Its culture and description are: " 
    + "\n" + vcar.Culture + vcar.Description);
            }
            mycollection = (IVCCollection)prj.Configurations;
            MessageBox.Show
    ("The number of configurations in the project is: " 
    + mycollection.Count);
            for(idx = 1;idx <= mycollection.Count;idx++)
            {
                cfg = (VCConfiguration)mycollection.Item(idx);
                sTest = sTest + "Configuration name: " 
    + cfg.Name + "\n" + "Its 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 is: " 
    + mycollection2.Count);
            for(idx = 1;idx <= mycollection2.Count;idx++)
            {
                file = (VCFile)mycollection2.Item(idx);
                MessageBox.Show("The file extension of item # " 
    + idx.ToString() + " is: " + file.Extension);
            }
        }
        catch (SystemException ex)
        {
            MessageBox.Show("ERROR: " + ex.ToString());
        }
    }
    
  5. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio integrated development environment (IDE).

  7. On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

    The add-in 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.

Example

public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst,
 ref System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    VCProjectEngineExample((DTE2)_applicationObject);
}

public void VCProjectEngineExample(DTE2 dte)
{
    VCProject prj;
    VCAssemblyReference vcar;
    int idx;
    IVCCollection mycollection, mycollection2;
    VCConfiguration cfg;
    String sTest = "Configuration Information:" + "\n";
    VCFile file;
    try
    {
        prj = (VCProject)dte.Solution.Projects.Item(1).Object;
        // Make sure the path to the assembly
        // is correct. The installation root in the 
        // example is "C:", but it might be different
        // on your computer.
        if (prj.CanAddAssemblyReference
("C:\\Program Files\\Microsoft Visual Studio 8\
\Common7\\IDE\\PublicAssemblies\\VSLangProj.dll"))
        {
            vcar= (VCAssemblyReference)prj.AddAssemblyReference
("C:\\Program Files\\Microsoft Visual Studio 8
\\Common7\\IDE\\PublicAssemblies\\VSLangProj.dll");

            MessageBox.Show("The assembly named" + vcar.AssemblyName 
+ " was added." + "\n" + "Its culture and description are: " + "\n" 
+ vcar.Culture + vcar.Description);
        }
        mycollection = (IVCCollection)prj.Configurations;
        MessageBox.Show
("The number of configurations in the project is: " 
+ mycollection.Count);
        for(idx = 1;idx <= mycollection.Count;idx++)
        {
            cfg = (VCConfiguration)mycollection.Item(idx);
            sTest = sTest + "Configuration name: " +cfg.Name + "\n" 
+ "Its plattform: " + "\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 is: " 
+ mycollection2.Count);
        for(idx = 1;idx <= mycollection2.Count;idx++)
        {
            file = (VCFile)mycollection2.Item(idx);
            MessageBox.Show("The file extension of item # " 
+ idx.ToString() + " is: " + file.Extension);
        }
    }
    catch (SystemException ex)
    {
        MessageBox.Show("ERROR: " + ex.ToString());
    }
}

Compiling the Code

To compile this code, create a new Visual Studio add-in project and replace the code of the OnConnection method in the Connect class with the code in the example. For information about how to run an add-in, see How to: Control Add-ins with the Add-In Manager.

See Also

Community Additions

ADD
Show:
© 2015 Microsoft