Logo
HOWTO: Get additional include directories of Visual C++ project from an add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: January 2014   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
Introduction

Visual C++ projects have standard include directories for each configuration / platform, and additional include directories. This article provides the sample code to get the additional include directories from an add-in. To get the standard include directories, see the article HOWTO: Get standard include directories of Visual C++ project from an add-in.

More Information

To get specific Visual C++ project information from an add-in, you need to add a reference to the Microsoft.VisualStudio.VCProjectEngine.dll assembly. There are specific versions of this assembly for each Visual Studio version:

  • 8.0.0.0 for Visual Studio 2005
  • 9.0.0.0 for Visual Studio 2008
  • 10.0.0.0 for Visual Studio 2010
  • 11.0.0.0 for Visual Studio 2012
  • 12.0.0.0 for Visual Studio 2013

Once added, you can use the following code to get the additional include directories of each configuration / platform. The add-in, once loaded, shows the additional include directories of the first project of the solution:

Language: C#   Copy Code Copy Code (IE only)
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;

namespace MyAddin
{
   public class Connect : IDTExtensibility2
   {
      private DTE2 _applicationObject;
      private AddIn _addInInstance;

      public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
      {
         _applicationObject = (DTE2)application;
         _addInInstance = (AddIn)addInInst;

         switch (connectMode)
         {
            case ext_ConnectMode.ext_cm_Startup:
               // Do nothing. OnStartupComplete will be called
               break;

            case ext_ConnectMode.ext_cm_AfterStartup:

               InitializeAddIn();
               break;
         }
      }

      public void OnStartupComplete(ref Array custom)
      {
         InitializeAddIn();
      }

      private void InitializeAddIn()
      {
         EnvDTE.Project project;

         try
         {
            project = _applicationObject.Solution.Projects.Item(1);

            ShowAdditionalIncludeDirectories(project);
         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.ToString());
         }
      }

      private void ShowAdditionalIncludeDirectories(EnvDTE.Project project)
      {
         Microsoft.VisualStudio.VCProjectEngine.VCProject proj;
         Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool compilerTool;
         Microsoft.VisualStudio.VCProjectEngine.IVCCollection toolsCollection;
         Microsoft.VisualStudio.VCProjectEngine.IVCCollection configurationsCollection;

         proj = (Microsoft.VisualStudio.VCProjectEngine.VCProject)project.Object;

         configurationsCollection = (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)proj.Configurations;

         foreach (Microsoft.VisualStudio.VCProjectEngine.VCConfiguration configuration in configurationsCollection)
         {
            toolsCollection = (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)configuration.Tools;

            foreach (Object toolObject in toolsCollection)
            {
               if (toolObject is Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)
               {
                  compilerTool = (Microsoft.VisualStudio.VCProjectEngine.VCCLCompilerTool)toolObject;

                  MessageBox.Show(configuration.Name + ": " + compilerTool.AdditionalIncludeDirectories);

                  break;
               }
            }
         }
      }

      public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
      {
      }

      public void OnAddInsUpdate(ref Array custom)
      {
      }

      public void OnBeginShutdown(ref Array custom)
      {
      }
   }
}

Related articles



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top