locked
How to use an EnvDTE.DTE instance from a Visual Studio package RRS feed

  • Question

  • I am able to set up and get the instance of the EnvDTE environment by using the code supplied here:

    http://www.visualstudioextensibility.com/articles/packages/

    Problem becomes how do I use it.   I want to be able to for example to get the project name that I am currently under by scrolling through solution explorer.  I also want to be able to get a list of selected files under the project. Basically I want to duplicate this behavior from a 2008 VS Addin.

    SelectedItems sis = _applicationObject.SelectedItems;

    Project projSelected = (Project)((Object[])_applicationObject.ActiveSolutionProjects)[0];

    string sCatalogName = projSelected.Name;

    Solution sln = _applicationObject.Solution;

    I need to be able to duplicate this is my package, is this possible?

    Thursday, January 29, 2015 10:36 PM

Answers

  • Once you have the DTE instance, what prevents you to make the code of your add-in to work in the package?

    * My new blog about VSX: http://www.visualstudioextensibility.com * Twitter: https://twitter.com/VSExtensibility * MZ-Tools productivity extension for Visual Studio: http://www.mztools.com.


    • Edited by Carlos J. Quintero Friday, January 30, 2015 12:26 PM fix
    • Marked as answer by Caillen Sunday, February 8, 2015 8:42 AM
    Friday, January 30, 2015 12:26 PM
  • As Carlos mentioned, you can still use the EnvDTE interfaces. You just need to retrieve it using something like Package.GetGlobalService, after your package has been initialized. Don't try getting it until after your Package.Initialize has been invoked. Otherwise the call might return a null value.

    Your example listed applicationObject, so I think the assumption was you already had it.

    For example:

       EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));

    or

       EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(SDTE));

    for the original version of the interface.

    There are other more package based ways of doing things like this, but given you are moving an addin into a package, it's usually just easier to keep what you have and just wire the code back up in the context of a package instead of an addin. For example, you can use IVsMonitorSelection interface to track selection state within the IDE.

    Sincerely,

    Sincerely,


    Ed Dore

    • Marked as answer by Caillen Sunday, February 8, 2015 8:42 AM
    Tuesday, February 3, 2015 4:16 AM

All replies

  • Once you have the DTE instance, what prevents you to make the code of your add-in to work in the package?

    * My new blog about VSX: http://www.visualstudioextensibility.com * Twitter: https://twitter.com/VSExtensibility * MZ-Tools productivity extension for Visual Studio: http://www.mztools.com.


    • Edited by Carlos J. Quintero Friday, January 30, 2015 12:26 PM fix
    • Marked as answer by Caillen Sunday, February 8, 2015 8:42 AM
    Friday, January 30, 2015 12:26 PM
  • There is nowhere that I searched that shows how this is done. Getting the DTE is the easy part. There are no blogs or other help that I can find.
    Monday, February 2, 2015 8:51 PM
  • As Carlos mentioned, you can still use the EnvDTE interfaces. You just need to retrieve it using something like Package.GetGlobalService, after your package has been initialized. Don't try getting it until after your Package.Initialize has been invoked. Otherwise the call might return a null value.

    Your example listed applicationObject, so I think the assumption was you already had it.

    For example:

       EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(SDTE));

    or

       EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(SDTE));

    for the original version of the interface.

    There are other more package based ways of doing things like this, but given you are moving an addin into a package, it's usually just easier to keep what you have and just wire the code back up in the context of a package instead of an addin. For example, you can use IVsMonitorSelection interface to track selection state within the IDE.

    Sincerely,

    Sincerely,


    Ed Dore

    • Marked as answer by Caillen Sunday, February 8, 2015 8:42 AM
    Tuesday, February 3, 2015 4:16 AM