Creating and Using Add-Ins

See Also

You can create add-ins two different ways: with the aid of a wizard or manually.

Creating Add-Ins with the Add-In Wizard

The best and easiest way to create an add-in (if you're using a common shell language such as Visual J++) is using a tool called the Add-In Wizard.

To run the Add-In Wizard

  1. Download the Visual J++ Add-In Wizard from http://msdn.microsoft.com/visualj/downloads/samples.asp. The downloaded file includes a help file informing you where to install the wizard.

  2. Copy the file into your Windows system directory.

  3. Run regsvr32.exe on the DLL.

  4. From the Visual Studio add-ins in the New Project dialog box, select Java Add-In to launch the wizard.

The Add-In Wizard creates a basic add-in framework that you can run immediately after you finish. The wizard lets you supply a display name for the add-in (which appears in the Add-In Manager dialog box) and a description of the add-in. You can optionally choose to have the wizard generate the code to add a command to the Tools menu for invoking the add-in once it is loaded into the IDE. When the wizard is done, you will have a new project with a single item.

Note   If you are using Visual J++, and you don't install it in the default location, you must modify the add-ins project settings to correct the pathname for the alternate launch program, devenv.exe. If you launch devenv.exe as the alternate launch program for your add-in project, then the second instance runs in debug mode. This allows you to step through your add-in code in the first IDE instance and watch the effects of the add-in in the second IDE instance.

Manually Creating Add-Ins

Although it's highly recommended that you use the Add-In Wizard to create your add-ins, you can also create an add-in from scratch.

To manually create an add-in in Visual J++

  1. Import the requisite extensibility type libraries msaddndr, office97, and dte:
    import com.ms.vstudio6.dte.*;
    import com.ms.vstudio6.msaddndr.*;
    import com.ms.office97.*;
    
  2. Implement the IDTExtensiblity2 interface:
    Public Class MyAddin implements IDTExtensibility2
    

    When you implement the IDTExtensibility2 interface in your code and register the resulting DLL after building the code, the IDE can create an add-in server and call its exposed methods:
    Method Name Description
    OnConnection Called when an add-in is connected
    OnDisconnection Called when an add-in is disconnected.
    OnAddInsUpdate Called when the Addins collection is updated.
    OnStartupComplete Called when the system has completed starting.
    OnBeginShutdown Called when the system is being shut down.

    With the Object Browser, you can view the following type libraries to examine objects in the Extensibility object model:
    Type Library Name File Name Description
    Visual Studio 6.0 Extensibility dte.olb The DTE add-in object model
    Microsoft Add-In Designer msaddndr.olb The IDTExtensibility interface
    Microsoft Office 8.0 Object Library office97.olb Command bars
    VJExt vjext.olb Visual J++ project and code models

  3. Create void procedures for the previously mentioned interface methods. For example, create this procedure:
    public void OnConnection(Object VSInst, int ConnectMode, Object 
    AddInInst, SafeArray custom){
    }
    
  4. In the Project Properties dialog box, select the add-in to register it.

    When you select the add-in, code is added to your add-in to identify its class ID, GUID, and type library ID. The compiler's pre-processor uses this information in conjunction with a method called @com.register to register your add-in. The registration string looks like this:

    /**
    * @com.register (clsid=710B1AE4-9E3A-1101-883A-B07C0CC10000,
    typelib=710B1AE3-9E3A-1101-883A-B07C0CC10000)
    */
    

    Note   These numbers are unique to each add-in.

  5. Once these IDs are added to your code, take note of the name listed in the Project Properties dialog box, such as MyAddin.Connect. Use this name to add a program ID to the newly-generated registration string. Using the example in step 4, your code would look like this:
    /**
    * @com.register (clsid=710B1AE4-9E3A-1101-883A-B07C0CC10000,
    typelib=710B1AE3-9E3A-1101-883A-B07C0CC10000, progid="MyAddin.Connect")
    */
    

Here's how it looks when it's all put together:

import com.ms.vstudio6.dte.*;
import com.ms.vstudio6.msaddndr.*;
import com.ms.office97.*;

/**
* @com.register (clsid=710B1AE4-9E3A-1101-883A-B07C0CC10000,
typelib=710B1AE3-9E3A-1101-883A-B07C0CC10000, progid="MyAddin.Connect")
*/

public class MyAddin implements _IDTExtensibility2 {
   public void OnConnection(Object VSInst, int ConnectMode, Object AddInInst, SafeArray custom) {
   }
   public void OnDisconnection(int RemoveMode, SafeArray custom) {
   }
   public void OnAddInsUpdate(SafeArray custom) {
   }
   public void OnStartupComplete(SafeArray custom) {
   }
   public void OnBeginShutdown(SafeArray custom) {
   }
}

Note that this code serves as only a basic framework for common shell add-ins. From this point, you would add code to place the add-in as a command in a menu or command bar, create unique functionality for the add-in, and so forth.

Registering Add-Ins

Add-ins, whether created manually or with the Add-In Wizard, must be registered with Microsoft Windows, just as any other ActiveX server. Once registered, the IDE recognizes your add-in and lists it in the Add-In Manager. If you are using Microsoft Visual Basic, the Add-In Designer prompts you for this information and automatically performs all required add-in registration tasks. If you manually create the add-in in Visual J++, however, you must add a public method named OnCOMRegister to reveal the add-in to the Add-In Manager. (This is done for you if you use the Add-In Wizard.) For example, this code is required:

public static void onCOMRegister (boolean reg) {
   if (reg == true) { //Register
      try {
         RegKey regKey 
            = new RegKey(RegKey.getRootKey(RegKey.USER_ROOT),
            "Software\\Microsoft\\VisualStudio\\6.0\\Addins\\ 
            MyAddin.Connect ", RegKey.KEYOPEN_CREATE);
         regKey.setValue("Description", "Addin description goes 
          here.");       
         regKey.setValue("FriendlyName", "Friendly Add-In Name");
         regKey.setValue("LoadBehavior", 0);
         regKey.close();
      }
      catch (RegKeyException rke) {
         }
   }
   else {       //Unregister
      try {
         RegKey regKey 
            = new RegKey(RegKey.USER_ROOT,
            "Software\\Microsoft\\VisualStudio\\6.0\\Addins",
            RegKey.KEYOPEN_WRITE);
         regKey.deleteSubKey("MyAddin.Connect");
         regKey.close();
      }
      catch (RegKeyException rke) {
      }
   }
}

Alternatively, you can manually create your own .reg file. A typical .reg file for an add-in might look like this:

REGEDIT4
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\6.0\Addins\
   ProjectName.Classname
"Description"="A description of the add-in."
"FriendlyName"="My new addin"
"LoadBehavior"=dword:00000000

For information about initializing new add-ins in Visual Basic, see Using Add-In Designer in the Visual Basic documentation. Also see the sample add-in written in Java in the Visual J++ Samples directory.