COM - #include generated header vs #import generated tlb

人盡茶涼 提交于 2019-12-22 09:18:04

问题


I have many projects of which generate COM DLL's, these projects output the following:

projectname_i.h
projectname_i.c
projectname_p.c
projectname_i.tlb

Then where another project consumes this DLL it is used like so:

#import "projectname.tlb" named_guids no_namespace

I want to change this to use include instead of import.

The reason behind wanting to change from #import to #include is because I want to enable the /MP compiler switch to speed up build times.

http://msdn.microsoft.com/en-us/library/bb385193.aspx

So what I would like to know is:

  • Why do COM DLLs have a TLB and a H?
  • Which should be used and why?
  • What is the difference between using #include vs #import? will there be any unforeseen consequences from switching to #include?

回答1:


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.



来源:https://stackoverflow.com/questions/13700266/com-include-generated-header-vs-import-generated-tlb

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