Search code examples
c++visual-studiovisual-c++com

Import a dll file into another IDL file


I am working on a project for developing a proxy COM component for the given component in visual C++. If "Comp" is the original component and Proxy_Comp is the proxy component, then Proxy_Comp should be capable of having the same Interfaces as of the original component.

I think this should be done by importing only the "Comp.dll". One of my friends has done this successfully before. But I cannot do it. Could anyone please help?


Solution

  • I'll assume you are talking about the importlib directive in the IDL file. It requires a type library, the filename extension is .tlb. It is a binary file produced by running midl.exe on an .idl file that contains the .idl definitions in a compact form.

    A DLL is not a type library. It is a common convention however in COM Automation to embed the type library for the COM server as a resource inside the DLL. Quite handy to keep the server code and its interface definitions together. You can see this easily from Visual Studio, use File + Open + File and select the DLL. You could pick c:\windows\system32\shell32.dll as an example.

    The resource type name is invariably "TYPELIB", open the node to see the resource ID, invariably "1". You can double click it to look at the content, not very interesting since it is only a hex dump. You'll recognize the strings though, the names of the interfaces, coclasses and their members.

    Close that window and right-click the "1", note the Export context menu option. That lets you write a file to disk, name it something.tlb. You'll now have an exact copy of the .tlb that the importlib() directive likes.

    That .tlb can be decompiled in turn to re-generated the original .idl file. Start the Visual Studio prompt. Run oleview.exe, File + Open Typelib and select the .tlb. Set the focus to the right pane, type Ctrl+A, Ctrl+C and paste that into a text editor. Now you got the original .idl back, you could just use the import directive.

    This only has 100% fidelity if the original .idl file was COM Automation clean and didn't contain any cpp_quote.