Aggregation explained

330 篇文章 ¥19.90 ¥99.00
本文介绍了COM组件中的聚合技术,通过一个Calculator与ComplexCalculator的例子,详细解释了如何在COM中实现聚合关系,包括下载源代码、创建项目、添加方法、修改IDL文件和头文件等步骤,并提供了相关资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sample image

Introduction

Let's have a look at the reuse technique "Aggregation" and how we can implement this in COM.

Definition

In this reuse mechanism, The Outer COM directly exposes the Inner COM's interfaces to the client. I will try to explain the steps involved to attain Aggregation relation between the InnerCom and OuterCom in the simplest way possible, so that beginners in COM can understand and workout.

Let's work on an example to establish aggregation relation between the Calculator Component (InnerCom), which already is available with us and ComplexCalculator, which we are going to build now.

Download the Calculator.dll from the source and register it.

Steps:

  • Create a new Project of type Atl COM, name it ComplexCalculator and select the type as "dll".
  • Insert a new Atl Object, select the Simple Object option and Name it ComplexMath.
  • Add a new method to the IComplexMath interface.

Method Name: Divide

Parameters: [in] int x, [in] int y, [out, retval] int* z

Add the following code:

STDMETHODIMP CComplexMath :: Divide (int x, int y, int *z)
{
    *z= x/y;

    return S_OK;
}

Do the following changes to CalculatoeEx.idl:

Copy the object section and the interface section of the Calculator.idl to ComplexCalculator.idl

[
    object,
    uuid(1E48B0F0-010D-4658-A0B6-70300FDA56F1),
    dual,
    helpstring("ISimpleMath Interface"),
    pointer_default(unique)

]

    interface ISimpleMath : IDispatch
    {
        [id(1), helpstring("method Add")] 
            HRESULT Add([in] int x, [in] int y,    
            [out, retval] int* z );
    };

In the coclass part of ComplexCalculator.idl, add the interface name that we want to aggregate.

Note: Changes that need to be done are indicated in bolds.

coclass ComplexMath
{
    [default] interface IComplexMath;
    interface ISimpleMath;
};

Do the following changes to the ComplexCalculator.h:

Copy the CLSID of the SimpleMath from Calculator_i.c to ComplexCalculator.h

const CLSID CLSID_SimpleMath = {0xF58218E0,0x645A,0x473D,
                               {0x84,0x4B,0x30,0x78,0x65,
                                0x03,0x8D,0x9E}};

Do the following change in the COM MAP.

Note: Changes that need to be done are indicated in bolds.

BEGIN_COM_MAP(CComplexMath)
    COM_INTERFACE_ENTRY (IComplexMath)
    COM_INTERFACE_ENTRY_AGGREGATE (IID_ISimpleMath, ptrUnk)
    COM_INTERFACE_ENTRY (IDispatch)
END_COM_MAP () 

Add the following macro, finalconstruct and release methods

DECLARE_GET_CONTROLLING_UNKNOWN()
IUnknown* ptrUnk;

HRESULT FinalConstruct()
{
    return CoCreateInstance(CLSID_SimpleMath,
            GetControllingUnknown(),CLSCTX_ALL, 
            IID_IUnknown, (void**)&ptrUnk);
}

void FinalRelease()
{
    ptrUnk->Release();
}

Search for "Aggregation" in the source code to find code that is inserted to build the relationship between the "Calculator" and "ComplexCalculator".

Note: Register the Calculator.dll and ComplexCalculator.dl before using the demo project. 

http://www.codeproject.com/com/AggregationSample.asp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值