Introduction
This sample covers the following concepts:
- Late binding to COM objects via Reflection
- Using the XmlDocument, DocumentNavigator, etc within the
System.Xml
namespace to load XML files and fire XPath querries.
- Using the COM+ admin objects to talk to the COM+
catalog and query the properties for each COM+ app installed as well as the
components within them.
Motivation
I wanted to explore the interoperability .NET provides
with COM components. What would be a better way than to test it with existing
components installed within the system. Since I already had worked with the COM+
admin objects sometime back and there not many samples are available on
it, I thought that working with it in C# would be cool.
Source/Misc Files | Descriptions |
estComAdmin.cs | Contains the client |
dispCOMWrapper.cs | Contains helper classes which are compiled as
components |
build.bat | Builds the source
files |
ComPlusPropertyMap.xml | Contains a complete list from
MSDN about the various properties exposed by the Applications as well as
the Components collection in XML format. *See diagram
below. |
Figure1 : ComPlusPropertyMap.xml
Description
There are 2 approaches I could have taken to use these objects:
- Use the tlbimp tool to generate equivalent .NET
metadata to be used within C# or
- Late binding via IDispatch using Reflection. I chose the latter purely to
explore reflection. Using tlbimp would have been easier.
When a .NET client tries to use and instantiate a COM component, the .NET
runtime exposes the object via something called a runtime callable wrapper (RCW)
that acts as a proxy for the real unmanaged object. The .NET client is fooled in
to thinking that it is talking to just another block of managed code. The
primary function of the RCW is to marshal calls between the boundaries of the
managed and unmanaged code. For more details/diagrams on the Runtime Callable
Wrappers refer to the .NET Framework SDK documentation under .NET Framework
Developer's Guide\Interoperating with Unmanaged Code\Advanced COM Interop\COM
Interop Wrappers. Every body starts off/steals/borrows/enhances code from
there.
In the example, I have create an instance of the
COMAdminCatalog
object along with these lines. Using this object I
can get at the Application's collection exposed as a
COMAdminCatalogCollection
Object.
The sample comes with two helper classes
dispCOMWrapper
and COMPlusAdminHelper
.The following lines uses these helper
classes to print out the count of all the COM+ apps installed on your
system.
dispCOMWrapper m_oCatalog;
dispCOMWrapper m_oDispColApps;
m_oCatalog = new dispCOMWrapper("COMAdmin.COMAdminCatalog");
m_oDispColApps = new dispCOMWrapper(m_oCatalog.callMethod("GetCollection",
new Object [] {"Applications"} ));
m_oDispColApps.callMethod("Populate",new object [] {});
Console.Write( "Total no of COM+ applications on your system :" +
m_oDispColApps.getProperty("Count").ToString());
There is a method known as
GenerateReport()
within the COMPlusAdminHelper
class
that peeks in to the COM+ catalog and iterates each and every application and
then through each and every Component within it, and while it does, it generates
an XML based report in the same directory as the executable.
One can
write equivalent XSL to display it in a more intuitive UI than the default
vanilla XSL that IE provides.
The
output file generated looks something like this:
Figure 2 :ComPlusReport.xml
Figure 3: The structure of the generated XML file.
To read/write/navigate xml files/nodes I
have used the following classes provided the System.Xml namespace:
- DocumentNavigator,
- XmlNode,
- XmlElement,
- XmlNamedNodeMap,
- XmlAttribute
One thing I have to admit. I found it difficult to initially move to a syntax
different than the regular MSXML syntax for firing XPath querries, but then no
choice, you gotta learn it.
Build:
Run build.bat, and then the sample.
References:
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/xml.asp (From the guru
Aaron Skonnard himself)
http://www.codeproject.com/com/complus_admin.asp (using
COM+ admin objects from VC++)
Note
I haven't done much bug
testing though and not much exception handling :( Lemme know if any bugs.
And as usual as Paul Di Lascia says, If this works I am
reponsible or I don't know who the hell did it !
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.