一个UNO服务/组件的例子

//-----------------------------------------------------------------------
// 定义: 服务名,实现名
//-----------------------------------------------------------------------

#define COMP_SERVICE_NAME  "com.sun.star.system.SimpleSystemMail" 
#define COMP_IMPL_NAME     "com.sun.star.system.SimpleSystemMail"
#define COMP_REGKEY_NAME   "/com.sun.star.system.SimpleSystemMail/UNO/SERVICES/com.sun.star.system.SimpleSystemMail"

//-----------------------------------------------------------------------
// 使用ServiceManager作为参数来实例化实现这个服务的对象
//-----------------------------------------------------------------------

namespace
{
    Reference< XInterface > SAL_CALL createInstance( const Reference< XMultiServiceFactory >& )
    {       
        return Reference< XInterface >( static_cast< XSimpleMailClientSupplier* >( new CSmplMailSuppl( ) ) );
    }
}

//-------------------------------------------------------------------------------
// the 3 important functions which will be exported    UNO环境操作需要的东西
//-------------------------------------------------------------------------------

extern "C"
{

//----------------------------------------------------------------------
// component_getImplementationEnvironment

// 这个函数指定了组件的环境,这里指定了C++组件环境,相对的还有其他组件环境
//----------------------------------------------------------------------

void SAL_CALL component_getImplementationEnvironment(
    const sal_Char ** ppEnvTypeName, uno_Environment ** )
{
    *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
}

//-----------------------------------------------------------------------
// 用于注册组件--写这个组件的信息到注册表
//-----------------------------------------------------------------------

sal_Bool SAL_CALL component_writeInfo( void* /*pServiceManager*/, void* pRegistryKey )
{
    sal_Bool bRetVal = sal_True;

    if ( pRegistryKey )
    {
        try
        {
            Reference< XRegistryKey > pXNewKey( static_cast< XRegistryKey* >( pRegistryKey ) );                           
            pXNewKey->createKey(
                OUString::createFromAscii( COMP_REGKEY_NAME ) );
        }
        catch( InvalidRegistryException& )
        {           
            OSL_ENSURE(sal_False, "InvalidRegistryException caught");           
            bRetVal = sal_False;
        }
    }

    return bRetVal;
}

//----------------------------------------------------------------------
// component_getFactory
// returns a factory to create XFilePicker-Services

// 查询到组件实现时就返回初始化功能的工厂
//----------------------------------------------------------------------

void* SAL_CALL component_getFactory( const sal_Char* pImplName, uno_Interface* pSrvManager, uno_Interface* /*pRegistryKey*/ )
{
    void* pRet = 0;

    if ( pSrvManager && ( 0 == rtl_str_compare( pImplName, COMP_IMPL_NAME ) ) )
    {
        Sequence< OUString > aSNS( 1 );
        aSNS.getArray( )[0] = OUString::createFromAscii( COMP_SERVICE_NAME );       
               
        Reference< XSingleServiceFactory > xFactory ( createOneInstanceFactory(
            reinterpret_cast< XMultiServiceFactory* > ( pSrvManager ),
            OUString::createFromAscii( pImplName ),
            createInstance, //初始化功能的工厂,可以创建组件的实例
            aSNS ) );
        if ( xFactory.is() )
        {
            xFactory->acquire();
            pRet = xFactory.get();
        }           
    }

    return pRet;
}

} // extern "C"

posted @ 2011-04-02 14:19  MayFirst  阅读(350)  评论(0编辑  收藏  举报