COM-#include生成的标头与#import生成的tlb [英] COM - #include generated header vs #import generated tlb

查看:129
本文介绍了COM-#include生成的标头与#import生成的tlb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然后在另一个项目消耗该DLL的方式是这样使用的:

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

#import "projectname.tlb" named_guids no_namespace

我想更改为使用include而不是import。

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

原因想要从 #import 更改为 #include 的背后原因是我想启用 / MP 编译器切换以加快生成时间。

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

所以我想知道的是:


  • 为什么COM DLL具有TLB和H?

  • 应该使用哪个,为什么?

  • 什么是#include和#import之间有什么区别?

推荐答案


为什么COM DLL具有TLB和H?

Why do COM DLLs have a TLB and a H?

生成的 _i.h 文件包含您在IDL文件中以C或c ++编译器可用的格式编写的声明。 .tlb 文件是类型库,它包含IDL声明,该声明的格式可用于任何支持COM的语言。它作为资源嵌入到COM服务器DLL中。任何使用您的COM服务器的人都将需要它。如果您不构建代理/存根DLL,则在运行时可能也需要它来封送公寓间的呼叫。

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.


什么是#include与#import之间的区别?

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

只要客户端是用C或C ++编写的,#包括 _i.h 文件足以获取使用服务器所需的声明。但请注意,#import指令确实具有 more 的功能,它会自动生成 .tlh .tli 文件在客户端代码中包含#include。这些文件为COM服务器中的接口声明了智能指针类型,这些类型使它变得更容易使用服务器很多。在文本编辑器中打开这些文件以查看它们包含的内容。如果您的客户代码使用XxxxPtr类型或捕获到从错误返回代码自动生成的_com_error异常,那么如果您不想使用客户代码,则您正在查看客户代码的非常大量重写

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.

如果COM服务器稳定且其接口声明将不再更改,则可以检入.tlh和.tli文件,然后将这些文件的#import替换为两个#include。确保在代码中留下注释,该注释向维护者展示了如何重新生成文件,永不更改是一个难以实现的目标。而且,当然,如果您尝试使/ MP有效,则此技巧不合适,这表明COM服务器仍在更改。

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.

这篇关于COM-#include生成的标头与#import生成的tlb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆