3

How can I get DTE instance in a VS package project? It's straigtforward in addin project since application is being passed as an argument to onConnection method, but it is unclear how to get it in a package.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

5

From your main Package class:

EnvDTE80.DTE2 dte = this.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)) as EnvDTE80.DTE2;
Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • 4
    If you are not in the main package, you can use this: `EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE)) as EnvDTE.DTE;` (or use EnvDTE80.DTE2 if you need that instead. – Shakaron Sep 24 '15 at 09:35
  • 1
    Further option found on the MSDN site: https://msdn.microsoft.com/en-us/library/68shb4dw.aspx – Shakaron Sep 24 '15 at 09:36
1

You can get a null instance getting the SDTE service if the shell is not fully initialized. See how to handle that in this code sample:

HOWTO: Get the EnvDTE.DTE instance from a package

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18