COM+ Automation using .NET C#
Accessing COM+ Catalog
The COM+ catalog is theunderlying data store that holds all COM+ configuration data. Wheneveryou do any kind of COM+ administration, you are reading and writingdata stored in the catalog. The only way that you can access thecatalog is through the Component Services administrative tool orthrough the COMAdmin library.
Security
To change data on the COM+ catalog, you needto have authority as an administrator. An application using theCOMAdmin objects must be running under a user account that is assignedto the Administrators role on the system application on the machinethat it is trying to administer.
The Applications collection contains an itemfor each COM+ application that is installed on the machine. Thecollections occur in a hierarchical structure, the Componentscollection is subsumed under the Applications collection and holds thecomponents installed into that particular application. In other wordsyou need to step through several collections to get to the element youwant. Each item in a collection exposes properties. These propertiesserve to hold configuration data for whatever element the itemrepresents. Here are some of the collections that are available inCOMAdminCatalog, 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 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.</summar>
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 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
Delete 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 newitem to the collection. When you set properties on an item, no changesare actually recorded to the COM+ catalog until you explicitly savechanges. You do this using the SaveChanges() method on theCOMAdminCatalogCollection object for the collection containing theitem.
#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 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 same name already exixts in regis
try !.","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 occured " + 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 return 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 shutdown.");
ShuttingDown = true;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to shutdown the application.");
}
return ShuttingDown;
}
#endregion




24. Feb, 2007 by 







No comments yet... Be the first to leave a reply!