COM组件开发(精品).ppt
《COM组件开发(精品).ppt》由会员分享,可在线阅读,更多相关《COM组件开发(精品).ppt(78页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、COM开发潘爱民2002-11-15http:/ SDK和和MFC介绍介绍lMFC对对COM的支持的支持l用用MFC开发开发COM组件组件lATL对对COM的支持的支持l用用ATL开发开发COM组件组件l布置作业布置作业Win32 SDK:Windows程序结构l入口函数入口函数WinMainl应用初始化应用初始化l主窗口的创建及显示主窗口的创建及显示l消息分发循环消息分发循环l程序结束处理程序结束处理Win32 SDK对COM的支持lWin32 SDK包括包括COM库函数的支持库函数的支持利用宏描述接口 DECLARE_INTERFACE_(IClassFactory,IUnknown)ST
2、DMETHOD(QueryInterface)(THIS_ REFIID riid,LPVOID FAR*ppvObj)PURE;STDMETHOD_(ULONG,AddRef)(THIS)PURE;STDMETHOD_(ULONG,Release)(THIS)PURE;STDMETHOD(CreateInstance)(THIS_ LPUNKNOWN pUnkOuter,REFIID riid,LPVOID FAR*ppvObject)PURE;STDMETHOD(LockServer)(THIS_ BOOL fLock)PURE;VC提供的用于描述接口的宏MFC基础l应用类应用类AfxGe
3、tAppCWinApp:InitInstanceCWinApp:ExitInstanceCWinApp:OnIdleCWinApp:RunCWnd*m_pMainWndl窗口类窗口类AfxGetMainWndMFC的消息处理机制消息映射表l在在CWnd派生类定义中加入声明:派生类定义中加入声明:DECLARE_MESSAGE_MAP()l在类的实现文件中加入表和表项的定义:在类的实现文件中加入表和表项的定义:BEGIN_MESSAGE_MAP(theClass,baseClass).END_MESSAGE_MAP消息映射表示例BEGIN_MESSAGE_MAP(theClass,baseCla
4、ss)/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,0 xFFFF,OnToolTipText)/AFX_
5、MSG_MAPEND_MESSAGE_MAPMFC应用类型l常规应用:常规应用:MDI应用、应用、SDI应用、基于对话框应用、基于对话框程序程序lDLL应用:静态连接应用:静态连接MFC库的正规库的正规DLL、动态动态连接连接MFC库的正规库的正规DLL、MFC扩展扩展DLLl其他应用:其他应用:支持支持OLE服务或者包容器的服务或者包容器的SDI应用应用支持支持OLE服务或者包容器的服务或者包容器的MDI应用应用支持自动化支持自动化(Automation)服务的服务的SDI或者或者MDI程序程序ActiveX控制应用控制应用(OCX应用应用)MFC库结构MFC对COM应用的支持用嵌套类实现C
6、OM接口class CDictionary /构造函数和析构函数构造函数和析构函数HRESULT _stdcall QueryInterface(REFIID iid,void*ppvObj);ULONG _stdcall AddRef();ULONG _stdcall Release();class XDictionaryObj:public IDictionary public:CDictionary*m_pParent;virtual HRESULT _stdcall QueryInterface(REFIID iid,void*ppvObj);virtual ULONG _stdcal
7、l AddRef();virtual ULONG _stdcall Release();virtual BOOL _stdcall Initialize();.virtual void _stdcall FreeLibrary();m_dictionaryObj;未完未完用嵌套类实现COM接口(续一)class XSpellCheckObj:public ISpellCheck public:CDictionary*m_pParent;virtual HRESULT _stdcall QueryInterface(REFIID iid,void*ppvObj);virtual ULONG _s
8、tdcall AddRef();virtual ULONG _stdcall Release();virtual BOOL _stdcall CheckWord(String word,String*);m_spellCheckObj;private:structDictWord*m_pData;char*m_DictFilename128;intm_Ref;intm_nWordNumber,m_nStructNumber;续续用嵌套类实现COM接口(续二)CDictionary:CDictionary()./Initializtionm_dictionaryObj.m_pParent=thi
9、s;m_spellCheckObj.m_pParent=this;用嵌套类实现COM接口(续三)HRESULT CDictionary:QueryInterface(const IID&iid,void*ppvObj)if(iid=IID_IUnknown|iid=IID_Dictionary)*ppvObj=&m_dictionaryObj;AddRef();return S_OK;else if(iid=IID_SpellCheck)*ppvObj=&m_spellCheckObj;AddRef();return S_OK;*ppv=NULL;return E_NOINTERFACE;用嵌
10、套类实现COM接口(续四)ULONG CDictionary:XDictionaryObj:QueryInterface(const IID&iid,void*ppvObj)return m_pParent-QueryInterface(iid,ppvObj);ULONG CDictionary:XDictionaryObj:AddRef()return m_pParent-AddRef();ULONG CDictionary:XDictionaryObj:Release()return m_pParent-Release();“用嵌套类实现COM接口”原理m_pDatam_DictFilen
11、ame128m_Ref m_nWordNumberm_nStructNumberQueryInterfaceAddRefRelease.m_dictionaryObjm_spellCheckObjQueryInterfaceAddRefReleaseQueryInterfaceAddRefReleaseVtable for IDictionaryVtable for ISpellCheckCDictionaryvptrvptrCDictionary的非虚函数MFC:接口映射表lCCmdTarget类类lCCmdTarget:m_dwRef为引用计数为引用计数l接口映射表与消息映射表非常类似接口
12、映射表与消息映射表非常类似l接口映射表:记录了接口映射表:记录了CCmdTarget类中类中每一个嵌套类的接口每一个嵌套类的接口ID以及接口以及接口vtable与父类与父类this指针之间的偏移量指针之间的偏移量loffsetof宏:成员类与父类之间的偏移值宏:成员类与父类之间的偏移值DECLARE_INTERFACE_MAP#define DECLARE_INTERFACE_MAP()private:static const AFX_INTERFACEMAP_ENTRY _interfaceEntries;protected:static AFX_DATA const AFX_INTERFA
13、CEMAP interfaceMap;static const AFX_INTERFACEMAP*PASCAL _GetBaseInterfaceMap();virtual const AFX_INTERFACEMAP*GetInterfaceMap()const;struct AFX_INTERFACEMAP_ENTRYconst void*piid;size_t nOffset;struct AFX_INTERFACEMAP#ifdef _AFXDLLconst AFX_INTERFACEMAP*(PASCAL*pfnGetBaseMap)();#elseconst AFX_INTERFA
14、CEMAP*pBaseMap;#endifconst AFX_INTERFACEMAP_ENTRY*pEntry;接口映射表定义BEGIN_INTERFACE_MAP(CDictionary,CCmdTarget)INTERFACE_PART(CDictionary,IID_IDictionary,Dictionary)INTERFACE_PART(CDictionary,IID_ISpellCheck,SpellCheck)END_INTERFACE_MAP()接口映射表的宏定义#define BEGIN_INTERFACE_MAP(theClass,theBase)const AFX_IN
15、TERFACEMAP*PASCAL theClass:_GetBaseInterfaceMap()return&theBase:interfaceMap;const AFX_INTERFACEMAP*theClass:GetInterfaceMap()const return&theClass:interfaceMap;AFX_COMDAT const AFX_DATADEF AFX_INTERFACEMAP theClass:interfaceMap=&theClass:_GetBaseInterfaceMap,&theClass:_interfaceEntries0,;AFX_COMDAT
16、 const AFX_DATADEF AFX_INTERFACEMAP_ENTRY theClass:_interfaceEntries=#define INTERFACE_PART(theClass,iid,localClass)&iid,offsetof(theClass,m_x#localClass),#define END_INTERFACE_MAP()NULL,(size_t)-1 ;MFC版本的字典对象类定义class CDictionary:public CCmdTargetDECLARE_DYNCREATE(CDictionary)CDictionary();/protecte
17、d constructor used by dynamic creationDECLARE_INTERFACE_MAP()./IDictionaryBEGIN_INTERFACE_PART(Dictionary,IDictionary)INIT_INTERFACE_PART(CDictionary,Dictionary)STDMETHOD_(BOOL,Initialize)();STDMETHOD_(void,FreeLibrary)();END_INTERFACE_PART_STATIC(Dictionary)/ISpellCheckBEGIN_INTERFACE_PART(SpellChe
18、ck,ISpellCheck)INIT_INTERFACE_PART(CDictionary,SpellCheck)STDMETHOD_(BOOL,CheckWord)(LPOLESTR,LPOLESTR*);END_INTERFACE_PART_STATIC(SpellCheck);MFC版本的字典对象类实现STDMETHODIMP_(ULONG)CDictionary:XDictionary:AddRef()METHOD_PROLOGUE_EX_(CDictionary,Dictionary)return pThis-ExternalAddRef();lMETHOD_PROLOGUE_EX
19、_宏定义:#define METHOD_PROLOGUE_EX(theClass,localClass)theClass*pThis=(theClass*)(BYTE*)this-m_nOffset);AFX_MANAGE_STATE(pThis-m_pModuleState)pThis;/avoid warning from compiler CCmdTarget类实现IUnknownpublic:/data used when CCmdTarget is made OLE awarelong m_dwRef;LPUNKNOWN m_pOuterUnknown;/external contr
20、olling unknown if!=NULLDWORD m_xInnerUnknown;/place-holder for inner controlling unknownpublic:/advanced operationsvoid EnableAggregation();/call to enable aggregationvoid ExternalDisconnect();/forcibly disconnectLPUNKNOWN GetControllingUnknown();/get controlling IUnknown for aggregate creationCCmdT
21、arget类实现IUnknown(续)public:/these versions do not delegate to m_pOuterUnknownDWORD InternalQueryInterface(const void*,LPVOID*ppvObj);DWORD InternalAddRef();DWORD InternalRelease();/these versions delegate to m_pOuterUnknownDWORD ExternalQueryInterface(const void*,LPVOID*ppvObj);DWORD ExternalAddRef()
22、;DWORD ExternalRelease();CCmdTarget中QueryInterface实现DWORD CCmdTarget:InternalQueryInterface(const void*iid,LPVOID*ppvObj)/check local interfacesif(*ppvObj=GetInterface(iid)!=NULL)/interface was found-add a referenceExternalAddRef();return S_OK;/check aggregatesif(*ppvObj=QueryAggregates(iid)!=NULL)r
23、eturn S_OK;/interface ID not found,fail the callreturn(DWORD)E_NOINTERFACE;CCmdTarget中ExternalXXX成员实现DWORD CCmdTarget:ExternalAddRef()/delegate to controlling unknown if aggregatedif(m_pOuterUnknown!=NULL)return m_pOuterUnknown-AddRef();return InternalAddRef();DWORD CCmdTarget:ExternalRelease()/./Qu
24、eryInterface that is exported to normal clientsDWORD CCmdTarget:ExternalQueryInterface(const void*iid,LPVOID*ppvObj)/delegate to controlling unknown if aggregatedif(m_pOuterUnknown!=NULL)return m_pOuterUnknown-QueryInterface(*(IID*)iid,ppvObj);return InternalQueryInterface(iid,ppvObj);嵌套类内部实现IUnknow
25、n的成员函数STDMETHODIMP_(ULONG)CDictionary:XDictionary:QueryInterface(const void*iid,LPVOID*ppvObj)METHOD_PROLOGUE_EX_(CDictionary,Dictionary)return pThis-ExternalQueryInterface(iid,ppvObj);COM引出函数和类厂实现l在在AppWizard中选中中选中“Automation”检查框检查框STDAPI DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID*ppv)AFX
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- COM 组件 开发 精品
限制150内