locked
Detect ActiveConfiguration has changed RRS feed

  • Question

  • Hi,

    I want to detect from my AddIn when the ActiveConfiguration of the project has changed, is this posible?
    I want to know if for example configuration changed from Debug to Release

    Regards.
    Thursday, May 7, 2009 9:57 PM

Answers

  • I don't think there's any easy way to do this. At least nothing in the stock automation methods. However, if you have the Visual Studio SDK installed, you can implement IVsUpdateSolutionEvents and it's OnActiveProjectCfgChange method will be called whenever the active solution configuration or platform is changed. For example:

    using

     

    System;
    using System.Diagnostics;
    using Extensibility;
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;

    using
    IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

    namespace
    TestCfgChange
    {
       public class Connect : IDTExtensibility2, IVsUpdateSolutionEvents
       {
          private DTE2 applicationObject;
          private AddIn addInInstance;
          private ServiceProvider serviceProvider;
          private uint useCookie;

          public
    Connect() {}

          public
    void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
          {
             applicationObject = (
    DTE2)application;
             addInInstance = (
    AddIn)addInInst;
             serviceProvider =
    new ServiceProvider((IOleServiceProvider)application);
             IVsSolutionBuildManager vsSlnBldMgr = (IVsSolutionBuildManager)serviceProvider.GetService(typeof(SVsSolutionBuildManager));
             vsSlnBldMgr.AdviseUpdateSolutionEvents(
    this, out useCookie);
          }

          public
    void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
          {
             if (useCookie != 0)
             {
                IVsSolutionBuildManager vsSlnBldMgr = serviceProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager;
                if (vsSlnBldMgr != null)
                   vsSlnBldMgr.UnadviseUpdateSolutionEvents(useCookie);

                useCookie = 0;
             }
          }

          public
    int OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
          {
             // Note this gets called for each project, and once with pIVsHierarchy
             // set to null. We only want to process this once, so do this when invoked
             // with pIVsHierarchy == null
             if (pIVsHierarchy == null)
             {
                SolutionConfiguration2 slnCfg = (SolutionConfiguration2)applicationObject.Solution.SolutionBuild.ActiveConfiguration;
                Debug.WriteLine(string.Format("Active Config changed to {0}:{1}", slnCfg.Name, slnCfg.PlatformName));
             }

             return 0;
          }

          ..............................

       }
    }

    This should get you pointed in the right direction if you decide to go down that road. :-)


    Ed Dore
    Friday, May 8, 2009 6:59 PM

All replies

  • So far as I know, this is not possible.
    Return to the source, learn the way!
    Friday, May 8, 2009 1:01 AM
  • I don't think there's any easy way to do this. At least nothing in the stock automation methods. However, if you have the Visual Studio SDK installed, you can implement IVsUpdateSolutionEvents and it's OnActiveProjectCfgChange method will be called whenever the active solution configuration or platform is changed. For example:

    using

     

    System;
    using System.Diagnostics;
    using Extensibility;
    using EnvDTE;
    using EnvDTE80;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;

    using
    IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

    namespace
    TestCfgChange
    {
       public class Connect : IDTExtensibility2, IVsUpdateSolutionEvents
       {
          private DTE2 applicationObject;
          private AddIn addInInstance;
          private ServiceProvider serviceProvider;
          private uint useCookie;

          public
    Connect() {}

          public
    void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
          {
             applicationObject = (
    DTE2)application;
             addInInstance = (
    AddIn)addInInst;
             serviceProvider =
    new ServiceProvider((IOleServiceProvider)application);
             IVsSolutionBuildManager vsSlnBldMgr = (IVsSolutionBuildManager)serviceProvider.GetService(typeof(SVsSolutionBuildManager));
             vsSlnBldMgr.AdviseUpdateSolutionEvents(
    this, out useCookie);
          }

          public
    void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
          {
             if (useCookie != 0)
             {
                IVsSolutionBuildManager vsSlnBldMgr = serviceProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager;
                if (vsSlnBldMgr != null)
                   vsSlnBldMgr.UnadviseUpdateSolutionEvents(useCookie);

                useCookie = 0;
             }
          }

          public
    int OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
          {
             // Note this gets called for each project, and once with pIVsHierarchy
             // set to null. We only want to process this once, so do this when invoked
             // with pIVsHierarchy == null
             if (pIVsHierarchy == null)
             {
                SolutionConfiguration2 slnCfg = (SolutionConfiguration2)applicationObject.Solution.SolutionBuild.ActiveConfiguration;
                Debug.WriteLine(string.Format("Active Config changed to {0}:{1}", slnCfg.Name, slnCfg.PlatformName));
             }

             return 0;
          }

          ..............................

       }
    }

    This should get you pointed in the right direction if you decide to go down that road. :-)


    Ed Dore
    Friday, May 8, 2009 6:59 PM