XPCOM支持的每种语言都必须有自己的组件加载器。
XPCOM组件至少有三层,从里到外是:1)核心XPCOM对象。2)工厂代码 3)模块代码<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
核心XPCOM对象是实现你所需要的功能的对象,其他层是用来支持它,将它插入到XPCOM系统中的。一个单独的库可能有很多个这样的核心对象。
在核心对象层上面的是工厂层,工厂对象提供了XPCOM对象的基本抽象。
模块层在最外面,模块接口提供了另一种抽象—为工厂提供了抽象—并允许有多个工厂对象。从组件库的外部只有唯一的入口点,NSGetModule().这个入口点可以扇出任意工厂,从这些工厂创建出任意XPCOM对象
#include<stdio.h>
#defineMOZILLA_STRICT_API
#include"nsIModule.h"
#include"nsIFactory.h"
#include"nsIComponentManager.h"
#include"nsIComponentRegistrar.h"
staticconstnsIIDkIModuleIID=NS_IMODULE_IID;
staticconstnsIIDkIFactoryIID=NS_IFACTORY_IID;
staticconstnsIIDkISupportsIID=NS_ISUPPORTS_IID;
staticconstnsIIDkIComponentRegistrarIID=NS_ICOMPONENTREGISTRAR_IID;
#defineSAMPLE_CID/
{0x777f7150,0x4a2b,0x4301,/
{0xad,0x10,0x5e,0xab,0x25,0xb3,0x22,0xaa}}
staticconstnsCIDkSampleCID=SAMPLE_CID;
classSample:publicnsISupports
{
private:
nsrefcntmRefCnt;
public:
Sample();
virtual~Sample();
NS_IMETHODQueryInterface(constnsIID&aIID,void**aResult);
NS_IMETHOD_(nsrefcnt)AddRef(void);
NS_IMETHOD_(nsrefcnt)Release(void);
};
Sample::Sample()
{
:mRefCnt(0);
}
Sample::~Sample()
{
}
NS_IMETHODIMPSample::QueryInterface(constnsIID&aIID,void**aResult)
{
if(aResult==NULL){
returnNS_ERROR_NULL_POINTER;
}
*aResult=NULL;
if(aIID.Equals(kISupportsIID)){
*aResult=(void*)this;
}
if(aResult!=NULL){
returnNS_ERROR_NO_INTERFACE;
}
AddRef();
returnNS_OK;
}
NS_IMETHODIMP_(nsrefcnt)Sample::AddRef()
{
return++mRefCnt;
}
NS_IMETHODIMP_(nsrefcnt)Sample::Release()
{
if(--mRefCnt==0){
deletethis;
return0;
}
returnmRefCnt;
}
//factoryimplementationclassforcomponent
classSampleFactory:publicnsIFactory
{
private:
nsrefcntmRefCnt;
public:
SampleFactory();
virtual~SampleFactory();
NS_IMETHODQueryInterface(constnsIID&aIID,void**aResult);
NS_IMETHOD_(nsrefcnt)AddRef(void);
NS_IMETHOD_(nsrefcnt)Release(void);
NS_IMETHODCreateInstance(nsISupports*aOuter,constnsIID&iid,void**result);
NS_IMETHODLockFactory(PRBoollock);
};
SampleFactory::SampleFactory()
{
mRefCnt=0;
}
SampleFactory::~SampleFactory()
{
}
NS_IMETHODIMPSampleFactory::QueryInterface(constnsIID&aIID,void**aResult)
{
if(aResult==NULL){
returnNS_ERROR_NULL_POINTER;
}
*aResult=NULL;
if(aIID.Equals(kISupportsIID)){
*aResult=(void*)this;
}
else
if(aIID.Equals(kIFactoryIID)){
*aResult=(void*)this;
}
if(aResult!=NULL){
returnNS_ERROR_NO_INTERFACE;
}
AddRef();
returnNS_OK;
}
NS_IMETHODIMP_(nsrefcnt)SampleFactory::AddRef()
{
return++mRefCnt;
}
NS_IMETHODIMP_(nsrefcnt)SampleFactory::Release()
{
if(--mRefCnt==0){
deletethis;
return0;
}
returnmRefCnt;
}
NS_IMETHODIMPSampleFactory::CreateInstance(nsISupports*aOuter,constnsIID&iid,void**result)
{
if(!result)
returnNS_ERROR_INVALID_ARG;
Sample*sample=newSample();
if(!sample)
returnNS_ERROR_OUT_OF_MEMORY;
nsresultrv=sample->QueryInterface(iid,result);
if(NS_FAILED(rv))
{
*result=nsnull;
deletesample;
}
returnrv;
}
NS_IMETHODIMPSampleFactory::LockFactory(PRBoollock)
{
returnNS_ERROR_NOT_IMPLEMENTED;
}
//Moduleimplementation
classSampleModule:publicnsIModule
{
public:
SampleModule();
virtual~SampleModule();
//nsISupportsmethods:
NS_IMETHODQueryInterface(constnsIID&uuid,void**result);
NS_IMETHOD_(nsrefcnt)AddRef(void);
NS_IMETHOD_(nsrefcnt)Release(void);
//nsIModulemethods:
NS_IMETHODGetClassObject(nsIComponentManager*aCompMgr,constnsCID&aClass,constnsIID&aIID,void**aResult);
NS_IMETHODRegisterSelf(nsIComponentManager*aCompMgr,nsIFile*aLocation,constchar*aLoaderStr,constchar*aType);
NS_IMETHODUnregisterSelf(nsIComponentManager*aCompMgr,nsIFile*aLocation,constchar*aLoaderStr);
NS_IMETHODCanUnload(nsIComponentManager*aCompMgr,PRBool*_retval);
private:
nsrefcntmRefCnt;
};
//----------------------------------------------------------------------
SampleModule::SampleModule()
{
mRefCnt=0;
}
SampleModule::~SampleModule()
{
}
//nsISupportsimplemention
NS_IMETHODIMP_(nsrefcnt)SampleModule::AddRef(void)
{
++mRefCnt;
returnmRefCnt;
}
NS_IMETHODIMP_(nsrefcnt)SampleModule::Release(void)
{
--mRefCnt;
if(mRefCnt==0)
{
mRefCnt=1;/**//*stabilize这里为什么要设置为1呢?*/
deletethis;
return0;
}
returnmRefCnt;
}
NS_IMETHODIMPSampleModule::QueryInterface(REFNSIIDaIID,void**aInstancePtr)
{
if(!aInstancePtr)
returnNS_ERROR_NULL_POINTER;
nsISupports*foundInterface;
if(aIID.Equals(kIModuleIID))
foundInterface=(nsIModule*)this;
elseif(aIID.Equals(kISupportsIID))
foundInterface=(nsISupports*)this;
else
foundInterface=0;
if(foundInterface)
{
foundInterface->AddRef();
*aInstancePtr=foundInterface;
returnNS_OK;
}
*aInstancePtr=foundInterface;
returnNS_NOINTERFACE;
}
//CreateafactoryobjectforcreatinginstancesofaClass.
NS_IMETHODIMPSampleModule::GetClassObject(nsIComponentManager*aCompMgr,constnsCID&aClass,constnsIID&aIID,void**result)
{
if(!kSampleCID.Equals(aClass))
returnNS_ERROR_FACTORY_NOT_REGISTERED;
if(!result)
returnNS_ERROR_INVALID_ARG;
SampleFactory*factory=newSampleFactory();
if(!factory)
returnNS_ERROR_OUT_OF_MEMORY;
nsresultrv=factory->QueryInterface(aIID,result);
if(NS_FAILED(rv))
{
*result=nsnull;
deletefactory;
}
returnrv;
}
NS_IMETHODIMPSampleModule::RegisterSelf(nsIComponentManager*aCompMgr,nsIFile*aPath,constchar*registryLocation,constchar*componentType)
{
nsIComponentRegistrar*compReg=nsnull;
nsresultrv=aCompMgr->QueryInterface(kIComponentRegistrarIID,(void**)&compReg);
if(NS_FAILED(rv))
returnrv;
rv=compReg->RegisterFactoryLocation(kSampleCID,"SampleClass",nsnull,aPath,registryLocation,componentType);
compReg->Release();
returnrv;
}
NS_IMETHODIMPSampleModule::UnregisterSelf(nsIComponentManager*aCompMgr,nsIFile*aPath,constchar*registryLocation)
{
nsIComponentRegistrar*compReg=nsnull;
nsresultrv=aCompMgr->QueryInterface(kIComponentRegistrarIID,(void**)&compReg);
if(NS_FAILED(rv))
returnrv;
rv=compReg->UnregisterFactoryLocation(kSampleCID,aPath);
compReg->Release();
returnrv;
}
NS_IMETHODIMPSampleModule::CanUnload(nsIComponentManager*aCompMgr,PRBool*okToUnload)
{
*okToUnload=PR_FALSE;//wedonotknowhowtounload.
returnNS_OK;
}
extern"C"NS_EXPORTnsresultNSGetModule(nsIComponentManager*servMgr,nsIFile*location,nsIModule**return_cobj)
{
nsresultrv=NS_OK;
//Createandinitializethemoduleinstance
SampleModule*m=newSampleModule();
if(!m)
{
returnNS_ERROR_OUT_OF_MEMORY;
}
//IncreaserefcntandstoreawaynsIModuleinterfacetominreturn_cobj
rv=m->QueryInterface(kIModuleIID,(void**)return_cobj);
if(NS_FAILED(rv))
{
deletem;
}
returnrv;
}
分享到:
相关推荐
XPCOM开发过程笔记,以及基于Microb SDK下的开发介绍,用着的赶紧下吧
该工具是用于xpcom组件开发的vc2010项目向导。 您可以使用此向导创建xpcom组件。 工具的羽毛:1.所有xpcom组件开发环境均已创建(适用于vc2010); 2.支持简单的UI; 3.支持壁虎17/18/19
XPCOM组件的研究与实现,万定生,周沫,随着面向对象技术的不断进步,组件这种技术逐渐产生并且不断发展。本文分析了XPCOM组件的构成原理,并且与微软的COM技术进行比较;�
这是一本有关Gecko的书,Gecko是所有Mozilla品牌的软件及其衍生产品中使用的开源Web浏览器布局引擎,以及有关为基于Gecko的应用程序创建XPCOM组件的书。
用于XPCOM组件开发,包含appcomps,embedstring,nspr,string,pref,xpcom,xpconnect等类库。
清晰给出繁杂配置和步骤, c++开发xpcom组件的完整例子, vs2005 环境配置 ,xpconnect调用xpcom组件, xul 中使用javascript 内有源码,配置图示
mozilla xpcom组件的开发。包含一些怎么开发组件的过程和例子。
xpcom实例大家分享下供linux学习
在xulrunner下面,利用xpcom可是使得我们的ria开发更具有传统客户端开发所不具备的功能以及扩展性和模块化。
火狐相关插件的开发 This is a book about XPCOM. The book is written in the form of a tutorial about creating XPCOM components, but it covers all major aspects, concepts, and terminology of the XPCOM ...
mozilla的xulrunner引擎作为最有竞争力的ria开发技术,不仅把网络开发与桌面开发结合在一起。而且,突破了语言的限制。利用xpcom技术可以实现javascript对底层的访问,这样即做到了使用c++功能上的强大,而且还可以...
xpcom base,vs 环境 流程 环境 流程
破解钥匙下载 万能XPcom.DLL 破解钥匙下载 万能XPcom.DLL
介绍用JS创建xpcom
creat xpcom componet
gecko-sdk-i586-pc-msvc-1.7,用于XPCOM组件开发
techniques introduced in the first tutorial, Bridging XPCOM/Bonobo -- techniques. It walks the reader through the building and testing of a static bridge from the XPCOM component system to Bonobo. ...
例子代码 博文链接:https://fuliang.iteye.com/blog/222158
火狐3.9,xpcom.dll源文件。可以javascript脚本利用XPConnect技术来使用XPCOM。包内还有: XPConnect来调用XPCOM函数.txt Creating+XPCOM+Components.pdf
本人近两年整理的xpcom资料合集,包括接口资料以及相应教学资料