Logo
BUG: Add-in reloaded after unloaded in Visual Studio .NET.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: June 2004   Microsoft Visual Studio .NET 2003
       
Introduction

If two unrelated add-ins add buttons with Command.AddControl to the same Visual Studio .NET built-in command bar (such the "Tools" commandbar), when you unload one of them through the Add-In Manager and it removes the button that it added, it is loaded again.

Cause

This is a bug of Visual Studio .NET.

More Information

Steps to reproduce the problem:

  • Create two add-ins with the following code, changing the Guid, ProgId and command name for each one:
<GuidAttribute("CEFCC4FC-B2D6-490A-9893-0509F682FB5B"), _
   ProgIdAttribute("TestAddin.Connect")> _
Public Class Connect
   Implements Extensibility.IDTExtensibility2
   Implements EnvDTE.IDTCommandTarget
 
   Private Const m_COMMAND_NAME As String = "Test"
 
   Private m_objDTE As EnvDTE.DTE
   Private m_objAddin As AddIn
   Private m_objCommandBarControl As CommandBarControl
 
   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
 
      Dim objCommandBar As CommandBar
      Dim objCommand As Command
 
      m_objDTE = CType(application, EnvDTE.DTE)
      m_objAddin = CType(addInInst, AddIn)
 
      Select Case connectMode
 
         Case ext_ConnectMode.ext_cm_UISetup

            objCommand = m_objDTE.Commands.AddNamedCommand( _
               m_objAddin, m_COMMAND_NAME, "Test", "Test", True, 59)
 
         Case ext_ConnectMode.ext_cm_AfterStartup, ext_ConnectMode.ext_cm_Startup
 
            objCommand = m_objDTE.Commands.Item(m_objAddin.ProgID & "." & m_COMMAND_NAME)
            objCommandBar = m_objDTE.CommandBars.Item("Tools")
            m_objCommandBarControl = objCommand.AddControl(objCommandBar)
            m_objCommandBarControl.Visible = True
 
      End Select
 
   End Sub
 
   Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
      ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnDisconnection
 
      Select Case RemoveMode
 
         Case ext_DisconnectMode.ext_dm_HostShutdown, ext_DisconnectMode.ext_dm_UserClosed
 
            If Not (m_objCommandBarControl Is Nothing) Then
               m_objCommandBarControl.Delete()
            End If
 
      End Select
 
   End Sub
 
   Public Sub Exec(ByVal cmdName As String, ByVal executeOption As vsCommandExecOption, _
      ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) _
      Implements IDTCommandTarget.Exec

      handled = True
 
   End Sub
 
   Public Sub QueryStatus(ByVal cmdName As String, _
      ByVal neededText As vsCommandStatusTextWanted, _
      ByRef statusOption As vsCommandStatus, ByRef commandText As Object) _
      Implements IDTCommandTarget.QueryStatus
 
      If neededText = EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
 
         If cmdName = m_objAddin.ProgID & "." & m_COMMAND_NAME Then
 
            statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _
               vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
 
         Else
 
            statusOption = vsCommandStatus.vsCommandStatusUnsupported
 
         End If
 
      End If
 
   End Sub
 
   Public Sub OnBeginShutdown(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnBeginShutdown
   End Sub
 
   Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
   End Sub
 
   Public Sub OnStartupComplete(ByRef custom As System.Array) _
      Implements Extensibility.IDTExtensibility2.OnStartupComplete
   End Sub
 
End Class
  • Load both add-ins in a new instance of Visual Studio .NET . The "Tools" commandbar should show the button of each add-in.
  • Go to the Add-in Manager and unload one of them. When you close the Add-in Manager, the button of the unloaded addin is still in the "Tools" commandbar and if you go again to the Add-in Manager, you see that it has been reloaded.

This problem does not happen if the add-ins add their buttons to the different command bars.



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)