C++ 如何从非托管 COM dll 生成类型库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4990045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:07:42  来源:igfitidea点击:

How to generate type library from unmanaged COM dll

c++comunmanagedregsvr32typelib

提问by RoboAlex

I have to use third party, unmanaged COM dll into my .NET application. I need to modify this assembly to produce a custom RCW. In order to Edit Interop AssemblyI need the type library of the particular assembly. Can any one explain me that how to How to generate type library from unmanaged COM dll?

我必须在我的 .NET 应用程序中使用第三方的非托管 COM dll。我需要修改此程序集以生成自定义 RCW。为了编辑互操作程序集,我需要特定程序集的类型库。任何人都可以向我解释如何从非托管 COM dll 生成类型库?

There is no option in regsvr32 to generate type library.

regsvr32 中没有选项可以生成类型库。

Thank you, Best Regards, Robo.

谢谢你,最好的问候,Robo。

采纳答案by smarcellus

If all you're trying to do is create an Interop Assembly from a native dll (and the native DLL embeds the TLB as a resource), you can just call tlbimp directly on the dll:

如果您要做的只是从本机 dll 创建互操作程序集(并且本机 DLL 将 TLB 作为资源嵌入),则可以直接在 dll 上调用 tlbimp:

tlbimp Foo.dll /out:Interop.Foo.dll

tlbimp Foo.dll /out:Interop.Foo.dll

Which will generate Interop.Foo.dll. You can then use ildasm to modify the IL:

这将生成 Interop.Foo.dll。然后您可以使用 ildasm 来修改 IL:

ildasm Interop.Foo.dll /out=Foo.il

回答by Guido Domenici

You need the OLE-COM Object Viewer, available as part of whatever is the latest Windows SDK. Then you can go to File->View Type Lib and save the IDL to a text file. Then you can use MIDL(also part of the Windows SDK) to regenerate a TLB and header file. Something like this should do for basic cases:

您需要OLE-COM 对象查看器,它是最新的Windows SDK 的一部分。然后您可以转到 File->View Type Lib 并将 IDL 保存到文本文件中。然后您可以使用MIDL(也是 Windows SDK 的一部分)重新生成 TLB 和头文件。对于基本情况,应该这样做:

midl /out c:\temp /header MyHeader.h MyIDLFile.idl

回答by wardies

If the typelib is embedded in the DLL Resources and the TLB file itself is what is required then 3rd party software can extract it (though, as others have pointed out, this may not be the most desirable option).

如果类型库嵌入在 DLL 资源中并且 TLB 文件本身是必需的,那么 3rd 方软件可以提取它(尽管正如其他人指出的那样,这可能不是最理想的选择)。

E.g. using Resource Hacker:

例如使用资源黑客

  1. Open the DLL file.
  2. Navigate to the TYPELIB\1\1033 (or whatever) node in the tree view.
  3. From the menu, choose Action -> Save Resource as binary file...
  4. Chose a file name and give it the .TLB extension.
  1. 打开 DLL 文件。
  2. 导航到树视图中的 TYPELIB\1\1033(或其他)节点。
  3. 从菜单中,选择操作 -> 将资源另存为二进制文件...
  4. 选择一个文件名并为其指定 .TLB 扩展名。

You can now reference that .TLB file and build without requiring the original DLL, e.g.

您现在可以引用该 .TLB 文件并在不需要原始 DLL 的情况下进行构建,例如

#import "test.tlb" named_guids

回答by MSalters

If all you have is that COM DLL, you can't generate a type library. A type library describes the COM interfaces implemented. But an unmanaged COM DLL merely needs to expose DllGetClassObject. This only gets you an IClassFactory, which lets you create new objects if you knwo the correct type up front.

如果您只有那个 COM DLL,则无法生成类型库。类型库描述了实现的 COM 接口。但非托管 COM DLL 只需要公开DllGetClassObject. 这只IClassFactory会给你一个,如果你预先知道正确的类型,它可以让你创建新对象。

回答by Boris Glick

Visual Studio IDE can directly extract binary resources from non-managed .exe and .dll files. If the type library is saved as a binary resource in a non-managedCOM DLL (e.g. one built using VS native C++ compiler), you can extract it as follows:

Visual Studio IDE 可以直接从非托管的 .exe 和 .dll 文件中提取二进制资源。如果类型库被保存为非托管COM DLL 中的二进制资源(例如使用 VS 原生 C++ 编译器构建的),您可以按如下方式提取它:

  1. Open the .dll file in the VS resource editor(the default editor when opening executables).
  2. Navigate to the type library resource ("TYPELIB", then 1) within the resource tree.
  3. Right-click on the type library resource and select export. That brings up a "Save File As" dialog box.
  4. In the "Save File As" box, change the filename from the default (typically, bin1.bin) to something like MyLibrary.tlband click Ok.
  5. Confirm by opening the exported .tlb file with OleView.exe(results should look identical to those you see by opening the .dll with the OleView.exe).
  1. 在 VS资源编辑器(打开可执行文件时的默认编辑器)中打开 .dll 文件。
  2. 导航到资源树中的类型库资源("TYPELIB",然后1)。
  3. 右键单击类型库资源并选择export。这会弹出一个“文件另存为”对话框。
  4. 在“文件另存为”框中,将文件名从默认值(通常为bin1.bin)更改为类似名称MyLibrary.tlb,然后单击“确定”。
  5. 通过打开导出的 .tlb 文件进行确认OleView.exe(结果应该与您通过打开 .dll 看到的相同OleView.exe)。

To extract type libraries from managedDLLs (e.g., the ones built using C#), VS includes the tool Tlbexp.exe(run it from the VS command prompt): https://msdn.microsoft.com/en-us/library/hfzzah2c(v=vs.110).aspx

要从托管DLL(例如,使用 C# 构建的 DLL)中提取类型库,VS 包含该工具Tlbexp.exe(从 VS 命令提示符运行):https: //msdn.microsoft.com/en-us/library/hfzzah2c(v =vs.110).aspx