COM+ Automation Using COMAdminCatalog in .NET C#

Environment: COM+, C#

Accessing COM+ Catalog

The COM+ catalog is the underlying data store that holds all COM+ configuration data. Whenever you do any kind of COM+ administration, you are reading and writing data stored in the catalog. The only way that you can access the catalog is through the Component Services administrative tool or through the COMAdmin library.

Security

To change data on the COM+ catalog, you need to have authority as an administrator. An application using the COMAdmin objects must be running under a user account that is assigned to the Administrator’s role on the system application on the machine that it is trying to administer.

The Applications collection contains an item for each COM+ application that is installed on the machine. The collections occur in a hierarchical structure; the Components collection is subsumed under the Applications collection and holds the components installed into that particular application. In other words, you need to step through several collections to get to the element you want. Each item in a collection exposes properties. These properties serve to hold configuration data for whatever element the item represents. Here are some of the collections that are available in COMAdminCatalog, you can find a complete list of collections on MSDN.

Collections Description
Applications Contains an object for each COM+ application installed on the local machine
Components Holds an object for each component in the application to which it is related
ErrorInfo Retrieves extended error information regarding methods that deal with multiple objects
Roles Holds an object for each role assigned to the application to which it is related
RolesForComponent Holds an object for each role assigned to the component to which the collection is related
RolesForInterface Holds an object for each role assigned to the interface to which the collection is related
RolesForMethod Holds an object for each role assigned to the method to which the collection is related
RolesForPartition Holds an object for each role assigned to the partition to which the collection is related
UsersInRole Holds an object for each user in the role to which the collection is related

Retrieving a COMAdminCatalog Application Collection

//Delegates
public delegate void DeleteEventHandler();
public delegate void ErrorMessageHandler(string s, Exception e);


//Events
/// <summary>Raised when the object is successfully deleted on
/// the remote host.</summary>
public event DeleteEventHandler Delete;
/// <summary>Raised when the object throws an exception.</summary>
public event ErrorMessageHandler Error;

//Method(s) to invoke the event
protected virtual void OnDelete()
{

    if(Delete != null)
    Delete();
}

#region Get COM + Applications
// Get COM + Applications

internal COMAdminCatalogCollection GetCollection()
{
    try
    {
        objAdmin = new COMAdmin.COMAdminCatalog();
        objRoot = objAdmin.Connect("192.168.1.150");

        objCollection =
            (COMAdmin.COMAdminCatalogCollection)
             objAdmin.GetCollection("Applications");
    }
    catch(Exception ex)
    {

        System.Windows.Forms.MessageBox.Show("Error : " + ex);
    }
    return objCollection;
}
    #endregion

Retrieving a COMAdminCatalog Component Collection

#region Get COM+ Applications / Components
// Return all COM + Applications and their corresponding
// components

public NameValueCollection GetCOMApplications()
{
    collection = new NameValueCollection();
    try
    {
        objCollection = GetCollection();
        objCollection.Populate();

        foreach(COMAdmin.COMAdminCatalogObject objAppNames
                in objCollection)
        {
            COMAdmin.ICatalogCollection objComponents =
            (COMAdmin.ICatalogCollection) objCollection.
             GetCollection("Components",objAppNames.Key);
            objComponents.Populate();
            foreach(COMAdmin.COMAdminCatalogObject Components
                    in objComponents)
            {
                collection.Add(Components.Name.ToString(),
                           objAppNames.Name.ToString());
            }
        }
    }
    catch(Exception e)
    {
        Error += new ErrorMessageHandler(Message);
        Error("GetCOMAPPlications",e);
        Dispose();
    }
   return collection;
}
#endregion

Deleting an Existing COM+ Application

#region Delete exisiting COM+ Application
public void DeleteCOMApplication(string appName)
{
    try
    {
        long l_Count = 0;
        ICatalogCollection pCollection = GetCollection();
        ICatalogObject pCatalog;
       pCollection.Populate();
        l_Count = pCollection.Count;
        if(l_Count == 0)
        return;

        for(int i= 0; i < l_Count; i ++)
        {
           pCatalog = (ICatalogObject) pCollection.get_Item(i);
            if(appName == (string) pCollection.get_Value("Name"))
            {
               pCollection.Remove(i);
               pCollection.SaveChanges();
               OnDelete();
               return;
            }
        }
    }
   catch(Exception e)
   {
       Error += new ErrorMessageHandler(Message);
         Error("Unable to delete the COM+ Application:" , e);
         Dispose();
   }
}
#endregion

Creating a New COM+ Application

Create a new COM+ application by adding a new item to the collection. When you set the properties on an item, no changes are actually recorded to the COM+ catalog until you explicitly save the changes. You do this by using the SaveChanges() method on the COMAdminCatalogCollection object for the collection containing the item.

#region CreateCOMApplication
/// <summary>
/// Create a new COM+ application
/// </summary>
public void CreateCOMApplication()
{
    bool Exists = false;
    try
     {
       Exists = ApplicationExists();    // Method to check whether
                                        // the application exists
                                        // in the Catalog
      if(!Exists)
      {
        objCollection = GetCollection();
        COMAdmin.COMAdminCatalogObject objObject =
        (COMAdmin.COMAdminCatalogObject) objCollection.Add();
        objObject.set_Value("Name",props.COM_Name);
        objObject.set_Value("Description",props.COM_Description);
        objObject.set_Value("Activation",props.COM_Type);
        objObject.set_Value("Identity",props.COM_User);
        objCollection.SaveChanges();
        OnCreate();
      }
      else
      {
        System.Windows.Forms.MessageBox.Show("An application with
               the same name already exixts in the Registry!",
               "Nexsure COM + Help",

      System.Windows.Forms.MessageBoxButtons.OK,System.Windows.
             Forms.MessageBoxIcon.Error);
    }
      }
     catch(Exception ex)
    {
      Error += new ErrorMessageHandler(Message);
      Error("Error creating COM+ Application :" , ex);
      Dispose();
    }
}
#endregion

internal  void Message(string message,Exception e)
{
    System.Windows.Forms.MessageBox.Show("Error occurred " +
                                          message + "
                                          error description : "
                                          + e ,"Nexsure COM + ",
    System.Windows.Forms.MessageBoxButtons.OK,System.Windows.
           Forms.MessageBoxIcon.Error);
}

Starting and Shutting Down COM+ Applications

#region Starting COM+ Application
public  bool StartCOMApplication()
{
      bool Startup = false;
      try
         {
           objCollection = GetCollection();    // Method returns
                                               // COM+ Applications
           objAdmin.StartApplication(applicationName);
           System.Windows.Forms.MessageBox.Show("Application was
                                                 successfully
                                                 started.");
           Startup = true;
          }
         catch
         {
           System.Windows.Forms.MessageBox.Show("Unable to start
                                                 the
                                                 application.");
          }
          return Startup;
}
#endregion

#region Shutting Down  COM+ Application
public  bool ShutDownCOMApplication()
{
    bool ShuttingDown = false;
    try
      {
        objCollection = GetCollection();
        objAdmin.ShutdownApplication(applicationName);
        System.Windows.Forms.MessageBox.Show("Application was
                                              successfully
                                              shut down.");
        ShuttingDown = true;
     }
    catch
    {
      System.Windows.Forms.MessageBox.Show("Unable to shut down
                                            the application.");
    }
     return ShuttingDown;
}
#endregion

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read