Walkthrough: Calling into the Visual Studio SDK By Using Automation
This walkthrough illustrates how to create a Visual Studio add-in that consumes a Visual Studio service. The add-in you create gets a service provider and uses it to obtain a service. You can use this same technique to obtain any proffered Visual Studio service. For more information on services and service providers, see Services. The procedures below demonstrate how to create an add-in and then obtain a service from the add-in.
Creating an Add-In
In this section, you create a Visual Studio add-in using the Visual Studio Add-In project template.
To create an add-in
Create a new Visual Studio project (File/New/Project).
In the left pane of the New Project dialog box, expand the Other Project Types node and then click the Extensibility node. Select Visual Studio Add-in.
Create a new add-in project named Addin.
In the Visual Studio Add-In wizard, click Next.
On the Select a Programming Language page, select Create an Add-In using Visual C# or Create an Add-In using Visual Basic.
On the Select an Application Host page, select Microsoft Visual Studio 2012.
On the Enter a Name and Description page, type MyAddin in the Name box and MyAddin Walkthrough in the Description box.
On the Choose Add-In Options page, select the option for a Tools menu item under Would you like to create command bar UI for your Add-In?. Clear the other check boxes.
Accept all other defaults.
Build the solution and verify that it compiles without errors.
Obtaining a Service from an Add-In
The following steps guide you through acquiring a service from your add-in.
To obtain a service
Open the file Connect.cs or Connect.vb and add these lines to the using (C#) or Imports (Visual Basic) statements:
Imports System.Runtime.InteropServices Imports Microsoft.VisualStudio.OLE.Interop Imports Microsoft.VisualStudio.Shell.Interop Imports IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider
using System.Runtime.InteropServices; using Microsoft.VisualStudio.OLE.Interop; using Microsoft.VisualStudio.Shell.Interop; using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
Right-click the project node in Solution Explorer and add these .NET references:
Microsoft.VisualStudio.OLE.Interop Microsoft.VisualStudio.Shell.Interop
Add these lines of code to the if(commandName == "Addin.Connect.Addin") or If commandName = "Addin.Connect.Addin" Then clause of the Exec method:
If commandName = "Addin.Connect.Addin" Then Dim sp As IOleServiceProvider = DirectCast(_applicationObject, IOleServiceProvider) Dim SID As Guid = GetType(SVsUIShell).GUID Dim IID As Guid = GetType(IVsUIShell).GUID Dim output As IntPtr sp.QueryService(SID, IID, output) Dim uiShell As IVsUIShell = DirectCast(Marshal.GetObjectForIUnknown(output), IVsUIShell) Dim clsid As Guid = Guid.Empty Dim result As Integer uiShell.ShowMessageBox(0, clsid, "MyAddin", String.Format(System.Globalization.CultureInfo.CurrentCulture, "Inside " & Me.ToString()), String.Empty, 0, _ OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, result) handled = True Return End If
if (commandName == "Addin.Connect.Addin") { IOleServiceProvider sp = (IOleServiceProvider) _applicationObject; Guid SID = typeof(SVsUIShell).GUID; Guid IID = typeof(IVsUIShell).GUID; IntPtr output; sp.QueryService(ref SID, ref IID, out output); IVsUIShell uiShell = (IVsUIShell)Marshal.GetObjectForIUnknown(output); Guid clsid = Guid.Empty; int result; uiShell.ShowMessageBox( 0, ref clsid, "MyAddin", string.Format( CultureInfo.CurrentCulture, "Inside " + this.ToString()), string.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, out result); handled = true; return; }
This code casts the current application object (type DTE2) into an IOleServiceProvider, then calls QueryService to obtain the SVsUIShell service. This service provides an IVsUIShell interface. The ShowMessageBox method displays a message box when the add-in runs.
Build and start the Addin project in debug mode by pressing F5.
This starts another instance of Visual Studio.
In the new Visual Studio instance, on the Tools menu, click Addin. The message box displays this:
MyAddin Inside Addin.Connect