Compartilhar via


How to: Get References to the DTE and DTE2 Objects

Note

In Visual Studio 2013, add-ins are deprecated. We recommend that you upgrade your add-ins to VSPackage extensions. For more information, see FAQ: Converting Add-ins to VSPackage Extensions.

In the EnvDTE assembly, the DTE object represents the Visual Studio integrated development environment (IDE) and is the highest-level object in the automation-model hierarchy. To gain access to the core automation model, an automation application must have a reference to this object.

However, the addition of the EnvDTE80 assembly provides a replacement top-level object, which is named DTE2 and supersedes the DTE object. Although both objects act—and are programmed—similarly, DTE2 contains new functionality and hosts new and updated objects and collections.

When you create new automation applications, we recommend that you create references to both objects—to the DTE2 object to provide access to the new functionality and to the DTE object to provide access to the rest of the core functionality. We also recommend that whenever possible you use the new objects and collections in DTE2 instead of those in DTE.

The following steps show how to get a reference to the DTE2 object. (The same procedure applies to the DTE object.) Before you add a reference to an object, you must add references to the appropriate assembly and type library. For more information, see How to: Add References to Automation Namespaces.

The programmatic identifier (ProgID) to use for Visual Studio 2013 is VisualStudio.DTE.12.0. You can then cast the returned object into a DTE2 interface.

When they are called, the DTE2 properties, methods, and events return the DTE types. For example, Solution returns a Solution object, not a Solution2 object as one might expect. This is why, when you use the DTE2 members or other type members, you must explicitly typecast them. This design promotes consistency and reduces confusion. The EnvDTE80.dll assembly consistently returns the same interface for all of the DTE2 properties. Returning the latest interface version would be confusing—for example, if a future version of Visual Studio were to have a DTE3 type, then some interfaces could return DTE, some DTE2, and some DTE3. Furthermore, it could create COM interop problems because the "2" interfaces in EnvDTE80 derive from the EnvDTE interfaces. For example, Window2 derives from Window; if a DTE property were added to Window2, it would hide the Windows property and not work correctly with COM applications.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To reference the DTE2 object in Visual Basic and C#

  • In your code, add this:

    ' Get an instance of the currently running Visual Studio IDE.
    Dim DTE2 as EnvDTE80.DTE2
    DTE2 = System.Runtime.InteropServices.Marshal. _
    GetActiveObject("VisualStudio.DTE.12.0")
    
    // Get an instance of the currently running Visual Studio IDE.
    EnvDTE80.DTE2 dte2;
    dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
    GetActiveObject("VisualStudio.DTE.12.0");
    

To reference the DTE or DTE2 object in Visual C++ (ATL)

  • In your code, add this:

    CComPtr<EnvDTE::_DTE> m_pDTE;
    CComPtr<EnvDTE80::DTE2> m_pDTE2;
    CLSID clsid;
    CLSID clsid2;
    CLSIDFromProgID(L"VisualStudio.DTE.12.0",&clsid);
    CLSIDFromProgID(L"VisualStudio.DTE.12.0",&clsid2);
    CComPtr<IUnknown> punk;
    CComPtr<IUnknown> punk2;
    // Get a running instance of Visual Studio.
    HRESULT hr = GetActiveObject(clsid,NULL,&punk);
    hr = GetActiveObject(clsid2,NULL,&punk2);
    m_pDTE = punk;
    m_pDTE2 = punk2;
    

See Also

Tasks

How to: Add References to Automation Namespaces

How to: Control Add-Ins By Using the Add-In Manager

Concepts

Attaching to Specific Instances of the IDE

Add-In Registration

Automation Object Model Chart

Other Resources

Creating Add-ins and Wizards