Logo
HOWTO: Write to the output window panes from a Visual Studio package.

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

This article shows sample code to write to the different panes (Build, Debug, General) of the Output window of Visual Studio from a package.

More Information

Visual Studio provides the Microsoft.VisualStudio.Shell.Interop.SVsOutputWindow service and its Microsoft.VisualStudio.Shell.Interop.IVsOutputWindow interface to get the output window. The GetPane method allows you to get the desired pane identified by a guid defined in the  Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid class.

The following package writes to the three panes (Build, Debug and General) of the Output window when the button created on the Tools menu is clicked:

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

namespace Company.CSharpOutputWindowVSPackage
{
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [ProvideMenuResource("Menus.ctmenu", 1)]
   [Guid(GuidList.guidCSharpOutputWindowVSPackagePkgString)]
   public sealed class CSharpOutputWindowVSPackagePackage : Package
   {
      public CSharpOutputWindowVSPackagePackage()
      {
      }

      protected override void Initialize()
      {
         OleMenuCommandService oleMenuCommandService;
         CommandID commandID;
         MenuCommand menuCommand;

         base.Initialize();

         oleMenuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

         if (oleMenuCommandService != null)
         {
            commandID = new CommandID(GuidList.guidCSharpOutputWindowVSPackageCmdSet, (int)PkgCmdIDList.cmdidMyCommand);
         
            menuCommand = new MenuCommand(MenuItemCallback, commandID);
            
            oleMenuCommandService.AddCommand(menuCommand);
         }
      }

      private void MenuItemCallback(object sender, EventArgs e)
      {
         try
         {
            OutputString(Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.DebugPane_guid, "Hello World in Debug pane");
            OutputString(Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid, "Hello World in Build pane");
            OutputString(Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.GeneralPane_guid, "Hello World in General pane");
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      private void OutputString(Guid guidPane, string text)
      {
         const int VISIBLE = 1;
         const int DO_NOT_CLEAR_WITH_SOLUTION = 0;

         IVsOutputWindow outputWindow;
         IVsOutputWindowPane outputWindowPane = null;
         int hr;

         // Get the output window
         outputWindow = base.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
         
         // The General pane is not created by default. We must force its creation
         if (guidPane == Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.GeneralPane_guid)
         {
            hr = outputWindow.CreatePane(guidPane, "General", VISIBLE, DO_NOT_CLEAR_WITH_SOLUTION);
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);
         }

         // Get the pane
         hr = outputWindow.GetPane(guidPane, out outputWindowPane);
         Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);

         // Output the text
         if (outputWindowPane != null)
         {
            outputWindowPane.Activate();
            outputWindowPane.OutputString(text);
         }
      }
   }
}
Language: VB.NET   Copy Code Copy Code (IE only)
Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.ComponentModel.Design
Imports Microsoft.Win32
Imports Microsoft.VisualStudio
Imports Microsoft.VisualStudio.Shell.Interop
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell

<PackageRegistration(UseManagedResourcesOnly:=True), _
 InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), _
 ProvideMenuResource("Menus.ctmenu", 1), _
 Guid(GuidList.guidVBNetOutputWindowVSPackagePkgString)> _
Public NotInheritable Class VBNetOutputWindowVSPackagePackage
   Inherits Package

   Public Sub New()
   End Sub

   Protected Overrides Sub Initialize()

      Dim oleMenuCommandService As OleMenuCommandService
      Dim menuCommandID As CommandID
      Dim menuItem As MenuCommand

      MyBase.Initialize()

      oleMenuCommandService = TryCast(GetService(GetType(IMenuCommandService)), OleMenuCommandService)

      If oleMenuCommandService IsNot Nothing Then

         menuCommandID = New CommandID(GuidList.guidVBNetOutputWindowVSPackageCmdSet, CInt(PkgCmdIDList.cmdidMyCommand))

         menuItem = New MenuCommand(New EventHandler(AddressOf MenuItemCallback), menuCommandID)

         oleMenuCommandService.AddCommand(menuItem)

      End If

   End Sub

   Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)

      Try

         OutputString(Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.DebugPane_guid, "Hello World in Debug pane")
         OutputString(Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid, "Hello World in Build pane")
         OutputString(Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.GeneralPane_guid, "Hello World in General pane")

      Catch ex As Exception

         System.Windows.Forms.MessageBox.Show(ex.ToString())

      End Try

   End Sub

   Private Sub OutputString(guidPane As Guid, text As String)

      Const VISIBLE As Integer = 1
      Const DO_NOT_CLEAR_WITH_SOLUTION As Integer = 0

      Dim outputWindow As IVsOutputWindow
      Dim outputWindowPane As IVsOutputWindowPane = Nothing
      Dim hr As Integer

      ' Get the output window
      outputWindow = DirectCast(MyBase.GetService(GetType(SVsOutputWindow)), IVsOutputWindow)

      ' The General pane is not created by default. We must force its creation
      If guidPane = Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.GeneralPane_guid Then

         hr = outputWindow.CreatePane(guidPane, "General", VISIBLE, DO_NOT_CLEAR_WITH_SOLUTION)
         Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr)

      End If

      ' Get the pane
      hr = outputWindow.GetPane(guidPane, outputWindowPane)
      Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr)

      ' Output the text
      If outputWindowPane IsNot Nothing Then

         outputWindowPane.Activate()
         outputWindowPane.OutputString(text)

      End If

   End Sub

End Class

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top