Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > FAQ: 1.3 How do I get the DTE object in a VSPackage?

Locked FAQ: 1.3 How do I get the DTE object in a VSPackage?

Locked

Answers

  • Wednesday, May 05, 2010 12:38 PM
     
     Answer

    The DTE appears as a global service in Visual Studio. Below, you have ways to get the DTE object in a VSPackage.

     

    Code Sample:

    1. EnvDTE.DTE dte = (EnvDTE.DTE) GetService(typeof(EnvDTE.DTE));

    2. EnvDTE.IVsExtensibility extensibility =

     GetService(typeof(EnvDTE.IVsExtensibility)) as

     EnvDTE.IVsExtensibility;

       EnvDTE.DTE dte = extensibility.GetGlobalsObject(null).DTE as EnvDTE.DTE;

    3. EnvDTE.DTE dte =

       System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0") as

     EnvDTE.DTE;

    Please note that GetService(typeof(EnvDTE.DTE)) may return null if the environment not being fully loaded and initialized.  To work around this problem, you simply need to defer the code in your Initialize method, until the VSPROPID_Zombie property (there is a better shell property to use in VS 2010: VSSPROPID_ShellInitialized) has been set to false. To do this, your package should implement IVsShellPropertyChanges interface, and execute your initialization code from your IVsShellPropertyChanges.OnShellPropertyChange implementation. For more details, please check this article: Dr. eX: Why does GetService(typeof(EnvDTE.DTE)) return null?

     

    Related Thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/55f5d612-fea6-42a9-a3e2-c2625a7cd1b3
    • Marked As Answer by MSDN FAQ Wednesday, May 05, 2010 12:38 PM
    •  

All Replies

  • Wednesday, May 05, 2010 12:38 PM
     
     Answer

    The DTE appears as a global service in Visual Studio. Below, you have ways to get the DTE object in a VSPackage.

     

    Code Sample:

    1. EnvDTE.DTE dte = (EnvDTE.DTE) GetService(typeof(EnvDTE.DTE));

    2. EnvDTE.IVsExtensibility extensibility =

     GetService(typeof(EnvDTE.IVsExtensibility)) as

     EnvDTE.IVsExtensibility;

       EnvDTE.DTE dte = extensibility.GetGlobalsObject(null).DTE as EnvDTE.DTE;

    3. EnvDTE.DTE dte =

       System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0") as

     EnvDTE.DTE;

    Please note that GetService(typeof(EnvDTE.DTE)) may return null if the environment not being fully loaded and initialized.  To work around this problem, you simply need to defer the code in your Initialize method, until the VSPROPID_Zombie property (there is a better shell property to use in VS 2010: VSSPROPID_ShellInitialized) has been set to false. To do this, your package should implement IVsShellPropertyChanges interface, and execute your initialization code from your IVsShellPropertyChanges.OnShellPropertyChange implementation. For more details, please check this article: Dr. eX: Why does GetService(typeof(EnvDTE.DTE)) return null?

     

    Related Thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/55f5d612-fea6-42a9-a3e2-c2625a7cd1b3
    • Marked As Answer by MSDN FAQ Wednesday, May 05, 2010 12:38 PM
    •  
  • Sunday, May 23, 2010 2:04 AM
     
     

    Hello.

    I tried the 3 examples.

    1 and 2 is good.

    But 3 is not.

    I encountered 2 problems.

    1. In Visual Studuio 2010, "VisualStudio.DTE.9.0" is not work. "VisualStudio.DTE.10.0" is required.
    2. GetActiveObject method returns first process instance of DTE, not caller DTE. (in Visual Studio SDK 2010 project on Visual Studio 2010, type F5 to execure experimental hive may fail)

    Thanks