Making a VB 6.0 Add-in Work in .NET

 < Free Open Study > 

If you are new to add-ins, you can ignore this section. If you are used to writing add-ins in VB 6.0, you may be asking, "Are all of my add-ins broken?" The answer to this question is, in a practical sense, "Yes!" What I mean here is that .NET provides a completely new and totally different automation model. It is referenced by implementing IDTExtensibility2, whereas VB 6.0 implemented IDTExtensibility. These two models are as different as night and day. It turns out that both models exist in .NET. However, upon trying to connect one of my VB 6.0 compiled add-ins via COM interoperability, I have found that it will not load, even though I have succeeded in getting it listed in the Add-in Manager dialog box. I receive an error message that says, "No such interface exists". This means that .NET does not recognize the VB 6.0 compiled IDTExtensibility interface. I explore the use of the VB 6.0 model in .NET in Chapter 13, where I discuss the migration of VB 6.0 add-ins to .NET.

There is one small exception to the declaration that VB 6.0 add-ins are broken. Add-in toolbars, tool buttons, and menus are not really so much a part of Visual Studio automation as they are Microsoft Office. For example, if you had an add-in that was launched from VB only because you added a toolbar, tool button, or menu to the IDE's toolbar, that add-in will still probably work. A good example of this is VisData. VisData was really a stand-alone executable, which allowed you to launch it from the IDE. It had functionality that allowed you to add it to VB IDE as an add-in. All this meant was that it could add a picture button menu to the Add-in menu in the VB 6.0 IDE. Once launched, it had no interaction with the IDE, and it required no interaction with the extensibility model.


The fact that you could add VisData to the Add-in menu in VB 6.0 does not now imply that VisData can be run as an add-in in .NET. The Connect class would have to be changed to implement IDExtensibility2 to use the new interfaces to .NET.

Therefore, if you have VB 6.0 add-ins that require no interaction with the IDE other than to add a UI to the IDE, you can still compile them under VB 6.0 with aminor modification to the Connect class, and then connect them to Visual Studio .NET, placing your UI on its toolbar.

If you look on the third CD of Beta 2 in the \Samples\Automation\cmdBrowse directory, you'll find a sample add-in (cmdBrowser.vbp), which you can compile under VB 6.0 and connect to Visual Studio .NET. I've done it, and it works fine. Why does this add-in work and mine doesn't? The answer is that this add-in doesn't implement IDTExtinsibility. Rather, it's a special type of add-in that exposes special OnConnection and OnDisconnection methods that are still recognized by .NET.


This book is based on the release version of Visual Studio .NET. I made a reference to the Beta 2 CD in the preceding paragraph simply because the automation samples were available on that CD. You can also download these samples from the Microsoft Web site.

The sample add-in is quite handy, in that it will search through the registry, find all of the commands in the Visual Studio .NET IDE (menus, tool bars, and tool buttons), and place them in a tree view. Furthermore, because the UI of the add-in launches an ActiveX document rather than a VB form, it is a dockable tool window. You can actually learn several things from examining this sample add-in in detail.

In order to make your old add-in interface work, you must add a reference to DTE.OLB to your VB 6.0 project references. Then, you need to replace the OnConnection and OnDisconnection methods in the Connect class. The code in Listing 1-3 is incomplete and is not meant to work by itself. It is included here as an illustration of how to connect a simple VB 6.0 add-in to .NET, and it is therefore not included with the code of this chapter. You can find an entire project using code similar to this on the third CD of the Beta 2 release.

Listing 1-3: VB 6.0 Add-in Code

start example
 Option Explicit Public FormDisplayed As Boolean Public oVB As EnvDTE.DTE Dim mcbMenuCmdBar As Office.CommandBarControl Dim gwinWindow As EnvDTE.Window Dim DismDisplay As Object Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler Attribute MenuHandler.VB_VarHelpID = -1 '-------------------------------------------------------- 'this method adds the add-in to VS '------------------------------------------------------ Private Sub AddinInstance_OnConnection(ByVal Application As Object, _                      ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _                      ByVal AddInInst As Object, custom() As Variant)     On Error GoTo ErrH      'save the vb instance     Set oVB = Application     ' place the menu on the Tools Menu     Set mcbMenuCommandBar = AddToToolsMenu("My VB6 Add-in")     ' set up event handler for click event     Set Me.MenuHandler = _             oVB.Events.CommandBarEvents(mcbMenuCmdBar)     Exit Sub ErrH:     MsgBox Err.Description End Sub 'this method removes the add-in from VS Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _         AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)     On Error Resume Next      'delete the command bar entry     mcbMenuCmdBar.Delete End Sub Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant) ' this does nothing but must be available to the IDE End Sub 'this event fires when the menu is clicked in the IDE Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, _             handled As Boolean, CancelDefault As Boolean)     ' Here you might load a form or Shell and executable     ' you just can't reference anything in the Extensibility Object End Sub Function AddToToolsMenu(rsMenuCaption As String) As Office.CommandBarControl     Dim cbMenuCmdBar As Office.CommandBarControl 'command bar object     Dim cbMenu As Object     On Error GoTo AddToToolsMenuErr     'see if we can find the add-ins menu     Set cbMenu = VSInstance.CommandBars("Tools")     'add it to the command bar     Set cbMenuCmdBar = cbMenu.Controls.Add(1)     'set the caption     cbMenuCommandBar.Caption = rsMenuCaption     Set AddToAddInCommandBar = cbMenuCommandBar     Exit Function AddToToolsMenuErr:     Err.Clear End Function 
end example

Obviously, this add-in Connect class does nothing but add a menu to the Tools menu in the IDE, but it does illustrate how to connect a VB 6.0 compiled add-in to Visual Studio .NET. The interface to the command bar interface is the same in .NET as it is in VB 6.0 because the toolbars in the respective IDEs are both derived from the Microsoft Office 8 object model. As you can see, the only difference between the illustrated Connect class and a VB 6.0 Connect class is the name that precedes the OnConnection and OnDisconnection methods, as shown here:

  • VB 6.0 interface: IDTExtensibility_OnConnection

  • .NET interface: AddinInstance_OnConnection

  • VB 6.0 interface: IDTExtensibility_OnDisconnection

  • .NET interface: AddinInstance_OnDisconnection

Obviously, this has been a slimmed-down, simple example. If you would like to see a real VB 6.0 add-in that even docks a Tool window in the Visual Studio .NET IDE, try the sample (cmdBrowser) project. You will need to compile the DLL under VB 6.0. Next, you will need to register the add-in in the proper place for Visual Studio .NET to find it and list it in its Add-in Manager dialog box. In the directory where you find the cmdBrowser project, you will also find a registration file named cmdbrws70.reg. The contents of the file are shown in Listing 1-4.

Listing 1-4: Creating a Registry Entry

start example
 REGEDIT4  [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\Addins]  [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\Addins\     CmdBrowser.Connect] "FriendlyName"="Command Browser" "Description"="View information about Visual Studio commands." "LoadBehavior"=dword:00000005 "CommandLineSafe"=dword:00000000 
end example

To run this registration file and register the add-in, double-click the file in Windows Explorer. The next time you start Visual Studio .NET, the add-in will appear in the Add-in Manager dialog box. Open the dialog box, click the check box to the left of the cmdBrowser add-in, and also click the StartUp check box. The add-in will start when you close the Add-in Manager dialog box.

 < Free Open Study > 

Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith © 2008-2017.
If you may any questions please contact us: