当前位置: 技术问答>linux和unix
★★ Qt 中有跨平台的 COM 组件开发库 ★★
来源: 互联网 发布时间:2015-06-15
本文导语: 看 Qt 源代码时发现的, 原来 Qt 中 Plugin 使用的都是类 COM 接口... | 如何在一个 COM Server 中定义多个 COM ("Multiple Components in One Component Server")? 答:其实那篇文章已经讲得很清楚...
看 Qt 源代码时发现的,
原来 Qt 中 Plugin 使用的都是类 COM 接口...
原来 Qt 中 Plugin 使用的都是类 COM 接口...
|
如何在一个 COM Server 中定义多个 COM ("Multiple Components in One Component Server")?
答:其实那篇文章已经讲得很清楚了,为了要在一个COM Server 中定义多个 COM,
关键在于定义一个自己的组件工厂类(ComponentFactory),即你需要从QComponentFactoryInterface派生一个组件工厂类,并重新实现createInstance()
接口函数。具体过程如下:
(1)首先定义那几个实现不同功能的COM对象,记得为每个COM对象生成一个UUID,例如
class Component1...
{
public:
...
static QUuid CID;
...
};
QUuid Component1::CID = QUuid( 0xDD19964B, 0xA2C8, 0x42AE,
0xAA, 0xF9, 0x8A, 0xDC, 0x50, 0x9B, 0xCA, 0x03 );
class Component2...
{
public:
...
static QUuid CID;
...
};
QUuid Component2::CID = QUuid( 0xDD19964B, 0xA2C8, 0x42AE,
0xAA, 0xF9, 0x8A, 0xDC, 0x50, 0x9B, 0xCA, 0x04 );
(2)定义一个组件工厂类,记得为该工厂类生成一个UUID,例如
class ComponentFactoryName: QComponentFactoryInterface
{
public:
//存放对象实例的指针
Component1 *comp1;
Component2 *comp2;
createInstance( const QUuid &cid, const QUuid &iid,
QUnknownInterface** iface,
QUnknownInterface *outer );
QRESULT queryInterface( const QUuid &uuid,
QUnknownInterface **iface )
};
重新实现createInstance()和queryInterface()接口函数
QRESULT ComponentFactoryName::createInstance( const QUuid &cid, const QUuid &iid,
QUnknownInterface** iface,
QUnknownInterface *outer )
{
QRESULT res = QE_NOCOMPONENT;
if ( cid == Component1::CID ) {
comp1 = new Component1;
res = comp1->queryInterface( iid, iface );
if ( res != QS_OK )
delete comp1;
} else if ( cid == Component2::CID ) {
Component2 *comp2= new Component2;
res = comp2->queryInterface( iid, iface );
if ( res != QS_OK )
delete comp2;
}
return res;
}
QRESULT ComponentFactoryName:: queryInterface( const QUuid &uuid,
QUnknownInterface **iface )
{
//先检查第一个组件有无要查询的Quuid接口,如无则检查第二组件,依次类推
QRESULT res = QE_NOCOMPONENT;
QunknownInterface * iface;
res = comp1->queryInterface(uuid, &iface );
if (res == QE_NOCOMPONENT)
{
res = comp2->queryInterface( uuid , &face);
}
return res;
}
(3)将上述定义的组件工厂类视作组件服务器的一个组件(component),
将其导出,导出函数定义如下
Q_EXPORT_COMPONENT()
{
ComponentFactoryName *comp = new ComponentFactoryName;
QUnknownInterface *iface;
//实例化所有的组件
comp->createInstance(cid1,iid1, &iface );
comp->createInstance(cid2,iid2, &iface );
//返回工厂类的缺省组件的接口指针(例如将组件1视为缺省组件)
comp->queryInterface( IID_COM1, &iface );
return iface;
}
答:其实那篇文章已经讲得很清楚了,为了要在一个COM Server 中定义多个 COM,
关键在于定义一个自己的组件工厂类(ComponentFactory),即你需要从QComponentFactoryInterface派生一个组件工厂类,并重新实现createInstance()
接口函数。具体过程如下:
(1)首先定义那几个实现不同功能的COM对象,记得为每个COM对象生成一个UUID,例如
class Component1...
{
public:
...
static QUuid CID;
...
};
QUuid Component1::CID = QUuid( 0xDD19964B, 0xA2C8, 0x42AE,
0xAA, 0xF9, 0x8A, 0xDC, 0x50, 0x9B, 0xCA, 0x03 );
class Component2...
{
public:
...
static QUuid CID;
...
};
QUuid Component2::CID = QUuid( 0xDD19964B, 0xA2C8, 0x42AE,
0xAA, 0xF9, 0x8A, 0xDC, 0x50, 0x9B, 0xCA, 0x04 );
(2)定义一个组件工厂类,记得为该工厂类生成一个UUID,例如
class ComponentFactoryName: QComponentFactoryInterface
{
public:
//存放对象实例的指针
Component1 *comp1;
Component2 *comp2;
createInstance( const QUuid &cid, const QUuid &iid,
QUnknownInterface** iface,
QUnknownInterface *outer );
QRESULT queryInterface( const QUuid &uuid,
QUnknownInterface **iface )
};
重新实现createInstance()和queryInterface()接口函数
QRESULT ComponentFactoryName::createInstance( const QUuid &cid, const QUuid &iid,
QUnknownInterface** iface,
QUnknownInterface *outer )
{
QRESULT res = QE_NOCOMPONENT;
if ( cid == Component1::CID ) {
comp1 = new Component1;
res = comp1->queryInterface( iid, iface );
if ( res != QS_OK )
delete comp1;
} else if ( cid == Component2::CID ) {
Component2 *comp2= new Component2;
res = comp2->queryInterface( iid, iface );
if ( res != QS_OK )
delete comp2;
}
return res;
}
QRESULT ComponentFactoryName:: queryInterface( const QUuid &uuid,
QUnknownInterface **iface )
{
//先检查第一个组件有无要查询的Quuid接口,如无则检查第二组件,依次类推
QRESULT res = QE_NOCOMPONENT;
QunknownInterface * iface;
res = comp1->queryInterface(uuid, &iface );
if (res == QE_NOCOMPONENT)
{
res = comp2->queryInterface( uuid , &face);
}
return res;
}
(3)将上述定义的组件工厂类视作组件服务器的一个组件(component),
将其导出,导出函数定义如下
Q_EXPORT_COMPONENT()
{
ComponentFactoryName *comp = new ComponentFactoryName;
QUnknownInterface *iface;
//实例化所有的组件
comp->createInstance(cid1,iid1, &iface );
comp->createInstance(cid2,iid2, &iface );
//返回工厂类的缺省组件的接口指针(例如将组件1视为缺省组件)
comp->queryInterface( IID_COM1, &iface );
return iface;
}