|
Type Library Info, XML and a bit of XSL for fun! Author: Richard Anderson Pre-Reading This article assumes you understand COM, Type Libraries, XML and enough about VB that you can either use the code as a starting point, or, translate it into another language like C/C++. Introduction Whilst hanging around on the SOAP list, I saw a post from Don Box that mentioned that DevelopMentor have written a neat utility to convert COM type libraries into CDL - an XML representation for SOAP interfaces. For some strange reason, that reminded me about a mail I got from Richard Grimes a while back, saying he'd spotted that MS had created a component that simplified the usage of type libraries. So, I thought it would be fun to take a quick look at maybe using that typelib component with MSXML to generate a very simple XML representation of bits of the type library. The TypeLib Component - TLBINF32.DLL The typelib component seems to have fairly complete coverage for accessing a type library using VB which is great, because the standard COM interfaces are a bit arcane. To get the ball rolling, fire up VB and add a project reference to the somewhat shyly named type library TypeLib Information. Once you've added the reference, bring up the object browser and take a look at all the interfaces. If you've not used type libraries before, dig around on MSDN and look up interfaces like ITypeInfo. Loading a TypeLib The COM server the type library describes provides a few components, but the one we are interested in is TLIApplication. This component provides a couple of way of loading a typelib, pretty much as you'd expect: loading from a file, via the registry and a few other ways best left to another Byte Size article. I'll stick with the file approach, because it is the simplest. To load a typelib from a file, you create an instance of the TLIApplication component, and then use the TypeLibInfoFromFile method which is a member of the default interface. For my play app, I decided to load MSXML.DLL as shown here:
Dim app As TLIApplication
Dim tli As TypeLibInfo
Set app = New TLIApplication
Set tli = app.TypeLibInfoFromFile("c:\winnt\system32\msxml.dll")
MsgBox tli.Interfaces.Count
If you run this code, you'll see the current interface count is 29. OK, that's pretty simple, but if you've ever used type libraries in C/C++ it would have probably taken you a few hours to figure that out. Yep, the interfaces this component hides are not what one would call friendly. So, accessing the type library wasn't at all that painful, and it really doesn't get much harder. For example, to actually view all the interface names defined in the MSXML type library, we just add the following bit of code:
Dim intf As InterfaceInfo
For Each intf In tli.Interfaces
MsgBox intf.Name
Next intf
In the standard type library interfaces, you have to fiddle around with bits n bobs to determine whether an entry is an interface, method etc. With the MS typelib component, they've nicely modeled the various elements as interfaces and properties, so you don't have to spend hours pulling your hair out trying to figure out how the underling interfaces like ITypeInfo/ITypeLib expose the same information. Let's Create Some XML Now we've seen how to access some of the type information, let's create some basic XML to represent a bit of the type library. Bring up VB, and add a reference to the Microsoft XML, version 2.0 type library. You'll probably find this listed as Microsoft XML 1.0 when you initially add the reference, but it will change once you've added the reference. In our code we create an instance of the DOM document, create a root element called interfaces and then modify the for/each code to add a child interface as highlighted here:
Dim app As TLIApplication
Dim tli As TypeLibInfo
Set app = New TLIApplication
Set tli = app.TypeLibInfoFromFile("c:\winnt\system32\msxml.dll")
Dim doc As New DOMDocument
Dim Interfaces As IXMLDOMElement
Dim Interface As IXMLDOMElement
Set Interfaces = doc.createElement("Interfaces")
doc.appendChild Interfaces
Dim intf As InterfaceInfo
For Each intf In tli.Interfaces
Set Interface = doc.createElement(intf.Name)
Interfaces.appendChild Interface
Interface.setAttribute "GUID", intf.Guid
Next intf
To view the XML, either use IE5, or add the component reference Microsoft Internet Controls to the VB project, drop an instance of the webbrowser onto the form, and at the end of the above code add these lines:
doc.save "c:\interfaces.xml"
Me.WebBrowser1.Navigate2 "c:\interfaces.xml"
This code simply saves the DOM to disk, and then instructs the web browser to load it. If everything has worked right, you should see something like this: Mix in a bit of XSL The sample code that can be downloaded with this article actually creates XML that is different to that shown. The format is shown here: As you can see, I've re-structured the XML, and included some additional information such as the interface and method help strings. By adding this additional information, I can use a simple XSL stylesheet to transform the XML into some basic type library documentation as shown here: The stylesheet that produces this basic output is here:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="https://www.w3.org/TR/WD-xsl"
xmlns="https://www.w3.org/TR/REC-html40"
result-ns="">
<xsl:template match="/">
<HTML>
<BODY>
<xsl:for-each select="Interfaces/Interface">
<H2>Interface - <xsl:value-of select="@name"/></H2>
<P><xsl:value-of select="HelpString"/></P>
<xsl:for-each select="Method">
<H3>Method - <xsl:value-of select="@name"/></H3>
<P><xsl:value-of select="HelpString"/></P>
</xsl:for-each>
<HR />
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
The code behind the transform button is simply:
Dim xsl As New DOMDocument
Dim out As New DOMDocument
xsl.Load "typelib.xsl"
doc.transformNodeToObject xsl, out
out.save "c:\interfaces.htm"
Me.WebBrowser1.Navigate2 "c:\interfaces.htm"
Summary This Byte Size article has hopefully given you a few ideas about the possibilities of using COM type information in conjunction with XML, or for that matter any other file format. It is easy thanks to the typelib component, and the possibilities are endless. By using XSL can then produce reports on the type library in any fashion you like. Mail a question to the author!! As part of the IDevResource commitment to Open Publishing, all of our authors are available to answer all of your trickiest questions at Author Central. For information about the authors, or to mail a question, visit them at Author Central. Did you like this article? If you liked this article, tell us about it. You can email your remarks to us at [email protected] Want to read more articles by this author? Try these:
Some links on XML: Coming Soon Author Bio: Author: Richard Anderson Richard has been developing since around the age of eight. Richard has recently completed co-authoring Professional XML and ASP ProgRef 3.0. Other books co-authored by Richard include Beginning ASP components and Pro ASP 3.0. Go to Richards pages in Author Central. © 1999 IDevResource.comContribute to IDR: To contribute an article to IDR, a click here.
|
|