MZ-Tools Articles Series: PRB: Unable to add buttons to toolbars of toolwindows of Visual Studio from an add-in

Today there was a question in the MSDN Forum for Visual Studio Extensibility (VSX) about adding a button to the toolbar (not to the context menu) of the Solution Explorer from a Visual Studio add-in. This is something that I am interested in too from time to time because I would like to move the “Collapse Projects” button of my MZ-Tools 6.0 add-in from the toolbar of the add-in to the more natural place, the toolbar of the Solution Explorer. I tried almost five years ago, when Visual Studio 2005 introduced EnvDTE80 and the new EnvDTE80.Window2 with its CommandBars property that gives you access to the toolbar of a toolwindow. Since add-ins are second-class citizens, this scenario was not tested by Microsoft and I found a bug that I reported:

COMException removing button from Solution Explorer commandbar
https://connect.microsoft.com/VisualStudio/feedback/details/113953/comexception-removing-button-from-solution-explorer-commandbar

The bug occurred actually removing the button, not adding it, but as Microsoft explained:

“It turns out the bug here is actually in the code to add the control, not delete it. The Solution Explorer toolbar has an internal flag indicating that modifications should not be allowed; CommandBarControl.Delete is properly respecting that flag (hence the error) but Command.AddControl is not.”

and when they finally fixed this problem (in VS 2008), it was not allowing add-ins to add buttons to the Solution Explorer toolbar, but rather throwing a System.UnauthorizedAccessException when you try to do it. Bottom line: add-ins are not allowed to add buttons to the Solution Explorer toolbar. VS 2010 is crystal-clear in its error message that this applies to other toolwindows too. I am not sure if this applies too to packages or only to add-ins.

The details and code to reproduce the problem are in my latest article:

PRB: Unable to add buttons to toolbars of toolwindows of Visual Studio from an add-in
http://www.mztools.com/articles/2010/MZ2010004.aspx