• 1356阅读
  • 4回复

com-tlb遍历(类型库) [复制链接]

上一主题 下一主题
离线啊冲
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2016-03-09

C/C++语言中需要事先对函数声明,那么就 会要求组件也必须提供C语言的头文件。不行!为了能使COM具有跨语言的能力,决定不再为任何语言提供对应的函数接口声明,而是独立地提供一个叫类型库(TLB)的声明。每个语言的IDE环境自己去根据TLB生成自己语言需要的包装。这个性质叫“接口声明的独立性” 遍历tlb:[backcolor=url(http://hgy413.com/wordpress/wp-content/plugins/wp-codebox/images/view_code.png) 0% 50% no-repeat]View Code CPP

1234567891011121314151617181920212223242526#include <atlcomcli.h>#include <OAIdl.h>using namespace std; int _tmain(int argc, _TCHAR* argv[]){CoInitialize(NULL); ITypeLib*            ptlb;HRESULT  hr = LoadTypeLib(L"d:\\1.tlb",&ptlb);if (SUCCEEDED(hr)){int count = ptlb->GetTypeInfoCount();for (int i=0;i<count;i++){ITypeInfo * pInfo;hr = ptlb->GetTypeInfo(i,&pInfo);if (SUCCEEDED(hr)){//...}}}system("pause");return 0;}


TYPEATTR: https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.comtypes.typeattr(v=vs.110).aspx
随手代码:[backcolor=url(http://hgy413.com/wordpress/wp-content/plugins/wp-codebox/images/view_code.png) 0% 50% no-repeat]View Code CPP

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179namespace DM{/// <summary>///        操作类型库TLB/// </summary>class CTypeLib{public:CTypeLib(){}CTypeLib(const CTypeLib& other):m_tlb(other.m_tlb){}CTypeLib(ITypeLib* tlb):m_tlb(tlb){}CTypeLib(LPCOLESTR file){LoadTypeLibEx(file, REGKIND_NONE, &m_tlb);} operator ITypeLib*(){return m_tlb.p;}ITypeLib* operator->(){return m_tlb.p;}ITypeLib** operator&(){return &m_tlb;} private:CComQIPtr<ITypeLib>                m_tlb;}; /// <summary>///        类型信息/// </summary>class CTypeInfo{public:CTypeInfo(){}CTypeInfo(const CTypeInfo &other) : m_ti(other.m_ti){}CTypeInfo(ITypeInfo* ti) : m_ti(ti){}CTypeInfo(IDispatch* disp){if (disp){HRESULT hr = disp->GetTypeInfo(0,0,&m_ti);}} operator ITypeInfo*(){return m_ti.p;}ITypeInfo* operator->(){return m_ti.p;}ITypeInfo** operator&(){return &m_ti;}private:CComQIPtr<ITypeInfo>            m_ti;}; /// <summary>///        ITypeInfo的属性/// </summary>class CTypeAttr{public:explicit CTypeAttr(ITypeInfo* ti) : m_ti(ti), m_typeAttr(NULL){if (m_ti){m_ti->GetTypeAttr(&m_typeAttr);}}explicit CTypeAttr(IDispatch* disp) : m_ti(disp), m_typeAttr(NULL){if (m_ti){m_ti->GetTypeAttr(&m_typeAttr);}}~CTypeAttr(){if (m_typeAttr){m_ti->ReleaseTypeAttr(m_typeAttr);}} operator TYPEATTR*(){return m_typeAttr;}TYPEATTR* operator->(){return m_typeAttr;} private:TYPEATTR*                 m_typeAttr;CTypeInfo                 m_ti;}; /// <summary>///        /// </summary>class CFuncDesc{public:CFuncDesc(ITypeInfo* ti, int index):m_ti(ti),m_funDesc(NULL){if (m_ti){m_ti->GetFuncDesc(index,&m_funDesc);}}~CFuncDesc(){if (m_funDesc){m_ti->ReleaseFuncDesc(m_funDesc);}} operator FUNCDESC*() {return m_funDesc;}FUNCDESC* operator->() {return m_funDesc;} private:FUNCDESC*                 m_funDesc;CTypeInfo                 m_ti;}; /// <summary>///        /// </summary>class CVarDesc{public:CVarDesc(ITypeInfo* ti, int index):m_ti(ti), m_varDesc(NULL){if (m_ti){m_ti->GetVarDesc(index, &m_varDesc);}}~CVarDesc(){if (m_varDesc){m_ti->ReleaseVarDesc(m_varDesc);}}operator VARDESC*(){return m_varDesc;}VARDESC* operator->(){return m_varDesc;} private:VARDESC*                  m_varDesc;CTypeInfo                 m_ti;}; /// <summary>///        /// </summary>class CLibAttr{public:explicit CLibAttr(ITypeLib* tlb): m_tlb(tlb),m_libAttr(NULL){if (m_tlb){m_tlb->GetLibAttr(&m_libAttr);}}~CLibAttr(){if (m_libAttr){m_tlb->ReleaseTLibAttr(m_libAttr);}} operator TLIBATTR*(){return m_libAttr;}TLIBATTR* operator->(){return m_libAttr;} private:TLIBATTR*                 m_libAttr;CTypeLib                  m_tlb;};}//namespace DM

善者 慈悲心常在 无怨无恨 以苦为乐
默认压缩密码www.hifyl.com
文件分享密码问题:http://www.hifyl.com/read-htm-tid-4444.html
离线雾∩_∩

只看该作者 沙发  发表于: 2016-03-16

离线xuaneye

只看该作者 板凳  发表于: 2016-04-17
      
离线v2680267313

只看该作者 地板  发表于: 2016-04-30
用户被禁言,该主题自动屏蔽!
离线浅若清风

只看该作者 4楼 发表于: 2016-05-14
用户被禁言,该主题自动屏蔽!
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个