Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM - #include generated header vs #import generated tlb

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?
like image 812
paulm Avatar asked Dec 26 '22 13:12

paulm


1 Answers

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.

like image 109
Hans Passant Avatar answered Feb 03 '23 15:02

Hans Passant