다음을 통해 공유


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

  1. Open Visual Studio. On the File menu, point to New and then click New Project.

    The New Project dialog box appears.

  2. In the left pane of the New Project dialog box, expand the Other Project Types node and then click the Extensibility node.

  3. Create a new Visual Studio Add-In project named Addin.

    The Visual Studio Add-In wizard runs.

  4. On the Select a Programming Language page, select Create an Add-In using Visual C# or Create an Add-In using Visual Basic.

  5. On the Select an Application Host page, select Microsoft Visual Studio 2010 and clear Microsoft Visual Studio 2010 Macros.

  6. On the Enter a Name and Description page, type MyAddin in the Name box and MyAddin Walkthrough in the Description box.

  7. 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.

  8. Accept all other defaults.

  9. 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

  1. 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;
    
  2. Right-click the project node in Solution Explorer and add these .NET references:

    Microsoft.VisualStudio.OLE.Interop
    Microsoft.VisualStudio.Shell.Interop
    
  3. 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.

  4. Build and start the Addin project in debug mode by pressing F5.

    This starts another instance of Visual Studio.

  5. In the new Visual Studio instance, on the Tools menu, click Addin. The message box displays this:

    MyAddin
    Inside Addin.Connect
    

See Also

Tasks

How to: Deactivate and Remove an Add-In