windows编程潘爱民com讲座_第1页
windows编程潘爱民com讲座_第2页
windows编程潘爱民com讲座_第3页
windows编程潘爱民com讲座_第4页
windows编程潘爱民com讲座_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

COMCOMDllGetClassObjectDllGetClassObject数调用DllGetClassObject引出–classCNewClass:publicCOldClass包容聚合对象对象对象classclassCB:publicISomeInterface,public{ CB(~CB();HRESULTstdcallQueryInterface(constIID&iid,void**ppv);ULONGstdcallAddRef();ULONGstdcall//ISomeInterfacemembersHRESULTstdcallSomeFunction();//IOtherInterfacemembersHRESULTstdcallOtherFunction();privateISomeInterfaceCB::CB({m_Ref=0;}CB::~CB({if(m_pSomeInterface!=NULL)}HRESULT{HRESULTresult=::CoCreateInstance(CLSID_ComponentA,NULL,CLSCTX_INPROC_SERVER,IID_ISomeInterface,(void**)&m_pSomeInterface);if(FAILED(result)) returnS_OK;}constIID&iid,void{CB HRESULT if(pUnknownOuter!=NULL)returnCLASS_E_NOAGGREGATION;pObj=newCB();if(pObj==NULL)returnif(FAILED(hr))deletepObj;}//Obtainthefirstinterfacepointer(whichdoesanAddRef)hr=pObj->QueryInterface(iid,ppv);return}HRESULTstdcallCB::SomeFunction({returnm_pSomeInterface->SomeFunction(}HRESULTstdcallCB::OtherFunction({}HRESULTHRESULTstdcallCB::SomeFunction({if(...{HRESULTresult=m_pSomeInterface->SomeFunction(return}{}}intFunc1(intx,inty){returnx*y;}intFunc2(intx,inty){returnint(*MyFunc)(int, MyFunc= inta=MyFunc(10, MyFunc=intb=MyFunc(10,classvirtualvoidstdcallMethod1(int,int)=0;virtualvoidstdcallMethod2(int)=0;classvirtualvoidstdcallOperator1(int,int)=0;virtualvoidstdcallOperator2(int)=0;classCMyObject:public指针类型并不实例化CMyObjectVtable1*p1=代码片断p1->Method1(a,代码片断Vtable2*p2=(Vtable2*)p1;p2->Operator1(a,b);对象外外义classCB:public{CB(~CBHRESULTstdcallQueryInterface(constIID&iid,void**ppv);ULONGstdcallAddRef();ULONGstdcall//IOtherInterfacemembersHRESULTstdcallOtherFunction();HRESULTprivateIUnknown*m_pUnknownInner;//pointertoA's{if(iid==IID_IUnknown)*ppv=(IUnknown*)this;((IUnknown*)(*ppv))->AddRef()}elseif(iid==IID_OtherInterface)*ppv=(IOtherInterface*)this;}elseif(iid==IID_SomeInterface)returnm_pUnknownInner->QueryInterface(iid,ppv)}else*ppv=}return}2.委托IUnknown(1)当对象被聚合时,调用外部对象的IUnknown(2)当对象未被聚合时,调用非委托IUnknown接IOtherInteISomeInteface 为controllingunknown内部对象类厂的e必须检查r参数,如果非L,则表明聚合,内部对象把指针保存起来,但不调用f。若内部对象不支持聚N如果pUnkOuter参数非NULL,并且外部对象请求对于除了非委托版本的Inknon之外的接口,它的三个Inknn调用必须全部委托给外部对象的pnkOtHRESULTCoCreateInstance(constCLSID&IUnknownconstIID&iid,(voidHRESULTconstIID&iid,voidclassCA:publicclassCA:publicISomeInterface,public{内义CA(IUnknown~CA(publicvirtualHRESULTstdcallQueryInterface(constIID&iid,void**ppv);virtualULONGstdcallAddRef();virtualULONGstdcallRelease()virtualHRESULTstdcallconstIID&iid,void**ppv);virtualULONG stdcallNondelegationAddRef();virtual stdcallNondelegationRelease()privateIUnknown*m_pUnknownOuter;//pointertoouterclass{virtualHRESULTstdcallconstIID&iid,void**ppv)=0;virtualULONGstdcallNondelegationAddRef()=0;virtualULONGstdcallNondelegationRelease()=0;HRESULTCA::NondelegationQueryInterface(constIID&iid,void{if(iid==IID_IUnknown)*ppv=(INondelegatingUnknown*)this;((IUnknown*)(*ppv))->AddRef();}elseif(iid==IID_SomeInterface)*ppv=(ISomeInterface*)this;((ISomeInterface*)(*ppv))->AddRef();}else*ppv=returnE_NOINTERFACE}return}ULONGCA::AddRef{if(m_pUnknownOuter!=NULLreturn}ULONGCA::Release{}HRESULTCA::QueryInterface(constIID&iid,void{if(m_pUnknownOuter!=NULL}厂的CreateInstance函数与包容模型相同。但{HRESULTresult=::CoCreateInstance(CLSID_ComponentA,pUnknownOuter,CLSCTX_INPROC_SERVER,if}CB::~CB({if(m_pUnknownInner!=NULL)m_pUnknownInner->Release();}CA::CA(IUnknown{CA::CA(IUnknown{m_pUnknownOuter=}constIID&iid,void{内//iidmustbeIID_IUnknownforif((pUnknownOuter!=NULL)&&(iid!=IID_IUnknown)部return对象//CreatetheobjectpassingfunctiontonotifyonCA*pObj=newCA创的if(pObj==NULL)return创建//Obtainthefirstinterfacepointer(whichdoesanAddRef)HRESULThr=pObj->NondelegatingQueryInterface(iid,ppv);returnhr;建}HRESULT{IUnknown*pUnknownOuter=(IUnknownHRESULTresult=::CoCreateInstance(CLSID_CompA,pUnknownOuter,IID_IUnknown,(void**)&m_pUnknownInner);if(FAILED(result))returnE_FAIL;result=m_pUnknownInner->QueryInterface(IID_ISomeInterface,(void**)&m_pSomeInterface);if{}returnS_OK;}constIID&iid,void{CB*pObj;HRESULThr;if(NULL!=pUnknownOuter)returnpObj=newCB();returnpObj->AddRef();//TheReferencecountofpObjis1hr=pObj->Init();if(FAILED(hr)) g_CompBNumber--;deletereturnE_FAIL;}hr=pObj->QueryInterface(iid,ppv); //TheReferencecountofpObjis1returnhr;}CB::~CB({m_Ref=IUnknown*pUnknownOuter=this;pUnknownOuter->AddRef();if(m_pSomeInterface!=NULL)if(m_pUnknownInner!=NULL)}(客户进程(组件进程(1)自定义列集法(custommarshaling)或基本列集法(basicmarshalingarchitecture)(2)标准列集法(standard列集:找到代理对象的CLSD象;列集数据包,包括跨进程信息。这些信息被称为对象引用(saedobtfeene)散集:客户进程中,根据传输过来的CLSD代理对象,并且把列集数据包传给代理对象。代理对象向客户返回一个接口指针。HRESULTCoMarshalInterface(IStream*pStm,REFIIDriid,IUnknown*pUnk,void*dwDestContext,unsignedlongpvDestContext,unsignedlongmshlflagsHRESULTCoUnmarshalInterface(IStreamREFIIDriid省代理对象,其CLSID为CLSID_StdMarshal)。classIMarshal:public{HRESULTGetUnmarshalClass(...)=0;HRESULTGetMarshalSizeMax(...)=0;HRESULTMarshalInterface(...)=0;HRESULTUnmarshalInterface(...)=0;HRESULTDisconnectObject(...)=0;HRESULTReleaseMarshalData(...)=0;调用IMarshal::UnmarshalInterface,把stream中的数据传给proxy,proxy根据而这个对象使用custom① ③调用类厂对对象不需要实现Irshal,COM提供代理对象,其CLI为CLI_tdarhal跨进程通信被抽象成RC通道,RC通道也是一个COM对象COMORPC建立在MSRPC基础上MSRPC{HRESULTGetBuffer(RPCOLEMESSAGEREFIIDriid)=HRESULTSendReceive(RPCOLEMESSAGEULONG*pStatus)=0;HRESULTFreeBuffer(RPCOLEMESSAGEpMessage)=0;HRESULTGetDestCtx(DWORD*pdwDestCtx,HRESULTIsConnected()=0;{HRESULTConnect(IRpcChannelBuffer*pRpcChannelBuffer)=0; Disconnect()=0;{ Connect(IUnknown*pUnkServer)=0; Disconnect()=0; Invoke(RPCOLEMESSAGEIRpcChannelBuffer*pChannel)=0;IRPCStubBuffer*IsIIDSupported(REFIIDiid)=0; CountRefs()=0; DebugServerQueryInterface(void**ppv)=0; DebugServerRelease(void*pv)=0;存根管理器通过Cnt方法把接口存根与目标对象联系起来clsid=LookUpInRegistry(iid);CoGetClassObject(clsid,clsid=LookUpInRegistry(iid);CoGetClassObject(clsid,CLSCTX_SERVER,NULL,IID_IPSFactoryBuffer,&pPSFactory));pPSFactory->CreateProxy(pUnkOuter,riid,&pProxy,clsid=LookUpInRegistry(iid);CoGetClassObject(clsid,CLSCTX_SERVER,NULL,IID_IPSFactoryBuffer,&pPSFactory));pPSFactory->CreateStub(iid,pUnkServer,如果是接口指针,调用如果是接口指针,则调用调用返回:对象-〉客户,处理[t]、nout参数及返回值在CGtCassObt函数内部,它找到EE组件的程序位置后,启动组件进程,等待...组件进程启动后,调用CoIntialze初始化,创建所有的类厂,调用CRegieCassObt把类厂注

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论