Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
FAQ & Support
Version History
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
FAQ & Support
Version History
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Removing commands and UI elements during Visual Studio .NET add-in uninstallation

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

This article describes how to delete commands and other UI elements created by an add-in when it is uninstalled.

More Information

When an add-in is loaded for the first time, typically it creates its commands at that point (when it receives the ext_ConnectMode.ext_cm_UISetup value in the connectMode parameter of the OnConnection method, which happens only once in the add-in lifetime). Commands should not be deleted when the add-in is unloaded, because it takes time to create them on each add-in startup and user customizations such as keyboard bindings would be lost. Instead, they should be removed when the add-in is uninstalled.

Also, add-ins can create permanent command bars (using DTE.Commands.AddCommandBar), which remain visible even if the add-in is unloaded through the Add-In Manager. But since this may be confusing for the user, most add-ins create temporary command bars (using DTE.CommandBars.Add). See HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in.

This article explains how to remove commands when the add-in is uninstalled. If the add-in creates permanent command bars, the same approach can be used to remove them on uninstallation.

The typical code (VB.NET) to create commands is the following:

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 objDTE As EnvDTE.DTE
   Dim objAddIn As EnvDTE.AddIn
   Dim objCommand As Command
  
   objDTE = CType(application, EnvDTE.DTE)
   objAddIn = CType(addInInst, EnvDTE.AddIn)
  
   If connectMode = ext_ConnectMode.ext_cm_UISetup Then
     Try
        objCommand = objDTE.Commands.AddNamedCommand(objAddIn, "MyCommand", "MyCommand", _
             "Executes the command for MyAddin", True, 59, Nothing, 1 + 2)    
     Catch e As System.Exception
     End Try
   End If
 
End Sub 

To remove the commands of the add-in, its uninstaller must perform a custom action. To learn how to create a custom action, check the documentation of your uninstaller. For the purpose of this article, we will use a VB script (.vbs file extension) for a custom action. The custom action must perform the following steps:

  • Create an instance of Visual Studio .NET through its ProgID ("VisualStudio.DTE.7.1" for Visual Studio .NET 2003 and "VisualStudio.DTE.7" for Visual Studio .NET 2002). This will return a DTE object which gives access to the extensibility model. That instance will remain invisible by default.
  • Retrieve the command(s) of the add-in using DTE.Commands.Item(commandFullName) and call the Delete() method on each one.
  • Close the instance of Visual Studio .NET, calling DTE.Quit() or DTE.ExecuteCommand("File.Exit").

There is an unfortunate bug in Visual Studio .NET 2002/2003, though. Commands (and permanent command bars) are persisted on disk in the file:

"C:\Documents and Settings\<user>\Application Data\Microsoft\VisualStudio\<version>\1033\CmdUI.PRF"

where:

<version> = 7.0 for Visual Studio .NET 2002
<version> = 7.1 for Visual Studio .NET 2003
<version> = 8.0 for Visual Studio 2005
<version> = 9.0 for Visual Studio 2008

With the approach described so far, if commands are added only to command bars created by the add-in, it happens that the file is not marked as dirty and therefore although the uninstallation does not show any error, commands are not actually removed since that file is not updated. The workaround is to add commands to a built-in command bar of Visual Studio .NET such as the "Tools" menu. Fortunately this can be done at uninstallation time, so you are not forced to add commands to that menu while the user was using the add-in.

The following VB Script sample shows the final code of the custom action with the workaround:

Dim objDTE
Dim objCommand
 
Set objDTE = CreateObject("VisualStudio.DTE.7.1")  ' Visual Studio .NET
 2003

Set objCommand = objDTE.Commands.Item("MyAddIn.Connect.MyCommand")

' PATCH: add the command to the Tools menu
objCommand.AddControl(objDTE.CommandBars.Item("Tools"))
 
objCommand.Delete

objDTE.Quit;

Visual Studio 2005 and higher introduces a new handy /ResetAddin command-line flag for the devenv.exe application that can be used to remove the commands of an add-in and its associated permanent buttons (but not for the permanent commandbars). The usage is the following:

devenv.exe /ResetAddIn Namespace.ClassName /Command File.Exit

where Namespace.ClassName is the full name of the Connect class. Since the IDE will remain open, /Command File.Exit is used to close it. Since devenv.exe is not available from the normal MS-DOS command prompt, you will need to specify its full path, which can be retrieved from the Windows registry, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0 registry entry (use 9.0 for Visual Studio 2008), "InstallDir" value. Finally, this command has no effect if the add-in is loaded when the IDE is loaded, so you need to unregister the add-in first, and then execute the command to remove the add-in commands. While the /SafeMode command-line flag could be used in Visual Studio 2008 to prevent the loading of the add-in, it doesn't work in Visual Studio 2005, so you must use the proper order.

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this

MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top