Jonny123

Previously (vs2003) I wrote code to get the "application object", how do I get this object with vs2005 SDK 4.0
I need that object to get text from the editor (currently selected text in the currently selected document), and also when executing find commands in the find window.

I'm using c# now btw.




Old VB code:

Public applicationObject As EnvDTE.DTE
Public addInInstance As EnvDTE.AddIn

Public Sub OnConnection(ByVal application As Object, _
ByVal connectMode As Extensibility.ext_ConnectMode, _
ByVal addInInst As Object, _
ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnConnection

applicationObject = CType(application, EnvDTE.DTE)
addInInstance = CType(addInInst, EnvDTE.AddIn)


Re: Visual Studio Extensibility get selected text

Karstman

How about this:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
}




Re: Visual Studio Extensibility get selected text

Jonny123

Shouldn't I use something like:

applicationObject = (DTE2) GetService(typeof(DTE2));





Re: Visual Studio Extensibility get selected text

Karstman

The application object is passed in to OnConnection, so there is no need.



Re: Visual Studio Extensibility get selected text

Jonny123

To get the OnConnection event the class must implement/inherit Extensibility.IDTExtensibility2.

Is is recommened to implement/inherit Extensibility.IDTExtensibility2 instead of calling GetService





Re: Visual Studio Extensibility get selected text

Karstman

If you have the object handed to you, why would you want to go looking for it again

I am not sure which is "recommended" but I have only seen GetService used on occasions where the object was not supplied.





Re: Visual Studio Extensibility get selected text

Ed Dore

Just to clarify here, as I think there has been some misunderstanding.

  1. Add-ins implement IDTExtensibility, and the DTE interface is passed directly via the OnConnection method as the applicationObject argument.
  2. For VS Packages, the DTE interface can be retrieved using GetService(typeof(EnvDTE.DTE)).

To get the EnvDTE80.DTE2 interface, just use a cast on the object returned by option #1 or #2 above:

  1. DTE2 dte2 = (DTE2)application; (note this is done for you in your OnConnection implementation, couresty of the Add-in project wizard)
  2. EnvDTE80.DTE2 dte2 = (EnvDTE80.DTE2)GetService(typeof(EnvDTE.DTE));

Sincerely,






Re: Visual Studio Extensibility get selected text

Karstman

oh well, I thought that was what I stated in my posts.