COM+ Automation using C#

Accessing COM+ Catalog

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

Security

To change data on the COM+ catalog, you need to have authority as anadministrator. An application using the COMAdmin objects must berunning under a user account that is assigned to the Administratorsrole on the system application on the machine that it is trying toadminister.

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<o:p></o:p>

Description<o:p></o:p>

Applications<o:p></o:p>

Contains an object for each COM+ application installed on the local machine.<o:p></o:p>

Components<o:p></o:p>

Holds an object for each component in the application to which it is related..<o:p></o:p>

ErrorInfo<o:p></o:p>

Retrieves extended error information regarding methods that deal with multiple objects.<o:p></o:p>

Roles<o:p></o:p>

Holds an object for each role assigned to the application to which it is related.<o:p></o:p>

RolesForComponent<o:p></o:p>

Holds an object for each role assigned to the component to which the collection is related.<o:p></o:p>

RolesForInterface<o:p></o:p>

Holds an object for each role assigned to the interface to which the collection is related.<o:p></o:p>

RolesForMethod<o:p></o:p>

Holds an object for each role assigned to the method to which the collection is related. <o:p></o:p>

RolesForPartition<o:p></o:p>

Holds an object for each role assigned to the partition to which the collection is related. <o:p></o:p>

UsersInRole<o:p></o:p>

Holds an object for each user in the role to which the collection is related.<o:p></o:p>

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.</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 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
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 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

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

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