COM培训

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

COM开发潘爱民2002-11-15内容Win32SDK和MFC介绍MFC对COM的支持用MFC开发COM组件ATL对COM的支持用ATL开发COM组件布置作业Win32SDK:Windows程序结构入口函数WinMain应用初始化主窗口的创建及显示消息分发循环程序结束处理Win32SDK对COM的支持Win32SDK包括COM库函数的支持Win32SDK提供的一些头文件的说明头文件说明Unknwn.h标准接口IUnknown和IClassFactory的IID及接口成员函数的定义。Wtypes.h包含了COM使用的数据结构的说明。Objidl.h所有标准接口的定义,既可用于C语言风格的定义,也可用于C++语言风格的定义。Comdef.h所有标准接口以及COM和OLE内部对象的CLSID。ObjBase.h所有的COMAPI函数的说明。Ole2.h所有经过封装的OLE辅助函数。利用宏描述接口DECLARE_INTERFACE_(IClassFactory,IUnknown){STDMETHOD(QueryInterface)(THIS_REFIIDriid,LPVOIDFAR*ppvObj)PURE;STDMETHOD_(ULONG,AddRef)(THIS)PURE;STDMETHOD_(ULONG,Release)(THIS)PURE;STDMETHOD(CreateInstance)(THIS_LPUNKNOWNpUnkOuter,REFIIDriid,LPVOIDFAR*ppvObject)PURE;STDMETHOD(LockServer)(THIS_BOOLfLock)PURE;};VC提供的用于描述接口的宏与COM接口有关的一些宏的说明宏说明DECLARE_INTERFACE(iface)声明接口iface,它不从其它的接口派生。DECLARE_INTERFACE_(iface,baseiface)声明接口iface,它从接口baseiface派生。STDMETHOD(method)声明接口成员函数method,函数返回类型为HRESULT。STDMETHOD_(type,method)声明接口成员函数method,函数返回类型为type。MFC基础应用类–AfxGetApp–CWinApp::InitInstance–CWinApp::ExitInstance–CWinApp::OnIdle–CWinApp::Run–CWnd*m_pMainWnd窗口类–AfxGetMainWndMFC的消息处理机制——消息映射表在CWnd派生类定义中加入声明:DECLARE_MESSAGE_MAP()在类的实现文件中加入表和表项的定义:BEGIN_MESSAGE_MAP(theClass,baseClass)......END_MESSAGE_MAP消息映射表示例BEGIN_MESSAGE_MAP(theClass,baseClass)//{{AFX_MSG_MAP(theClass)ON_WM_SETFOCUS()ON_WM_CREATE()ON_WM_DESTROY()ON_WM_CLOSE()ON_WM_SIZE()ON_MESSAGE(WM_COMMANDHELP,OnCommandHelp)ON_MESSAGE(WM_HELPHITTEST,OnHelpHitTest)ON_UPDATE_COMMAND_UI(ID_VIEW_STATUS_BAR,OnUpdateControlBarMenu)ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW,0,0xFFFF,OnToolTipText)//}}AFX_MSG_MAPEND_MESSAGE_MAPMFC应用类型常规应用:MDI应用、SDI应用、基于对话框程序DLL应用:静态连接MFC库的正规DLL、动态连接MFC库的正规DLL、MFC扩展DLL其他应用:–支持OLE服务或者包容器的SDI应用–支持OLE服务或者包容器的MDI应用–支持自动化(Automation)服务的SDI或者MDI程序–ActiveX控制应用(OCX应用)MFC库结构CObjectCCmdTargetCWnd应用类结构窗口支持异常类文件服务类文档类框架窗口类控制条类属性页表类对话框类视类控制类图形设备环境类控制支持类Windows套接字类图形对象类菜单类ODBC支持类DAO支持类同步类其它类:Internet支持类自动化类型运行时刻对象支持简单值类型结构其它支持类集合模板类用于同步的类数组类列表类映射类Internet类MFC对COM应用的支持用嵌套类实现COM接口classCDictionary{……//构造函数和析构函数HRESULT__stdcallQueryInterface(REFIIDiid,void**ppvObj);ULONG__stdcallAddRef();ULONG__stdcallRelease();classXDictionaryObj:publicIDictionary{public:CDictionary*m_pParent;virtualHRESULT__stdcallQueryInterface(REFIIDiid,void**ppvObj);virtualULONG__stdcallAddRef();virtualULONG__stdcallRelease();virtualBOOL__stdcallInitialize();…...virtualvoid__stdcallFreeLibrary();}m_dictionaryObj;未完用嵌套类实现COM接口(续一)classXSpellCheckObj:publicISpellCheck{public:CDictionary*m_pParent;virtualHRESULT__stdcallQueryInterface(REFIIDiid,void**ppvObj);virtualULONG__stdcallAddRef();virtualULONG__stdcallRelease();virtualBOOL__stdcallCheckWord(Stringword,String*);}m_spellCheckObj;private:structDictWord*m_pData;char*m_DictFilename[128];intm_Ref;intm_nWordNumber,m_nStructNumber;};续用嵌套类实现COM接口(续二)CDictionary::CDictionary(){.......//Initializtionm_dictionaryObj.m_pParent=this;m_spellCheckObj.m_pParent=this;}用嵌套类实现COM接口(续三)HRESULTCDictionary::QueryInterface(constIID&iid,void**ppvObj){if(iid==IID_IUnknown||iid==IID_Dictionary){*ppvObj=&m_dictionaryObj;AddRef();returnS_OK;}elseif(iid==IID_SpellCheck){*ppvObj=&m_spellCheckObj;AddRef();returnS_OK;}*ppv=NULL;returnE_NOINTERFACE;}用嵌套类实现COM接口(续四)ULONGCDictionary::XDictionaryObj::QueryInterface(constIID&iid,void**ppvObj){returnm_pParent-QueryInterface(iid,ppvObj);}ULONGCDictionary::XDictionaryObj::AddRef(){returnm_pParent-AddRef();}ULONGCDictionary::XDictionaryObj::Release(){returnm_pParent-Release();}“用嵌套类实现COM接口”原理m_pDatam_DictFilename[128]m_Refm_nWordNumberm_nStructNumberQueryInterfaceAddRefRelease…….m_dictionaryObjm_spellCheckObjQueryInterfaceAddRefRelease……QueryInterfaceAddRefRelease……VtableforIDictionaryVtableforISpellCheckCDictionaryvptrvptrCDictionary的非虚函数MFC:接口映射表CCmdTarget类CCmdTarget::m_dwRef为引用计数接口映射表与消息映射表非常类似接口映射表:记录了CCmdTarget类中每一个嵌套类的接口ID以及接口vtable与父类this指针之间的偏移量offsetof宏:成员类与父类之间的偏移值DECLARE_INTERFACE_MAP#defineDECLARE_INTERFACE_MAP()\private:\staticconstAFX_INTERFACEMAP_ENTRY_interfaceEntries[];\protected:\staticAFX_DATAconstAFX_INTERFACEMAPinterfaceMap;\staticconstAFX_INTERFACEMAP*PASCAL_GetBaseInterfaceMap();\virtualconstAFX_INTERFACEMAP*GetInterfaceMap()const;\structAFX_INTERFACEMAP_ENTRY{constvoid*piid;size_tnOffset;};structAFX_INTERFACEMAP{#ifdef_AFXDLLconstAFX_INTERFACEMAP*(PASCAL*pfnGetBaseMap)();#elseconstAFX_INTERFACEMAP*pBaseMap;#endifconstAFX_INTERFACEMAP_ENTRY*pEntry;};接口映射表定义BEGIN_INTERFACE_MAP(CDictionary,CCmdTarget)INTERFACE_PART(CDictionary,IID_IDictionary,Dictionary)INTERFACE_PART(CDictionary,IID_ISpellCheck,SpellCheck)END_INTERFACE_MAP()接口映射表的宏定义#defineBEGIN_INTERFACE_MAP(theClass,theBase)\constAFX_INTERFACEMAP*PASCALtheClass::_GetBaseInterfaceMap()\{return&theBase::interfaceMap;}\constAFX_INTERFACEMAP*theClass::GetInterfaceMap()const\{return&theClass::interfaceMap;}\AFX_COMDATconstAFX_DATADEF\AFX_INTERFACEMAPtheClass::interfaceMap=\{&theClass::_GetBaseInterfaceMap,&theClass::_interfaceEntries[0],};\AFX_COMDATconstAFX_DATADEFAFX_INTERFACEMAP_ENTRYtheClass::_interfaceEntries[

1 / 78
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功