COM - #include generated header vs #import generated tlb

…衆ロ難τιáo~ 提交于 2019-12-05 14:39:59

Why do COM DLLs have a TLB and a H?

The generated _i.h file contains the declarations you wrote in the IDL file in a format that's usable by a C or c++ compiler. The .tlb file is a type library, it contains the IDL declarations in a format that's usable by any language that supports COM. It gets embedded in the COM server DLL as a resource. Whomever uses your COM server will need it. If you don't build the proxy/stub DLL then it may also be needed at runtime to marshal calls across apartments.

What is the difference between using #include vs #import?

As long as the client is written in C or C++, #including the _i.h file is enough to get the necessary declarations to use the server. Do note however that the #import directive does more, it auto-generates a .tlh and a .tli file that get #included in the client code. These files declare smart pointer types for the interfaces in the COM server, types that make it a lot easier to use the server. Open these files in a text editor to see what they contain. If your client code uses the XxxxPtr types or catches the _com_error exceptions that are auto-generated from error return codes then you are looking at a very substantial rewrite of the client code if you don't want to use the #import directive.

If the COM server is stable and its interface declarations are not going to change anymore then you could check-in the .tlh and .tli files and replace the #import by two #includes for these files. Be sure to leave a comment in the code that shows a maintainer how to re-generate the files, "never change" is an elusive goal. And, of course, this trick isn't appropriate if you try to make /MP effective, that indicates that the COM server is still changing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!