How to generate type library from unmanaged COM dl

2019-02-04 19:18发布

问题:

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 Assembly I 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?

There is no option in regsvr32 to generate type library.

Thank you, Best Regards, Robo.

回答1:

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:

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

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

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


回答2:

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:

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


回答3:

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.



回答4:

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).

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.

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

#import "test.tlb" named_guids


回答5:

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-managed COM DLL (e.g. one built using VS native C++ compiler), you can extract it as follows:

  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.tlb and 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).

To extract type libraries from managed DLLs (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