![]() |
||||
|
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:
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 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
You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||