Share via


Referencing the DTE Object

All of the objects and members in the Visual Studio .NET automation model are based on the DTE object. The DTE object represents the Visual Studio .NET IDE and is the top-most object in the automation model hierarchy. It is located in a namespace called EnvDTE. The .NET assembly name for this namespace is "envdte" and is contained in a file called envdte.dll. The COM type library name is "Microsoft Development Environment 7.0" and is contained in a file called dte.olb.

Since all of the automation objects require the DTE object, to work with the Visual Studio .NET automation model, you must do two things: establish a reference to the EnvDTE namespace and establish a reference to the DTE object.

Adding a Reference to the EnvDTE namespace

Whether you choose the .NET assembly or the COM typelib depends upon your project. When you create an add-in using the Add-In Wizard (in any programming language) or create a macro, a reference to the EnvDTE namespace is already established for you. If you want to access the automation objects outside of macros or add-ins created with the Add-In Wizard, you must manually set up the reference.

To add a reference to EnvDTE namespace in Visual Basic .NET or Visual C# .NET

  1. In Solution Explorer, right-click either the project node or the References node and select Add Reference.
  2. In the Add Reference dialog box, click the tab of the type of component you want, such as .NET or COM.
  3. Scroll down the list of components, select either "envdte" or " Microsoft Development Environment 7.0," and then click OK.

To add a reference to EnvDTE namespace in Visual C++ .NET

  • In an appropriate header or source file, add the following:

        #pragma warning( disable : 4278 )
        //The following #import imports DTE based on its LIBID
        #import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("7.0") lcid("0") raw_interfaces_only named_guids
        #pragma warning( default : 4278 )
        using namespace EnvDTE; // optional
    

Referencing the DTE object

Once your project has a reference to EnvDTE, you can now set up a reference to the DTE object.

To reference the DTE object in Visual Basic .NET

  • In your code, enter the following:

    ' Create a new instance of Visual Studio .NET.
    Dim DTE As Object
    DTE = Microsoft.VisualBasic.Interaction.CreateObject ("VisualStudio.DTE.7.1")
    

    -or-

    ' Get an instance of the currently running Visual Studio .NET IDE.
    Dim DTE as EnvDTE.DTE
    DTE = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE")
    

To reference the DTE object in Visual C# .NET

  • In your code, enter the following:

    EnvDTE.DTE myDTE;
    myDTE = (EnvDTE.DTE)Microsoft.VisualBasic.Interaction.CreateObject("VisualStudio.DTE.7.1", "");
    

    -or-

    EnvDTE.DTE dte;
    DTE = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.7.1") // Get an instance of the currently running Visual Studio .NET IDE.
    

To reference the DTE object in Visual C++ .NET (ATL)

  • In your code, enter the following:

    CComPtr<EnvDTE::_DTE> m_pDTE;
    HRESULT hr = m_pDTE.CoCreateInstance("VisualStudio.DTE.7.1", 0, CLSCTX_ALL); // Create a new instance of Visual Studio .NET
    

    -or-

    CComPtr<EnvDTE::_DTE> m_pDTE;
    CLSID clsid;
    CLSIDFromProgID(L"VisualStudio.DTE.7.1",&clsid);
    CComPtr<IUnknown> punk;
    HRESULT hr = GetActiveObject(clsid,NULL,&punk); // get a running instance of Visual Studio .NET
    m_pDTE = punk;
    

See Also

Add-In Registration | Automation Object Model Chart | Controlling Add-Ins with the Add-In Manager | Creating Add-Ins and Wizards | Creating a Wizard | DTE Object | IDTExtensibility2 Interface