VS2012中使用(#import导入)tlb文件

虽然作为VC++用户,生成tlb文件后我们可以直接使用当时生成的*_i.h与*_i.c文件,这样确实方便。不过既然Microsoft提供了#import方法,那我还是试试吧。

参考:

#import与#include或者#import *.dll VS #import *.tlb的差异:

forums.codeguru.com/showthread.php?376416-What-is-the-difference-between-import-and-include

#improt用法:http://msdn.microsoft.com/en-us/library/8etzzkb6.aspx


环境:VS2012 , Win8.1 64bit CS

项目环境:

ATLProject1负责提供com接口IFun并生成tlb,生成目录指定为demo\debug

MFCApplication1负责导入tlb,生成目录指定为demo\debug

步骤:

1. 生成tlb文件,略


2. 添加"demo\debug"到MFCApplication1的"VC++目录"->"库目录" (VS会搜索*.tlb文件)

3. MFCApplication1的stdafx.h里添加 引用语句

4. 按照传言,我们需要编译一次MFCApplication1,这样会才会生成tlh与tli文件,而这2个文件相当于头文件与实现。

    经过实验与理解,我们不应该#include "*.tlh"文件,因为在compile时VS会自动把刚在‘中间目录’生成的*.TLH/*.TLI代码包含进我们的源码。如果#include *.tlh,反而不对。

 

   (关于tlh与tli是干什么的,MSDN里已经提到了:#importcreates two header files that reconstruct the type library contents in C++ source code. The primary header file is similar to that produced by the Microsoft Interface Definition Language (MIDL) compiler, but with additional compiler-generated code and data. Theprimary header file has the same base name as the type library, plus a .TLH extension. The secondary header file has the same base name as the type library, with a .TLI extension. It contains the implementations for compiler-generated member functions, and is included (#include) in the primary header file.)

5.    VS的Inteligense提示接口没有定义

     

6. 添加完后发现还是提示”未定义标识符“,指明接口未定义,原来#import时自动给我的接口加上了namespace。


     所以我们要把第3步改为#import "ATLProject1.tlb" no_namespace, 然后重新编译生成*.tlh与*.tli。如此我们才能在代码中使用IFun接口而不报错.

当然也可以using namespace 自动生成的namespace名字,就不加no_namespace了

7.     如果过段时间*.TLB文件更新了,我们要更新自己的项目。clean项目,VS的inteligense也会提示接口没有定义,没关系,编译项目生成新的*.TLH、*.TLI文件继续写

8.  推荐: 我们可以把#import放进stdafx.h里,然后编译stdafx.cpp,这样也会生成*.TLH/*.TLI文件,而且更方便。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值