Category Archives: VS 2010

Visual Studio “painful” releases and “painless” releases for add-ins. VS 2010 belongs to the first group (by far). And maybe next ones too…

There are “painful” Visual Studio releases which require you a lot of testing and changes in your add-in, a lot of time isolating bugs to reproduce them with the minimal code, and a lot of wait until they are fixed in some beta, CTP, etc:

– VS.NET 2002: introduced a new automation model (EnvDTE) very different to the one used in VB6, with commands (a new concept at that time), required (native!) satellite DLLs, shim COM controls for toolwindows, etc.

– VS 2005: introduced XML-based add-in registration, a new
Microsoft.VisualStudio.CommandBars assembly, managed (.NET) satellite DLLs, truecolor bitmaps, a new EnvDTE80 assembly, temporary solutions, build platforms, partial classes, etc.

Then there are “painless” Visual Studio releases which don’t introduce many changes and for the most part use the same code than the one before, so add-ins don’t suffer much:

– VS.NET 2003: it was very similar to VS.NET 2002.

– VS 2008: it is very similar to VS 2005.

VS 2010 belongs to the first group. In the long journey (several releases) that Microsoft is taking to convert the COM-based original VS.NET 2002 IDE to a 100% managed (.NET) IDE, it is the one suffering the most important changes so far. Suffice to say that this is the first VS release that needs to load the .NET Framework when launched (so it loads slower), previous ones went native code all the launch code path.

In this VS 2010 release the commandbars and the code editors are being changed, which causes add-ins to suffer lots of bugs, specially because Microsoft don’t use add-ins (but SDK packages), so bugs get unnoticed until developers of add-ins start testing extensively. I haven’t counted them exactly, but I’m quite sure I have reported more than 30 bugs in the automation model at Microsoft Connect since I started to test VS 2010 Beta 1. I don’t remember me reporting so many bugs in the beta period of any previous release. BTW, the bugs that I report that are still in beta 2 are prefixed with “VSIP: VS 2010 Beta 2” if you want to search them.

Yesterday I finally got tired of not being able to load my MZ-Tools add-in in VS 2010 Beta 2 due to its multiple frustrating critical bugs in the new WPF-based commandbars. So, for testing purposes, I decided to skip the creation of toolbars, menus and commandbar popups and instead I created by hand a custom toolbar with the commands of my add-in (through the Tools, Customize dialog), so I could at last test the actual features. Of course, the process of creating and populating a custom toolbar couldn’t go without finding a new bug:

VSIP: VS 2010 Beta 2 Bug: Removing last button from custom toolbar causes it to disappear temporarily from the list of commandbars
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=515143

(and another one that I was unable to reproduce)

So today I have started to test some features and soon I have found a bug that didn’t happen in previous releases and that can break add-ins with toolwindows whose content changes with the active document window:

VS 2010 Beta 2 Bug: EnvDTE.WindowEvents.WindowActivated event not fired for window getting the focus after closing other window
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=515234

Notice that the Properties window of VS 2010 is not affected by that bug and changes the content correctly as you close document windows, because it doesn’t use the automation model (EnvDTE.WindowEvents.WindowActivated event).

Maybe I am too pessimistic, but I think this will be the outlook for add-ins in this and next releases of Visual Studio: a lot of internal changes in Visual Studio that cause bugs in the automation model for add-ins that get unnoticed until very late in the beta cycle (because early betas and CTPs are under heavy construction and are unusable for add-ins).

And if you think that the next VS release (maybe VS 2012 or VS 2013) will be a “painless” one, Microsoft is planning to introduce a new language model (different than the code model provided by Project.CodeModel or ProjectItem.FileCodeModel). Even if the current code model is preserved for backward compatibility, chances are that add-ins will want to use a better code model, given the limitations of the current one (no support for generics, no support for code inside methods, limited code generation, bugs, etc.).

And if you think that the new WPF-based commandbars will be here to stay many years, I bet that sooner of later Microsoft will change them by the ribbon UI of Office 2007 and new Windows 7 applications 😉

On the positive side, I do know that Microsoft people are doing a lot of hard work to fix the bugs and are providing the means to get fixed as many bugs as possible by the RTM release at March 2010.

Importing Visual Studio settings destroys commandbars of add-ins

There is a bug/issue that I reported years ago for VS 2005 and was not even acknowledged as a bug but “by design”. VS 2010 (Beta 2) still exhibits it so I keep pushing to get if fixed (the original bug report was lost, I think it died when MSDN Product Feedback became Microsoft Connect):

VS 2010 Beta2: importing settings destroys commandbars created by add-ins

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=512214

The problem is that Visual Studio thinks that all the commandbars of the IDE belong to it, ignoring the fact that some commandbars belong to add-ins and it should ignore them when importing settings, rather than resetting them or destroying them…

To complicate things, when the add-in is unloaded at some point later (by the user or closing VS), it doesn’t have a direct way to know if its commandbars have been destroyed by Visual Studio, so it tries to delete them and it gets a ObjectDisposedException….

Add-ins and the “Tools”, “Customize” dialog, “Commands” tab of Visual Studio 2010 (Beta 2)

In VS 2008 (and previous versions), when you opened the “Tools”, “Customize…” dialog, “Commands” tab, you got a list of  “categories”, and each category has a list of commands that you could drag and drop on some toolbar to create a button from that command. For commands provided by add-ins, you had the “Add-Ins” category and for commands provided by macros you had the “Macros” category. Furthermore, you had a “Rearrange Commands…” button where a new dialog allowed you to select a menu bar or a toolbar, and add/move/remove commands (buttons), etc. So far so good, except one thing that VS has never managed well which is that toolbars, menu bars, commandbar popups and buttons provided by add-ins can be temporary if the add-in uses that approach instead of the permanent one, and therefore Visual Studio should not offer the temporary UI items of add-ins for customization, since customization will be lost when the add-in is unloaded and when loaded again the add-in will recreate the UI from scratch. I discussed these issues in detail in the article:

HOWTO: Handling buttons and toolbars user customizations from a Visual Studio add-in
http://www.mztools.com/articles/2008/MZ2008019.aspx

In VS 2010 (Beta 2), commandbars are WPF-based and the Customize dialog has suffered a re-design, but if it is not fixed in the RC / RTM builds things are getting worse due to these bugs (currently under evaluation, but likely closed as “won’t fix” in this release):

VS 2010 Beta 2 Bug: wrong customization for commandbarpopup of add-ins
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=501255

VS 2010 Beta2: Add-ins and macros commands should not appear in the
“Tools”, “Customize” dialog, “Commands” tab, “Menu bar” combobox
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=512141

Those bugs will cause confusion to users of add-ins if they try to use the Customize dialog…

At least these two minor bugs were fixed after Beta 2:

VS 2010 Beta 2 Bug: Command picture not shown in Tools, Customize window, Commands tab
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=502250

VS 2010 Beta 2 Bug: Command name shown instead of caption in Tools, Customize window, Commands tab
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=502242

Another critical VS 2010 Beta 2 bug that causes the IDE to hang

This bug eluded me during several days because there was another bug that already caused VS 2010 Beta 2 to hang, but finally a couple of days ago I realized that was seeing a different issue (no commandbarbutton involved in this new bug) and I was able to isolate and report it today:

VS 2010 Beta 2 Bug: adding CommandBarPopup to CommandbarPopup causes VS to hang if CommandBarPopup was added to Toolbar before

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=507650

As I expressed some days ago, my feeling with VS 2010 so far (after four builds) is that once you patch your add-in to (somehow) bypass a bug, the add-in finds a new one.

The (VB.NET) code to reproduce it is:

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect
   Implements IDTExtensibility2

   Private _applicationObject As DTE2
   Private _addInInstance As AddIn
   Private _commandBarToolbar As CommandBar
   Private _commandBarPopupOnToolbar As CommandBarPopup
   Private _commandBarPopupMenu As CommandBarPopup
   Private _commandBarPopupOnMenu As CommandBarPopup

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

      _applicationObject = CType(application, DTE2)
      _addInInstance = CType(addInInst, AddIn)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_AfterStartup
            InitializeAddIn()

         Case ext_ConnectMode.ext_cm_Startup
            ' OnStartupComplete will be called

      End Select

   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
      InitializeAddIn()
   End Sub

   Private Sub InitializeAddIn()

      Dim commandBars As CommandBars

      Try

         commandBars = CType(_applicationObject.CommandBars(), CommandBars)

         If MessageBox.Show("Create toolbar before menu?", "", MessageBoxButtons.YesNo) = DialogResult.Yes Then

            ' This hangs VS 2010 Beta 2
            CreateToolbar(commandBars)
            CreateMenu(commandBars)

         Else

            ' This causes nested menus but doesn’t hang VS 2010 Beta 2
            CreateMenu(commandBars)
            CreateToolbar(commandBars)

         End If

      Catch ex As Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try

   End Sub

   Private Sub CreateToolbar(ByVal commandBars As CommandBars)

      _commandBarToolbar = commandBars.Add(Name:="My toolbar", Position:=MsoBarPosition.msoBarTop, Temporary:=True)
      _commandBarPopupOnToolbar = AddCommandBarPopupToCommandBar(_commandBarToolbar, "MyPopupOnToolbar", "My Popup On Toolbar")
      _commandBarToolbar.Visible = True

   End Sub

   Private Sub CreateMenu(ByVal commandBars As CommandBars)

      Dim menuCommandBar As CommandBar

      menuCommandBar = commandBars.Item("MenuBar")

      _commandBarPopupMenu = AddCommandBarPopupToCommandBar(menuCommandBar, "MyPopupMenu", "My Menu")
      _commandBarPopupOnMenu = AddCommandBarPopupToCommandBar(_commandBarPopupMenu.CommandBar, "MyPopupOnMenu", "My Popup On Menu")
      _commandBarPopupMenu.Visible = True

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) Implements IDTExtensibility2.OnDisconnection

      Try

         If Not _commandBarPopupOnMenu Is Nothing Then
            _commandBarPopupOnMenu.Delete()
         End If

         If Not _commandBarPopupOnToolbar Is Nothing Then
            _commandBarPopupOnToolbar.Delete()
         End If

         If Not _commandBarPopupMenu Is Nothing Then
            _commandBarPopupMenu.Delete()
         End If

         If Not _commandBarToolbar Is Nothing Then
            _commandBarToolbar.Delete()
         End If

      Catch ex As Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

   Friend Shared Function AddCommandBarPopupToCommandBar(ByVal objParentCommandBar As CommandBar, _
   ByVal sCommandBarName As String, ByVal sCaption As String) As CommandBarPopup

      Dim objCommandBarPopup As CommandBarPopup = Nothing
      Dim objCommandBar As CommandBar
      Dim objCommandBarControl As CommandBarControl
      Dim iPosition As Integer

      If Not (objParentCommandBar Is Nothing) Then

         iPosition = objParentCommandBar.Controls.Count + 1

         objCommandBarControl = objParentCommandBar.Controls.Add(Type:=MsoControlType.msoControlPopup, Before:=iPosition)

         objCommandBarPopup = CType(objCommandBarControl, CommandBarPopup)

         objCommandBar = objCommandBarPopup.CommandBar

         If Not (objCommandBar Is Nothing) Then

            objCommandBar.Name = sCommandBarName

            objCommandBarPopup.Caption = sCaption

         End If

      End If

      Return objCommandBarPopup

   End Function

End Class

More on CommandBarButton.Mask property deprecated in VS 2010

In the post CommandBarButton.Mask property deprecated in VS 2010 I mentioned that VS 2010 will deprecate the Mask property of CommandBarButton to specify bitmap transparency, accepting 32-bit bitmaps with transparency in the alpha channel in the CommandBarButton.Picture property instead.

The VS team told me some weeks ago that the CommandBarButton.Picture would accept also in next VS 2010 builds 24-bit bitmaps with RGB=0,254,0 (almost pure green) as transparent color (as VS has always accepted for commands). I have verified this week that VS 2010 Beta 2 accepts that possibility correctly.

Bottom line: VS 2010 will accept both in Commands (AddNamedCommand) and CommandBarButton.Picture:

  • 32-bit bitmaps with transparency in the alpha channel
  • 24-bit bitmaps with RGB=0,254,0 (almost pure green) as transparent color

Workaround for VS 2010 Beta 2 Bug: CommandBarButton.Style not honored, always Icon + Caption

Back in VS.NET 2002/2003 the only way to change the style (Icon only, or Icon + Caption) of a CommandBarButton created from a Command was to cast the CommandBarControl returned by Command.AddControl to CommandBarButton and use the CommandBarButton.Style property.

If you use that approach in VS 2010 Beta 2 you will find this bug that I already mentioned:

VS 2010 Beta 2 Bug: CommandBarButton.Style not honored, always Icon + Caption
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=500403

That means that if the toolbar of your add-in has 30 buttons which should be shown as “only icon”, due to this bug they will shown as “icon + caption” and you hardly will see 7 of them…

Fortunately there is a workaround thanks to the EnvDTE80.Commands2.AddNamedCommand2 method that was introduced in VS 2005. So you can:

  • Cast the EnvDTE.DTE.Commands (or EnvDTE80.DTE2.Commands) property to EnvDTE80.Commands2
  • Call the Commands2.AddNamedCommand2 method passing in the CommandStyleFlags parameter the EnvDTE80.vsCommandStyle.vsCommandStylePict value

I hope this helps.

Yet another critical VS 2010 Beta 2 bug in CommandBars: this one causes VS to hang, nothing less

This one happens when an add-in:

– Creates a temporary toolbar using CommandBars.Add

– Creates a temporary CommandBarPopup on that toolbar

– Creates a command and tries to add a CommandBarButton from that command to that CommandBarPopup using Command.Add(CommandBarPopup.CommandBar)

The bug report is here:

VS 2010 Beta 2 Bug: adding CommandBarButton from Command to CommandbarPopup on Toolbar causes VS to hang

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=503871

The (VB.NET) code to reproduce the problem is:

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect
   Implements IDTExtensibility2

   Private _applicationObject As DTE2
   Private _addInInstance As AddIn
   Private _myCommandBarToolbar As CommandBar
   Private _myCommandBarPopup As CommandBarPopup

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

      _applicationObject = CType(application, DTE2)
      _addInInstance = CType(addInInst, AddIn)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_AfterStartup
            InitializeAddIn()

         Case ext_ConnectMode.ext_cm_Startup
            ' OnStartupComplete will be called

      End Select

   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
      InitializeAddIn()
   End Sub

   Private Sub InitializeAddIn()

      Const MY_COMMAND_NAME As String = "MyCommand"

      Dim command As Command = Nothing
      Dim commandBars As CommandBars
      Dim commandBarControlObject As Object

      Try

         commandBars = CType(_applicationObject.CommandBars(), CommandBars)

         _myCommandBarToolbar = commandBars.Add(Name:="My toolbar", Position:=MsoBarPosition.msoBarTop)

         ' Create a commandbar popup
         _myCommandBarPopup = CType(_myCommandBarToolbar.Controls.Add(Type:=MsoControlType.msoControlPopup), CommandBarPopup)
         _myCommandBarPopup.Caption = "My CommandBarPopup"

         ' Try to retrieve existing command
         Try
            command = _applicationObject.Commands.Item(_addInInstance.ProgID & "." & MY_COMMAND_NAME)
         Catch ex As Exception
         End Try

         ' Create command
         If command Is Nothing Then
            command = _applicationObject.Commands.AddNamedCommand(_addInInstance, MY_COMMAND_NAME, "My command", "My tooltip", True, 59)
         End If

         ' This line hangs VS 2010 Beta 2
         commandBarControlObject = command.AddControl(_myCommandBarPopup.CommandBar)

      Catch ex As Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) Implements IDTExtensibility2.OnDisconnection

      Try

         If Not _myCommandBarPopup Is Nothing Then
            _myCommandBarPopup.Delete()
         End If

         If Not _myCommandBarToolbar Is Nothing Then
            _myCommandBarToolbar.Delete()
         End If

      Catch ex As Exception
         System.Windows.Forms.MessageBox.Show(ex.ToString)
      End Try

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

End Class

Another critical VS 2010 Beta 2 bug in CommandBars: this one causes VS to evaporate without warning, nothing less

After 3 hours isolating this to the minimal expression (which is tedious if your add-in is has lots of user interface items and debugging a VS 2010 add-in on a virtual machine is painfully slow), here it is:

VS 2010 Beta 2 Bug: deleting a CommandbarPopup unloading add-in causes VS to disappear
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=503093

(the problem happens if the add-in creates two (not just one) CommandBarPopups on a parent CommandBarPopup and tries to delete them when unloaded)

Frustrating new bugs in commandbars of VS 2010 Beta 2

I can’t express how disappointed, frustrated (and more things!) I am with VS 2010 Beta 2 regarding correct behavior of commandbars for add-ins. It seems that once a bug is fixed in a VS 2010 build and an add-in can pass that point, new bugs surface that prevent its correct loading (and I have tested several builds since Beta 1).

Suffice to make you aware of these bugs:

VS 2010 Beta 2 Bug: CommandBarButton of add-in duplicated in new commandbar when already created in another commandbar
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=502289

(For my MZ-Tools add-in with tons of buttons, menus and popups, only that bug of VS 2010 Beta 2 has completely ruined it, not to mention the time it took me to isolate it until I realized what was happening because initially the add-in seemed to hang, due to the number of duplications that were occurring at the glacial speed of VS 2010 debugging an add-in on another VS 2010 instance on a virtual machine)

VS 2010 Beta 2 Bug: CommandBarButton.Style not honored, always Icon + Caption
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=500403

VS 2010 Beta 2 Bug: System.AccessViolationException attempting to create a temporary popup commandbar
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=500408

VS 2010 Beta2: InvalidCastException casting
DirectCast(CommandBarControl, CommandBarPopup).CommandBar.Parent back to CommandBarControl
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=499483

I realize that changing the COM-based commandbars of VS 2008 by new WPF-based commandbars in VS 2010 is a daunting and difficult task, but if the next milestone after Beta 2 is Release Candidate (where only critical bugs are fixed) then Beta 2 should be already free of such nasty bugs. Or there should be a known roadmap of betas / CTPs until RTM so that developers of add-ins can know how many builds they will have to test and rest assured that they will have enough weeks/months to test their add-ins (considering that in Beta 2 they can’t get them even loaded in Visual Studio).

I think that since Microsoft builds and extends Visual Studio with packages (SDK) and not add-ins, commandbars/commands/buttons of packages get better behavior earlier in the development cycle of Visual Studio. But for add-ins (automation) every time that commandbars are changed in some VS release, you have to wait until the RTM to check is some bug that could ruin your add-in is fixed. I experienced the same with VS 2005, which introduced 24-bit bitmaps (rather than the 16-color bitmaps of VS.NET 2003) and a different assembly for Microsoft.VisualStudio.Commandbars.

VS 2010 RC/RTM fixing ” ‘Object must be the same type as the enum’ exception calling CommandBars.Add from an add-in” problem

Microsoft has reported that this problem will be fixed:

 ” ‘Object must be the same type as the enum’ exception calling CommandBars.Add from an add-in”
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=462766

Notice that the “Embed Interop Types” property seems to have been renamed to “Isolated” in VS 2010 Beta 2, in one of those renaming changes that make things more obscure…