I wrote a small program that generates xml from a type library. The type library can be a .tlb, or embedded as a resource in a PE (.dll, .ocx, .exe, etc).
I’m using this as a build tool—basically it’s the glue that makes a big project using C#, C++, VB6, WiX and NAnt hold together.
The source can be downloaded from this link. This doesn’t grab everything from the tlb, but it’s pretty simple and not hard to extend.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (c) 2005, Dan McKinley
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
- The name of Dan McKinley may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
using System;
using System.Collections.Generic;
using System.Text;
using isvc = System.Runtime.InteropServices;
using System.IO;
using System.Xml;
using System.Runtime.InteropServices.ComTypes;
namespace tlbspit
{
class Program
{
[isvc.DllImport("oleaut32.dll", PreserveSig = false)]
static extern void LoadTypeLib(
[isvc.MarshalAs(isvc.UnmanagedType.LPWStr)] string szFile,
[isvc.MarshalAs(isvc.UnmanagedType.Interface)] ref ITypeLib pLib);
static int Main(string[] args)
{
if (args.Length == 0)
{
Usage();
return -1;
}
try
{
string path = string.Join(" ", args);
using (XmlTextWriter w = new XmlTextWriter(Console.Out))
{
w.Formatting = Formatting.Indented;
Run(path, w);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return -1;
}
return 0;
}
static void Run(string path, XmlWriter w)
{
w.WriteStartElement("library");
ITypeLib tlb = null;
LoadTypeLib(path, ref tlb);
WriteTlbAttribs(tlb, w);
int ct = tlb.GetTypeInfoCount();
for (int i = 0; i < ct; i++)
{
ITypeInfo t = null;
tlb.GetTypeInfo(i, out t);
WriteType(t, w);
}
w.WriteEndElement();
}
static void WriteType(ITypeInfo t, XmlWriter w)
{
w.WriteStartElement("type");
ITypeInfo2 t2 = (ITypeInfo2)t;
TYPEKIND k;
t2.GetTypeKind(out k);
w.WriteAttributeString("name", NameOf(t));
w.WriteElementString("guid", GuidOf(t));
w.WriteElementString("kind", k.ToString());
w.WriteEndElement(); // type
}
static unsafe string GuidOf(ITypeInfo t)
{
IntPtr pAttr = IntPtr.Zero;
try
{
t.GetTypeAttr(out pAttr);
TYPEATTR* attr = (TYPEATTR*)pAttr;
Guid g = attr->guid;
return g.ToString();
}
finally
{
if (pAttr != IntPtr.Zero)
t.ReleaseTypeAttr(pAttr);
}
}
static string NameOf(ITypeInfo t)
{
string name = null, doc = null, hlpfile = null;
int i = 0;
t.GetDocumentation(-1, out name, out doc, out i, out hlpfile);
return name;
}
static string NameOf(ITypeLib tlb)
{
string name = null, doc = null, hlp = null;
int hc = 0;
tlb.GetDocumentation(-1, out name, out doc, out hc, out hlp);
return name;
}
static unsafe void WriteTlbAttribs(ITypeLib tlb, XmlWriter w)
{
IntPtr pAttr = IntPtr.Zero;
tlb.GetLibAttr(out pAttr);
try
{
TYPELIBATTR* attr = (TYPELIBATTR*)pAttr;
w.WriteAttributeString("guid", attr->guid.ToString());
w.WriteAttributeString("version",
string.Format("{0}.{1}", attr->wMajorVerNum, attr->wMinorVerNum));
w.WriteAttributeString("name", NameOf(tlb));
}
finally
{
if (pAttr != IntPtr.Zero)
tlb.ReleaseTLibAttr(pAttr);
}
}
static void Usage()
{
Console.WriteLine(@"
TLBSPIT by Dan McKinley
Writes out (some) of the attributes of a type library as xml to
standard output.
Usage: tlbspit.exe <type library file>
The file can be a dll/ocx/etc. or a tlb. It can optionally include a
slash followed by the number of a resource.
Examples:
tlbspit foo.tlb
tlbspit bar.dll\3
");
}
}
}