Follow @RoyOsherove on Twitter

Writing a VS.Net Add-in, ugh

I've been dabbling a bit with the notion of creating a Regulator Add-in for VS.Net. Still not sure what exactly it will feature  - got any suggestions?
Anyway - I don't have much experience with writing Add-ins for vs.net so I happily started one project. Boy - it should not be this hard. Even trying to do something as simple as creating a tool window in Vs.Net is really hard.
All the interfaces and extensibility model of VS is still in set in the wayback machine  - meaning COM interfaces and ActiveX galore.
I wonder - when will COM be like COBOL? (And will we call it “COMOL“?)
 
Anyway - to make a short story long and unreadable, the only way to make a tool window in VS.Net that features your .Net control is to make it host an ActiveX host control. That means you need to make an ActiveX control that will host your own .Net managed user control which will reside inside it. it's pretty horrific.
 
A nice intro article to this whole ToolWindow horror can be read here.
The question remains - how do you get an ActiveX control that will host your .Net user control inside it?
Luckily for us developers - this dark corner of the universe has been exposed: there is a ATL C++ implementation of just such an ActiveX control. You can get it here , look for “ToolWindow Add-ins“ down in the page (and here's a direct download link).
once you get past this hurdle - you can actually write wonderful(ugly, long) code.
 
So, the least I could do is refactor that bit of code you need to write into some form of generic Class with a static method.
 
---------------------
Here's the code for a simple VB.Net class that takes some of the pain out of this process:
 

Public Class ControlHoster

    Public Shared Function SetupToolWindow(ByVal applicationObject As EnvDTE.DTE, _

                                            ByVal addInInstance As EnvDTE.AddIn, _

                                            ByVal caption As String, _

                                            ByVal HostedControlFullName As String)

 

        Dim GUIDString As String = Guid.NewGuid().ToString()

        Dim ProgID As String = "VSUserControlHost.VSUserControlHostCtl"

        Dim assemblyLocation As String = System.Reflection.Assembly.GetExecutingAssembly().Location

        Dim objTemp As Object = Nothing

 

        Dim retVal As EnvDTE.Window = _

                applicationObject.Windows.CreateToolWindow(addInInstance, _

                                                            ProgID, _

                                                            caption, _

                                                            GUIDString, _

                                                            objTemp)

 

 

 

        retVal.Visible = True

        objTemp.HostUserControl(assemblyLocation, HostedControlFullName)

 

        Return retVal

    End Function

 

End Class       

 
 
I really like doing this in VB.Net - makes all the casting and Reflection goo work seamlessly.
Anyway - I'm still not happy about this. I'm now facing  a more difficult problem - my Add-in does not even show up in the “Tool“ menu. I bet it's something really silly, isn't it?

Convincing Management to buy books

[ot] Piano is love