Click here to Skip to main content
15,886,016 members
Articles / Programming Languages / C#

Working with the COM+ admin objects under .NET/C#

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
30 May 20013 min read 71.9K   789   19  
An article that explores ,NET interoperability with COM components.
/*

	<? Copyright Ranjeet Chakraborty 29/05/2001 ranjeetc@hotmail.com ?> 
	A basic C# Component that provides 2 helper classes 
	dispCOMWrapper --> Helper class that uses reflection to make late bound calls on a component's default interface.
	COMPlusAdminHelper --> Wraps the functionality that talks to the COM+ Admin objects, not all of its methods are 
				used though. Viz: deleteComPlusApp which could delete an app from your system given the 
				app's name as a parameter.(USE with extreme caution !)

*/

namespace COMHelperSpace
{
using System;
using System.Xml;
using System.Reflection;

	public 
	class dispCOMWrapper
	{
		private Object m_oCOM;
		private Type m_Type;
		public dispCOMWrapper (Object o)
		{
			this.m_oCOM = o;
			this.m_Type = this.m_oCOM.GetType();
		}
		public dispCOMWrapper (String strProgId)
		{
			Type typ;
			typ = Type.GetTypeFromProgID(strProgId);
			this.m_oCOM = Activator.CreateInstance(typ);
			this.m_Type = this.m_oCOM.GetType();
		}
		public Object getProperty(String strPropName)
		{
			Object oPropValue = this.m_Type.InvokeMember(strPropName,
									BindingFlags.InvokeMethod|BindingFlags.GetProperty,
									null,
									this.m_oCOM,
									new object [] {}
									);
			return oPropValue;
		}
		public Object getProperty(String strPropName,String strPropType )
		{
			Object oPropValue = this.m_Type.InvokeMember(strPropName,
									BindingFlags.InvokeMethod|BindingFlags.GetProperty,
									null,
									this.m_oCOM,
									new object [] {strPropType}
									);
			return oPropValue;
		}

		public Object getProperty(String strPropName,int iPropType )
		{
			Object oPropValue = this.m_Type.InvokeMember(strPropName,
									BindingFlags.InvokeMethod|BindingFlags.GetProperty,
									null,
									this.m_oCOM,
									new object [] {iPropType}
									);
			return oPropValue;
		}

		public void setProperty(String strPropName, Object [] args)
		{
			this.m_Type.InvokeMember(strPropName,
									BindingFlags.Default | BindingFlags.SetProperty,
									null,
									this.m_oCOM,
									args
									);
			return;

		}
		public Object callMethod(String strMethodName, Object [] args)
		{
			Object objRetVal = this.m_Type.InvokeMember(strMethodName,
									BindingFlags.Default | BindingFlags.InvokeMethod,
									null,
									this.m_oCOM,
									args
									);
			return objRetVal ;
		}
	}
}

namespace COMAdminHelperSpace
{
using COMHelperSpace;
using System;
using System.Xml;
using System.Reflection;

public 
	class COMPlusAdminHelper
	{
		private dispCOMWrapper m_oCatalog;
		private dispCOMWrapper m_oDispColApps;

		public COMPlusAdminHelper()
		{
			m_oCatalog = new dispCOMWrapper("COMAdmin.COMAdminCatalog");
			m_oDispColApps = new dispCOMWrapper(m_oCatalog.callMethod("GetCollection" , new Object [] {"Applications"} ));
		}

		public bool deleteComPlusApp(String strAppName)
		{
			bool blnRetVal = false;
			m_oDispColApps.callMethod("Populate",new object [] {});
			int j = (int)m_oDispColApps.getProperty("Count"), i = 0;
			while (i < j)
			{
				Object objTempApp = m_oDispColApps.getProperty("item", i) ;
				dispCOMWrapper oDispTempApp = new dispCOMWrapper(objTempApp);
				//Console.WriteLine(oDispTempApp.getProperty("Name").ToString());
				//Console.WriteLine(oDispTempApp.getProperty("Value").ToString());
				if (oDispTempApp.getProperty("Name").ToString() == strAppName)
				{
					m_oDispColApps.callMethod("Remove",new object [] {i});
					m_oDispColApps.callMethod("SaveChanges",new object [] {});
					blnRetVal = true;
					return blnRetVal;
				}
				i = i + 1;
			}
			return blnRetVal;
		}

		public void installComponent(String strAppName, String strComponentPath)
		{
			m_oCatalog.callMethod("InstallComponent" , new Object [] {strAppName, strComponentPath, "", ""} );
		}

		public void GenerateReport()
		{
			XmlDocument oDocNew = new XmlDocument();
			/*Create the main root Applications Element*/
			oDocNew.LoadXml("<?xml version='1.0'?><Applications></Applications>");


			XmlDocument oDoc = new XmlDocument();
			oDoc.Load ("ComPlusPropertyMap.xml");
			DocumentNavigator nav = new DocumentNavigator(oDoc);
			
			m_oDispColApps.callMethod("Populate",new object [] {});
			int j = (int)m_oDispColApps.getProperty("Count"), i = 0;
			while (i < j)
			{
				Object objTempApp = m_oDispColApps.getProperty("item", i) ;
				dispCOMWrapper oDispTempApp = new dispCOMWrapper(objTempApp);

				nav.Select ("/PropertyMap/Application");
				nav.MoveToNextSelected();
				nav.Select ("PropName");

				XmlNode oAppNode;		/*Create the Application for every COM+ App*/
				oAppNode = oDocNew.CreateNode (XmlNodeType.Element,"Application","");
				XmlElement oRootElement = oDocNew.DocumentElement; 
				XmlNode oNewAddedAppNode = oRootElement.AppendChild (oAppNode);
				XmlNamedNodeMap oNamedNodeMapForApps = oAppNode.Attributes; 
				XmlAttribute oNewAttribute;

				while (nav.MoveToNextSelected())
				{
					String strAttribName = nav.InnerText.ToString(), strAttribValue;
					oNewAttribute = oDocNew.CreateAttribute(strAttribName);
					strAttribValue = oDispTempApp.getProperty("Value",strAttribName).ToString();  
					oNewAttribute.Value = strAttribValue;
					oNamedNodeMapForApps.SetNamedItem(oNewAttribute); 
				}
				//Console.WriteLine(oDocNew.OuterXml.ToString()); 

				dispCOMWrapper m_oDispColComps = new dispCOMWrapper(m_oDispColApps.callMethod("GetCollection" , new Object [] {"Components", oDispTempApp.getProperty("Key")} ));
				m_oDispColComps.callMethod("Populate",new object [] {});
				int k = (int)m_oDispColComps.getProperty("Count"), l = 0;
				while(l < k)
				{
					Object objTempComp = m_oDispColComps.getProperty("item", l) ;
					dispCOMWrapper oDispTempComp = new dispCOMWrapper(objTempComp);
					nav.Select ("/PropertyMap/Component");
					nav.MoveToNextSelected();
					nav.Select ("PropName");

					XmlNode oCompNode;		/*Create the Component element for every COM+ App*/
					oCompNode = oDocNew.CreateNode (XmlNodeType.Element,"Component","");
					XmlNode oNewAddedCompNode =  oNewAddedAppNode.AppendChild (oCompNode);
					XmlNamedNodeMap oNamedNodeMapForComps = oCompNode.Attributes; 
					XmlAttribute oNewCompAttribute;
					XmlNode oCompAttribNode;

					while (nav.MoveToNextSelected())
					{
						String strCompAttribName = nav.InnerText.ToString(), strCompAttribValue;
						oCompAttribNode = oDocNew.CreateNode(XmlNodeType.Element,strCompAttribName,"");  
						strCompAttribValue = oDispTempComp.getProperty("Value",strCompAttribName).ToString();  
						oCompAttribNode.InnerText = strCompAttribValue;
						oNewAddedCompNode.AppendChild (oCompAttribNode);
					}
					l = l + 1;
				}
				i = i + 1;
			}
			oDocNew.Save ("CompPlusReport.xml");
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am originally from Mumbai, India, Currently living in Los Angeles doing Software Development. If there is something I miss here in LA, its the crazy Mumbai monsoon and delicious "wadapavs".
Apart from spending time writing software, I spend most of my time with XBox riding the Warthog and killing covenants in Halo.

Comments and Discussions