Logo
HOWTO: Guessing the IDE mode (design, debug or run-time) from a Visual Studio package

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2010
Date: June 2015   Microsoft Visual Studio 2012
      Microsoft Visual Studio 2013
      Microsoft Visual Studio 2015
Introduction

The IDE can be in one of these modes:

  • Design-time: this is the mode that you use to code or design your application.
  • Run-time: this is the mode when your application is running.
  • Debug-time or Break-time: this is the mode when you debug your running application.

Your package may want to know which is the current mode of the IDE, and to be notified when the mode changes.

More Information

To get both the current mode and the notification when the mode changes your package needs to use the debugger, represented by the IVsDebugger interface. The IVsDebugger interface offers several methods (to handle breakpoints, etc.) and a GetMode method to get the current mode. To be notified when the mode changes you need to use the AdviseDebuggerEvents / UnadviseDebuggerEvents methods passing a class that implements the IVsDebuggerEvents interface (it can be the own package), which has only the OnModeChange method.

The following sample shows a package that gets the debugger mode when initialized and is notified when the debugger mode changes. Notice that the package is marked to load when a solution exists.

Language: C#   Copy Code Copy Code (IE only)
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;

namespace Company.VSPackageDebuggerEvents
{
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [Guid(GuidList.guidVSPackageDebuggerEventsPkgString)]
   [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
   public sealed class VSPackageDebuggerEventsPackage: Package, IVsDebuggerEvents
   {
      private IVsDebugger m_debugger = null;
      private uint m_cookie = 0;

      public VSPackageDebuggerEventsPackage()
      {
      }

      protected override void Initialize()
      {
         int hr;
         DBGMODE[] modeArray = new DBGMODE[1];

         try
         {
            base.Initialize();

            m_debugger = base.GetService(typeof(SVsShellDebugger)) as IVsDebugger;
            if (m_debugger != null)
            {
               hr = m_debugger.AdviseDebuggerEvents(this, out m_cookie);
               Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);

               hr = m_debugger.GetMode(modeArray);
               Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
               ShowMode(modeArray[0]);
            }
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      protected override int QueryClose(out bool canClose)
      {
         int hr = VSConstants.S_OK;

         canClose = true;

         try
         {
            if (m_debugger != null)
            {
               if (m_cookie != 0)
               {
                  hr = m_debugger.UnadviseDebuggerEvents(m_cookie);
                  Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
                  m_cookie = 0;
               }
            }
            hr = base.QueryClose(out canClose);
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
         return hr;
      }

      public int OnModeChange(DBGMODE dbgmodeNew)
      {
         ShowMode(dbgmodeNew);

         return VSConstants.S_OK;
      }

      private void ShowMode(DBGMODE mode)
      {
         string msg = "";

         // Remove the DBGMODE.DBGMODE_Enc flag if present
         mode = mode & ~DBGMODE.DBGMODE_EncMask;

         switch (mode)
         {
            case DBGMODE.DBGMODE_Design:

               msg = "Entered mode: Design";
               break;

            case DBGMODE.DBGMODE_Break:

               msg = "Entered mode: Break";
               break;

            case DBGMODE.DBGMODE_Run:

               msg = "Entered mode: Run";
               break;
         }
         Debug.WriteLine(msg);
      }
   }
}


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