Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Extending Managed VSPackages with Automation

Visual Studio .NET 2003

Microsoft Corporation

February 2004

Applies to:
    Microsoft® Visual Studio® .NET 2003

Summary: Learn how to create a managed VSPackage that programs the Visual Studio .NET environment through automation. (3 printed pages)

This walkthrough guides you through the steps of creating a managed VSPackage that programs the Microsoft® Visual Studio® .NET environment through automation. You can create a sample managed VSPackage using the Package Wizard. Use automation with the resulting VSPackage to display Visual Studio .NET properties in the Output window. Carry out the following steps to create a Managed VSPackage and then extend it with automation:

  • Create the managed VSPackage.
  • Call the Automation Model.

To create a managed VSPackage

  1. Create a new project called Auto.
    1. On the File menu, point to New, and then click Project.
    2. In the New Project dialog box, expand the Other Projects node, and select Extensibility Projects.
    3. Click the Visual Studio Integration Package icon in the Templates pane.
    4. In the Location box, type the file path for your VSPackage. In the Name box, type Auto.
    5. Click OK to start the wizard.
  2. In the Select a Programming Language page:
    • Set the language to Visual C#.
  3. In the Select VSPackage Options page, check the Menu Command checkbox. Accept all other defaults.

    The wizard generates a managed project Auto and an unmanaged resource-only project AutoUI.

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

To call the automation model

  1. In the Solution window, right-click the Auto project node and select Add Reference.
    • In the .NET tab view of the Add Reference dialog box, double-click envdte and then click OK.

    This establishes a reference to the EnvDTE namespace.

  2. Add this boldfaced line to the top of the VsPkg.cs file:
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.VSIP;
    using EnvDTE;
  3. Expand the IOleCommandTarget Members region and find the Exec method.
  4. Add these boldfaced lines to the beginning of the PkgCmdIDList case statement:
    case PkgCmdIDList.cmdidMyCommand:
    case PkgCmdIDList.cmdidMyCommand:
     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(EnvDTE.Constants.vsWindowKindOutput);
        window.Visible = true;
        OutputWindow outputWindow = (OutputWindow) window.Object;
        OutputWindowPane outputWindowPane =

    This code uses Package.GetService to get a DTE automation object that represents the Visual Studio .NET environment. Using automation, a new Test pane is created in the Output window and the Visual Studio .NET name and version is written to it.

  5. Build and launch the Auto project in debug mode by pressing the keyboard shortcut, F5.

    This launches Visual Studio .NET Exp.

    Note   Both versions of Visual Studio are open at this time.
  6. In Visual Studio .NET Exp, select My Menu Item from the Tools item.

    In addition to an alert box, you should see a new Test pane open in the Output window that displays something like:

    Name is Microsoft Development Environment
    Version is 7.10

For more information on Automation samples, see Automation Samples for Visual Studio .NET.

For more information about automation and extensibility, see Automation and Extensibility Overview.

© 2014 Microsoft. All rights reserved.