Visual Studio 2010 (.vsix) extension, getting a DTE2 instance

I have a Visual Studio 2010 extension, a .vsix file. I can get a DTE instance for my specific instance of Visual Studio, which I confirm by typing dte_instance.Solution.Fullname. But for my DTE2 instance, it seems to give me information about the wrong instance of Visual Studio.

Here is the workflow: Visual Studio IDE development environment is open, has code for extension. Run the project, which starts the instance of Visual Studio in which the extension is installed. Press my menu button (in the new IDE), which runs the following code:

DTE dte; DTE2 dte2, dte2Macros; dte = (DTE)GetService(typeof(DTE)); dte2 = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0"); dte2Macros = (DTE2)dte2.MacrosIDE; //this returns what I expect, the solution name in the newer IDE. MessageBox.Show("solution name: " + dte.Solution.FullName); //code to get the startup project from MSDN //http://msdn.microsoft.com/en-us/library/ms228782.aspx SolutionBuild2 sb = (SolutionBuild2)dte2.Solution.SolutionBuild; string msg = ""; Int32 configs = sb.SolutionConfigurations.Count; foreach (String item in (Array)sb.StartupProjects) { msg += item; } //this returns a project from the development IDE, the one I don't want. System.Windows.Forms.MessageBox.Show("startup project is: " + msg); Project startupProject = dte2.Solution.Item(msg); 

I found some links to purchasing a DTE2 object in the add-on using the connect () method, but I could not find a similar callback for extensions.

Question: how to get a DTE2 instance for the IDE in which the extension is running?

+4
source share
2 answers

Try this , which uses imported service provider, or simply use Package.GetGlobalService :

DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;

+11
source

I had a problem that on some Package.GetGlobalService(typeof(DTE)) machines it returns null. Now I am using (DTE2)base.GetService(typeof(DTE)) in the Package Initialize() method (which is similar to the add connect() method of the add ().

0
source

All Articles