How to generate type library from unmanaged COM dll
Asked Answered
C

5

16

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.

Cytokinesis answered 14/2, 2011 at 8:19 Comment(0)
P
7

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
Pundit answered 24/2, 2011 at 18:38 Comment(1)
actually I thought tlbimp requires Foo.tlb to generate Interop.Foo.dll as it explained in linkEdit Interop Assembly. Thanks for your hintCytokinesis
W
15

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
Whitebook answered 14/2, 2011 at 8:49 Comment(1)
Hi Guido Thanks a lot. but I'm getting the following error. I ll google it and short it out. [.\Sample.IDL(60):error MIDL2025:syntax error : expecting a type specification near "single"]Cytokinesis
P
7

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
Pundit answered 24/2, 2011 at 18:38 Comment(1)
actually I thought tlbimp requires Foo.tlb to generate Interop.Foo.dll as it explained in linkEdit Interop Assembly. Thanks for your hintCytokinesis
R
6

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.

Retrorocket answered 14/2, 2011 at 8:51 Comment(2)
The typelib could be in the DLL resources and then it can be viewed by OleView and reconstructred.Incense
@sharptooth: Indeed there are cases in which you already have a embedded typelib. But then the question makes no sense. Tlbimp.exe can parse typelibs in DLLs (use inputDLL\1 to specify the resource number)Retrorocket
G
6

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
Graybeard answered 22/4, 2014 at 16:2 Comment(0)
C
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

Colossians answered 6/2, 2017 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.