Share via


How to: Get References to the DTE and DTE2 Objects

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. All automation applications must have a reference to this object to gain access to the core automation model. That continues to be the case with Visual Studio 2005.

But the addition of the EnvDTE80 assembly provides a replacement top-level object named DTE2 which supersedes the DTE object. Both objects act and program similarly, but DTE2 contains new functionality and hosts a number of new and updated objects and collections.

As a result, when creating new automation applications and add-ins, you should create references to both objects. The new DTE2 object provides access to the new functionality, while the DTE object provides access to the remainder of the core automation functionality. The new objects and collections under DTE2 should be used instead of the objects and collections in DTE whenever possible.

The following procedures show how to get a reference to the DTE2 object. (The same procedure applies to the DTE object.) Before adding references to the objects, you must add references to the appropriate assemblies and type library. For more information, see How to: Add References to the EnvDTE, EnvDTE80, and EnvDTE90 Namespaces.

The programmatic identifier (ProgID) to use is VisualStudio.DTE.8.0. You can then cast the returned object to a DTE2 interface.

The DTE2 properties, methods, and events, when called, return the DTE types. For example, Solution returns a Solution object, not a Solution2 object as you might expect. This is why, when using the DTE2 members or other type members, you must explicitly typecast them. This was done intentionally for a number of reasons. The first reason is to promote consistency and reduce 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 has a DTE3 type, then some interfaces could return DTE, some DTE2, and some DTE3. Furthermore, it would pose 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, then it would hide the Windows property and would not work correctly with COM applications.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and ExportSettings on the Tools menu. For more information, see Visual Studio Settings.

To reference the DTE2 object in Visual Basic and C#

  • In your code, add the following:

    ' Get an instance of the currently running Visual Studio IDE.
    Dim DTE2 as EnvDTE80.DTE2
    DTE2 = System.Runtime.InteropServices.Marshal. _
    GetActiveObject("VisualStudio.9.0.DTE")
    
    // Get an instance of the currently running Visual Studio IDE.
    EnvDTE80.DTE2 dte2;
    dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
    GetActiveObject("VisualStudio.DTE.9.0");
    
  • To reference the Macros IDE object, in your code, add the following:

    ' Get an instance of the currently running Macros IDE.
    Dim DTE2 as EnvDTE80.DTE2
    Dim MacrosDTE as EnvDTE.DTE
    DTE2 = System.Runtime.InteropServices.Marshal.GetActiveObject_
    ("VisualStudio.9.0.DTE")
    MacrosDTE = DTE2.MacrosIDE
    
    // Get an instance of the currently running Macros IDE.
    EnvDTE80.DTE2 dte2;
    EnvDTE80.DTE MacrosDTE;
    dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
    GetActiveObject("VisualStudio.DTE.9.0");
    MacrosDTE = dte2.MacrosIDE;
    

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

  • In your code, enter the following:

    CComPtr<EnvDTE::_DTE> m_pDTE;
    CComPtr<EnvDTE80::DTE2> m_pDTE2;
    CLSID clsid;
    CLSID clsid2;
    CLSIDFromProgID(L"VisualStudio.DTE.9.0",&clsid);
    CLSIDFromProgID(L"VisualStudio.DTE.9.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;
    
  • To reference the Macros IDE object, in your code, add the following:

    // Get an instance of the currently running Macros IDE.
    CComPtr<EnvDTE80::DTE2> m_pDTE2;
    CComPtr<EnvDTE::_DTE> m_pMacrosIDE;
    CLSID clsid;
    CLSIDFromProgID(L"VisualStudio.DTE.10.0",&clsid);
    CComPtr<IUnknown> punk;
    HRESULT hr = GetActiveObject(clsid,NULL,&punk);
    m_pDTE = punk;
    m_pDTE->get_MacrosIDE((void **)&m_pMacrosIDE);
    

See Also

Tasks

How to: Add References to the EnvDTE, EnvDTE80, and EnvDTE90 Namespaces

How to: Control Add-ins with 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

Change History

Date

History

Reason

December 2009

Revised code samples.

Customer feedback.