《XPCOM组件开发》笔记(三)

XPCOM支持的每种语言都必须有自己的组件加载器。

XPCOM组件至少有三层,从里到外是:1)核心XPCOM对象。2)工厂代码 3)模块代码<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

核心XPCOM对象是实现你所需要的功能的对象,其他层是用来支持它,将它插入到XPCOM系统中的。一个单独的库可能有很多个这样的核心对象。

在核心对象层上面的是工厂层,工厂对象提供了XPCOM对象的基本抽象。

模块层在最外面,模块接口提供了另一种抽象为工厂提供了抽象并允许有多个工厂对象。从组件库的外部只有唯一的入口点,NSGetModule().这个入口点可以扇出任意工厂,从这些工厂创建出任意XPCOM对象

None.gif#include<stdio.h>
None.gif
#defineMOZILLA_STRICT_API
None.gif#include
"nsIModule.h"
None.gif#include
"nsIFactory.h"
None.gif#include
"nsIComponentManager.h"
None.gif#include
"nsIComponentRegistrar.h"
None.gif
None.gif
staticconstnsIIDkIModuleIID=NS_IMODULE_IID;
None.gif
staticconstnsIIDkIFactoryIID=NS_IFACTORY_IID;
None.gif
staticconstnsIIDkISupportsIID=NS_ISUPPORTS_IID;
None.gif
staticconstnsIIDkIComponentRegistrarIID=NS_ICOMPONENTREGISTRAR_IID;
None.gif
None.gif
#defineSAMPLE_CID/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{0x777f7150,0x4a2b,0x4301,/
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{0xad,0x10,0x5e,0xab,0x25,0xb3,0x22,0xaa}}

None.gif
staticconstnsCIDkSampleCID=SAMPLE_CID;
None.gif
None.gif
classSample:publicnsISupports
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
private:
InBlock.gifnsrefcntmRefCnt;
InBlock.gif
InBlock.gif
public:
InBlock.gifSample();
InBlock.gif
virtual~Sample();
InBlock.gifNS_IMETHODQueryInterface(
constnsIID&aIID,void**aResult);
InBlock.gifNS_IMETHOD_(nsrefcnt)AddRef(
void);
InBlock.gifNS_IMETHOD_(nsrefcnt)Release(
void);
ExpandedBlockEnd.gif}
;
None.gif
None.gifSample::Sample()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif:mRefCnt(
0);
ExpandedBlockEnd.gif}

None.gifSample::
~Sample()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedBlockEnd.gif}

None.gif
None.gifNS_IMETHODIMPSample::QueryInterface(
constnsIID&aIID,void**aResult)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aResult==NULL)dot.gif{
InBlock.gif
returnNS_ERROR_NULL_POINTER;
ExpandedSubBlockEnd.gif}

InBlock.gif
*aResult=NULL;
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aIID.Equals(kISupportsIID))dot.gif{
InBlock.gif
*aResult=(void*)this;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aResult!=NULL)dot.gif{
InBlock.gif
returnNS_ERROR_NO_INTERFACE;
ExpandedSubBlockEnd.gif}

InBlock.gifAddRef();
InBlock.gif
returnNS_OK;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMP_(nsrefcnt)Sample::AddRef()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return++mRefCnt;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMP_(nsrefcnt)Sample::Release()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(--mRefCnt==0)dot.gif{
InBlock.gifdelete
this;
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnmRefCnt;
ExpandedBlockEnd.gif}

None.gif
None.gif
//factoryimplementationclassforcomponent
None.gif
classSampleFactory:publicnsIFactory
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
private:
InBlock.gifnsrefcntmRefCnt;
InBlock.gif
public:
InBlock.gifSampleFactory();
InBlock.gif
virtual~SampleFactory();
InBlock.gifNS_IMETHODQueryInterface(
constnsIID&aIID,void**aResult);
InBlock.gifNS_IMETHOD_(nsrefcnt)AddRef(
void);
InBlock.gifNS_IMETHOD_(nsrefcnt)Release(
void);
InBlock.gifNS_IMETHODCreateInstance(nsISupports
*aOuter,constnsIID&iid,void**result);
InBlock.gifNS_IMETHODLockFactory(PRBool
lock);
ExpandedBlockEnd.gif}
;
None.gifSampleFactory::SampleFactory()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifmRefCnt
=0;
ExpandedBlockEnd.gif}

None.gifSampleFactory::
~SampleFactory()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleFactory::QueryInterface(
constnsIID&aIID,void**aResult)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aResult==NULL)dot.gif{
InBlock.gif
returnNS_ERROR_NULL_POINTER;
ExpandedSubBlockEnd.gif}

InBlock.gif
*aResult=NULL;
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aIID.Equals(kISupportsIID))dot.gif{
InBlock.gif
*aResult=(void*)this;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aIID.Equals(kIFactoryIID))dot.gif{
InBlock.gif
*aResult=(void*)this;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
if(aResult!=NULL)dot.gif{
InBlock.gif
returnNS_ERROR_NO_INTERFACE;
ExpandedSubBlockEnd.gif}

InBlock.gifAddRef();
InBlock.gif
returnNS_OK;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMP_(nsrefcnt)SampleFactory::AddRef()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
return++mRefCnt;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMP_(nsrefcnt)SampleFactory::Release()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(--mRefCnt==0)dot.gif{
InBlock.gifdelete
this;
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnmRefCnt;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleFactory::CreateInstance(nsISupports
*aOuter,constnsIID&iid,void**result)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(!result)
InBlock.gif
returnNS_ERROR_INVALID_ARG;
InBlock.gifSample
*sample=newSample();
InBlock.gif
if(!sample)
InBlock.gif
returnNS_ERROR_OUT_OF_MEMORY;
InBlock.gifnsresultrv
=sample->QueryInterface(iid,result);
InBlock.gif
if(NS_FAILED(rv))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
*result=nsnull;
InBlock.gifdeletesample;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnrv;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleFactory::LockFactory(PRBool
lock)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
returnNS_ERROR_NOT_IMPLEMENTED;
ExpandedBlockEnd.gif}

None.gif
//Moduleimplementation
None.gif
classSampleModule:publicnsIModule
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gifSampleModule();
InBlock.gif
virtual~SampleModule();
InBlock.gif
//nsISupportsmethods:
InBlock.gif
NS_IMETHODQueryInterface(constnsIID&uuid,void**result);
InBlock.gifNS_IMETHOD_(nsrefcnt)AddRef(
void);
InBlock.gifNS_IMETHOD_(nsrefcnt)Release(
void);
InBlock.gif
//nsIModulemethods:
InBlock.gif
NS_IMETHODGetClassObject(nsIComponentManager*aCompMgr,constnsCID&aClass,constnsIID&aIID,void**aResult);
InBlock.gifNS_IMETHODRegisterSelf(nsIComponentManager
*aCompMgr,nsIFile*aLocation,constchar*aLoaderStr,constchar*aType);
InBlock.gifNS_IMETHODUnregisterSelf(nsIComponentManager
*aCompMgr,nsIFile*aLocation,constchar*aLoaderStr);
InBlock.gifNS_IMETHODCanUnload(nsIComponentManager
*aCompMgr,PRBool*_retval);
InBlock.gif
private:
InBlock.gifnsrefcntmRefCnt;
ExpandedBlockEnd.gif}
;
None.gif
//----------------------------------------------------------------------
None.gif
SampleModule::SampleModule()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifmRefCnt
=0;
ExpandedBlockEnd.gif}

None.gifSampleModule::
~SampleModule()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedBlockEnd.gif}

None.gif
//nsISupportsimplemention
None.gif
NS_IMETHODIMP_(nsrefcnt)SampleModule::AddRef(void)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
++mRefCnt;
InBlock.gif
returnmRefCnt;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMP_(nsrefcnt)SampleModule::Release(
void)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
--mRefCnt;
InBlock.gif
if(mRefCnt==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifmRefCnt
=1;/**//*stabilize这里为什么要设置为1呢?*/
InBlock.gifdelete
this;
InBlock.gif
return0;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnmRefCnt;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleModule::QueryInterface(REFNSIIDaIID,
void**aInstancePtr)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(!aInstancePtr)
InBlock.gif
returnNS_ERROR_NULL_POINTER;
InBlock.gifnsISupports
*foundInterface;
InBlock.gif
if(aIID.Equals(kIModuleIID))
InBlock.giffoundInterface
=(nsIModule*)this;
InBlock.gif
elseif(aIID.Equals(kISupportsIID))
InBlock.giffoundInterface
=(nsISupports*)this;
InBlock.gif
else
InBlock.giffoundInterface
=0;
InBlock.gif
if(foundInterface)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffoundInterface
->AddRef();
InBlock.gif
*aInstancePtr=foundInterface;
InBlock.gif
returnNS_OK;
ExpandedSubBlockEnd.gif}

InBlock.gif
*aInstancePtr=foundInterface;
InBlock.gif
returnNS_NOINTERFACE;
ExpandedBlockEnd.gif}

None.gif
//CreateafactoryobjectforcreatinginstancesofaClass.
None.gif
NS_IMETHODIMPSampleModule::GetClassObject(nsIComponentManager*aCompMgr,constnsCID&aClass,constnsIID&aIID,void**result)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(!kSampleCID.Equals(aClass))
InBlock.gif
returnNS_ERROR_FACTORY_NOT_REGISTERED;
InBlock.gif
if(!result)
InBlock.gif
returnNS_ERROR_INVALID_ARG;
InBlock.gifSampleFactory
*factory=newSampleFactory();
InBlock.gif
if(!factory)
InBlock.gif
returnNS_ERROR_OUT_OF_MEMORY;
InBlock.gifnsresultrv
=factory->QueryInterface(aIID,result);
InBlock.gif
if(NS_FAILED(rv))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
*result=nsnull;
InBlock.gifdeletefactory;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnrv;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleModule::RegisterSelf(nsIComponentManager
*aCompMgr,nsIFile*aPath,constchar*registryLocation,constchar*componentType)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifnsIComponentRegistrar
*compReg=nsnull;
InBlock.gifnsresultrv
=aCompMgr->QueryInterface(kIComponentRegistrarIID,(void**)&compReg);
InBlock.gif
if(NS_FAILED(rv))
InBlock.gif
returnrv;
InBlock.gifrv
=compReg->RegisterFactoryLocation(kSampleCID,"SampleClass",nsnull,aPath,registryLocation,componentType);
InBlock.gifcompReg
->Release();
InBlock.gif
returnrv;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleModule::UnregisterSelf(nsIComponentManager
*aCompMgr,nsIFile*aPath,constchar*registryLocation)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifnsIComponentRegistrar
*compReg=nsnull;
InBlock.gifnsresultrv
=aCompMgr->QueryInterface(kIComponentRegistrarIID,(void**)&compReg);
InBlock.gif
if(NS_FAILED(rv))
InBlock.gif
returnrv;
InBlock.gifrv
=compReg->UnregisterFactoryLocation(kSampleCID,aPath);
InBlock.gifcompReg
->Release();
InBlock.gif
returnrv;
ExpandedBlockEnd.gif}

None.gifNS_IMETHODIMPSampleModule::CanUnload(nsIComponentManager
*aCompMgr,PRBool*okToUnload)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
*okToUnload=PR_FALSE;//wedonotknowhowtounload.
InBlock.gif
returnNS_OK;
ExpandedBlockEnd.gif}

None.gif
extern"C"NS_EXPORTnsresultNSGetModule(nsIComponentManager*servMgr,nsIFile*location,nsIModule**return_cobj)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifnsresultrv
=NS_OK;
InBlock.gif
//Createandinitializethemoduleinstance
InBlock.gif
SampleModule*m=newSampleModule();
InBlock.gif
if(!m)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
returnNS_ERROR_OUT_OF_MEMORY;
ExpandedSubBlockEnd.gif}

InBlock.gif
//IncreaserefcntandstoreawaynsIModuleinterfacetominreturn_cobj
InBlock.gif
rv=m->QueryInterface(kIModuleIID,(void**)return_cobj);
InBlock.gif
if(NS_FAILED(rv))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdeletem;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnrv;
ExpandedBlockEnd.gif}

None.gif
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页