Partilhar via


Walkthrough: Extending Managed VSPackages By Using Automation

This walkthrough illustrates how to use automation to create a managed VSPackage that manipulates the Visual Studio integrated development environment (IDE). You create a sample managed VSPackage and then use automation methods in the resulting VSPackage to display Visual Studio properties in the Output window.

Prerequisites

To complete this walkthrough, you must install the Visual Studio 2010 SDK.

Note

For more information about the Visual Studio SDK, see Extending Visual Studio Overview. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.

Locations for the Visual Studio Package Project Template

The Visual Studio Package project template can be found in three different locations in the New Project dialog:

  1. Under Visual Basic Extensibility. The default language of the project is Visual Basic.

  2. Under C# Extensibility. The default language of the project is C#.

  3. Under Other Project Types Extensibility. The default language of the project is C++.

To create a managed VSPackage

  1. Create a new Visual Studio Package project named Auto.

    For more information about how to create a managed VSPackage, see Walkthrough: Creating a Menu Command By Using the Visual Studio Package Template.

  2. On the Select a Programming Language page, set the language to Visual C#.

  3. Keep the default values in the Basic VSPackage Information page.

  4. On the Select VSPackage Options page, select the Menu Command check box.

  5. On the Command Options page, change the Command Name to Auto.

  6. Click the Finish button.

    The template generates a managed project named Auto.

  7. Build the solution and verify that it compiles without errors.

To call the Automation model

  1. In the Solution Explorer window, right-click the Auto project node and then click Add Reference.

  2. On the .NET tab of the Add Reference dialog box, double-click EnvDTE.

    This adds a reference to the EnvDTE automation namespace.

  3. In the AutoPackage file, add the following namespace reference.

    Imports EnvDTE
    
    using EnvDTE;
    
  4. In the AutoPackage file, replace the body of the MenuItemCallback method with the following lines:

    Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)
        Dim dte As DTE
        dte = CType(GetService(GetType(DTE)), DTE)
        Dim myString As String = "Name is " & dte.Name + vbCrLf & "Version is " & dte.Version
    
        Dim windows As Windows = dte.Windows
        Dim window As Window = CType(windows.Item(EnvDTE.Constants.vsWindowKindOutput), Window)
        window.Visible = True
    
        Dim outputWindow As OutputWindow = CType(window.Object, OutputWindow)
        Dim outputWindowPane As OutputWindowPane = outputWindow.OutputWindowPanes.Add("Test")
        outputWindowPane.Activate()
        outputWindowPane.OutputString(myString)
    End Sub
    
    private void MenuItemCallback(object sender, EventArgs e)
    {
        DTE dte;
        dte = (DTE)GetService(typeof(DTE));
        string myString =
          "Name is " + dte.Name + "\rVersion is " + dte.Version;
    
        Windows windows = dte.Windows;
        Window window =
        (Window)windows.Item("{34E76E81-EE4A-11D0-AE2E-00A0C90FFFC3}");
        window.Visible = true;
    
        OutputWindow outputWindow = (OutputWindow)window.Object;
        OutputWindowPane outputWindowPane =
        outputWindow.OutputWindowPanes.Add("Test");
        outputWindowPane.Activate();
        outputWindowPane.OutputString(myString);
    }
    

This code calls GetService to obtain a DTE automation object that represents the Visual Studio IDE. The automation code in MenuItemCallback creates a new pane in the Output window named Test. The Visual Studio name and version is then written to the new Output pane.

  1. Build and start the Auto project in debug mode by pressing F5.

    This starts the Visual Studio experimental build (Visual Studio Exp).

    Note

    Both versions of Visual Studio are open at this point.

  2. In Visual Studio Exp, on the Tools menu, click Auto.

    A new pane named Test opens in the Output window and displays the following:

    Name is Microsoft Visual Studio
    Version is x.xx
    

    Where x.xx is the latest Visual Studio version number.

    For more information about automation samples, see Automation Samples for Visual Studio.

See Also

Other Resources

Extending the Visual Studio Environment