Walkthrough: Calling into the Visual Studio SDK from Automation
This walkthrough illustrates how to create a Visual Studio 2008 add-in that consumes a Visual Studio 2008 service. Visual Studio 2008 consists of a set of VSPackages that offer and use services. 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 2008 or VSIP 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 2008 add-in using the Visual Studio 2008 Add-In Wizard.
To create an add-in
Open Visual Studio 2008. On the File menu, point to New and then click New Project.
The New Project dialog box appears.
In the left pane of the New Project dialog box, expand the Other Project Types node and then click the Extensibility node.
Create a new Visual Studio 2008 Add-In project named Addin.
The Visual Studio 2008 Add-In wizard runs.
On the Select a Programming Language page, select Create an Add-In using Visual C#.
On the Select an Application Host page, select Microsoft Visual Studio 2008 and clear Microsoft Visual Studio 2008 Macros.
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 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.
The wizard generates the managed add-in project Addin.
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 and add these lines to the using statements:
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") clause of the Exec method:
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 2008.
注意
Two instances of Visual Studio 2008 are open at this time.
In the new Visual Studio 2008 instance, on the Tools menu, click Addin. The message box displays the following:
MyAddin Inside Addin.Connect