4

我有许多生成 COM DLL 的项目,这些项目输出以下内容:

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

然后在另一个项目使用这个 DLL 的地方,它的使用方式如下:

#import "projectname.tlb" named_guids no_namespace

我想将其更改为使用包含而不是导入。

#import想要从to更改背后的原因#include是因为我想启用/MP编译器开关以加快构建时间。

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

所以我想知道的是:

  • 为什么 COM DLL 有一个 TLB 和一个 H?
  • 应该使用哪个,为什么?
  • 使用#include 和#import 有什么区别?切换到#include 会有什么不可预见的后果吗?
4

1 回答 1

10

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.

于 2012-12-04T13:05:01.510 回答