How to read COM TypeLib with C# or C++?

前端 未结 1 2012
北荒
北荒 2021-01-06 09:01

My company has created several COM objects and they were using them happily from .NET. But now, our client wants to change to Java. I thought it would be interesting to use

1条回答
  •  执念已碎
    2021-01-06 09:53

    The official API is available here: Type Description Interfaces.

    You can use it from C++ directly but I suggest you use .NET (C# in my sample) with an extra tool that Microsoft has written long time ago (mine is dated 1997), named TLBINF32.DLL. It's also a COM object but is Automation (VBScript, Javascript, VB/VBA) and .NET compatible.

    You can find TLBINF32.DLL googling for it (this link seems to work today: tlbinf32.dll download, make sure you get the .ZIP file, not what they call the "fixer"...). Note it's a 32-bit DLL so your program must be compiled as 32-bit to be able to use it. I don't know of any 64-bit version.

    How to use this library is explained in detail here in this december 2000 MSDN magazine's article: Inspect COM Components Using the TypeLib Information Object Library. It's VB (not .NET) oriented, but it's quite easy to translate in .NET terms.

    Here is a sample console app in C# that just dumps all type info from a type lib (here MSHTML.TLB):

    class Program
    {
        static void Main(string[] args)
        {
            TypeLibInfo tli = new TypeLibInfo();
            tli.ContainingFile = @"c:\windows\system32\mshtml.tlb";
            foreach (TypeInfo ti in tli.TypeInfos)
            {
                Console.WriteLine(ti.Name);
                // etc...
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题