ArcGIS SDK  

Registering .NET Classes in COM Component Categories

Much of the extensibility of ArcGIS relies on COM component categories. In fact, most custom ArcGIS components must be registered in component categories appropriate to their intended context and function for the host application to make use of their functionality. For example, all ArcMap commands and tools must be registered in the ESRI Mx Commands component category. There are a few different ways you can register a .NET component in a particular category, but before doing so, the .NET components must be registered with COM. See the Registering .NET Components with COM' topic for details.

Customize Dialog Box

Custom .NET ArcGIS commands and tools can quickly be added to toolbars via the Add From File button on the Customize dialog box. In this case, you simply have to browse for the TLB and open it. The ArcGIS framework will automatically add the classes you select in the type library to the appropriate component category.

Categories Utility

Another option is to use the Component Categories Manager (Categories.exe). In this case you select the desired component category in the utility, browse for your type library, and choose the appropriate class.

COMRegisterFunction

The final and recommended solution is to add code to your .NET classes that will automatically register them in a particular component category whenever the component is registered with COM. The .NET Framework contains two attribute classes (ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute) that allow you to specify methods that will be called whenever your component is being registered or unregistered. Both methods are passed the CLSID of the class currently being registered, and with this information you can write code inside the methods to make the appropriate registry entries or deletions. Registering a component in a component category requires that you also know the component category’s unique ID (CATID).

The code excerpt below shows a custom ArcMap command that automatically registers itself in the MxCommands component category whenever the .NET assembly in which it resides is registered with COM.

[C#]
  public sealed class AngleAngleTool: BaseTool
  {
 
    [ComRegisterFunction()]
    static void Reg(String regKey)
    {
      Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(regKey.Substring(18)+ "\\Implemented Categories\\" + "{B56A7C42-83D4-11D2-A2E9-080009B6F22B}");
    }

    [ComUnregisterFunction()]
    static void Unreg(String regKey)
    {
      Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(regKey.Substring(18)+ "\\Implemented Categories\\" + "{B56A7C42-83D4-11D2-A2E9-080009B6F22B}");
    }

To simplify this process, ESRI provides classes for each component category ArcGIS exposes with static functions to register and unregister components. Each class knows the GUID of the component category it represents, so registering custom components becomes greatly simplified. For more details on using these classes, see the Working with the ESRI .NET Component Category Classes section of the ESRI.ArcGIS.Utilities utility classes topic.