問題描述
我的公司已經創建了幾個 COM 對象,他們在 .NET 中愉快地使用它們.但是現在,我們的客戶想要更改為 Java.我認為將 JACOB 或 j-interop(我不確定它們中的哪一個)用于某些任務會很有趣,但是生成的代碼非常難以管理.所以我想寫一個工具,可以讀取 COM 庫的 TypeLib,然后生成 Java 包裝類來隱藏所有那些無法管理的代碼.
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 JACOB or j-interop (I'm not sure which of them) for some tasks, but the resultant code is pretty unmanageable. So I want to write a tool that can read the TypeLib of the COM library and then generate Java wrapper classes for hidding all those unmanageable code.
我是COM世界的新手,所以我不知道如何獲取描述COM對象的接口、方法和參數的信息.我讀到了一個叫做 TypeLib 的東西,但我不知道怎么讀.如何從中獲取信息?
I'm a newbie in the COM world, so I don't know how to obtain the information about interfaces, methods and parameters that describe a COM object. I read about something called TypeLib, but I don't know how to read it. How can I obtain information from it?
推薦答案
官方 API 在這里:類型描述接口.
The official API is available here: Type Description Interfaces.
您可以直接從 C++ 中使用它,但我建議您使用 .NET(我的示例中為 C#)和 Microsoft 很久以前編寫的額外工具(我的日期為 1997 年),名為 TLBINF32.DLL.它也是一個 COM 對象,但兼容自動化(VBScript、Javascript、VB/VBA)和 .NET.
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.
您可以通過谷歌搜索找到 TLBINF32.DLL(此鏈接今天似乎有效:tlbinf32.dll 下載,確保您獲得的是 .ZIP 文件,而不是他們所謂的修復程序"...).請注意,它是一個 32 位的 DLL,因此您的程序必須編譯為 32 位才能使用它.我不知道任何 64 位版本,但這里描述了如何將其與 64 位客戶端一起使用:tlbinf32.dll in一個 64 位 .Net 應用程序
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 but how to use it a with 64-bit client is described here: tlbinf32.dll in a 64bits .Net application
如何使用這個庫在 2000 年 12 月的 MSDN 雜志的這篇文章中進行了詳細說明:使用 TypeLib 信息對象庫檢查 COM 組件.它是面向 VB(不是 .NET)的,但用 .NET 術語翻譯很容易.
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.
這是一個 C# 中的示例控制臺應用程序,它只是從類型庫(此處為 MSHTML.TLB)中轉儲所有類型信息:
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:windowssystem32mshtml.tlb";
foreach (TypeInfo ti in tli.TypeInfos)
{
Console.WriteLine(ti.Name);
// etc...
}
}
}
這篇關于如何使用 C# 或 C++ 讀取 COM TypeLib?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!