bug in MFC Ocx BEGIN_DISPATCH_MAP

An wrong BEGIN_DISPATCH_MAP
dispidOnEvent1 = 1,dispidFunction1 = 1
OnEvent1 is ocx event, Function1 is ocx function


BEGIN_DISPATCH_MAP(CMyOcx2Ctrl, COleControl)
    DISP_FUNCTION_ID(CMyOcx2Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
    DISP_FUNCTION_ID(CMyOcx2Ctrl, "OnEvent1", dispidOnEvent1, OnEvent1, VT_EMPTY, VTS_NONE)
    DISP_FUNCTION_ID(CMyOcx2Ctrl, "Function1", dispidFunction1, Function1, VT_EMPTY, VTS_I4)
END_DISPATCH_MAP()

If you call  Function1,(dispId = 1), MFC will search  dispatch map and find entry id = 1,
thus return entry  "OnEvent1", this is error, you have no way to invoke Function1.
Every function interface should place before event interface in BEGIN_DISPATCH_MAP

below is ok.

BEGIN_DISPATCH_MAP(CMyOcx2Ctrl, COleControl)
    DISP_FUNCTION_ID(CMyOcx2Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
    DISP_FUNCTION_ID(CMyOcx2Ctrl, "Function1", dispidFunction1, Function1, VT_EMPTY, VTS_I4)
    DISP_FUNCTION_ID(CMyOcx2Ctrl, "OnEvent1", dispidOnEvent1, OnEvent1, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


-------------------------------------------------
reference:
-------------------------------------------------

COleDispatchImpl::Invoke()
{
    ...
    const AFX_DISPMAP_ENTRY* pEntry = pThis->GetDispEntry(dispid);
    ...
}

const AFX_DISPMAP_ENTRY* PASCAL CCmdTarget::GetDispEntry(MEMBERID memid)
{
    const AFX_DISPMAP* pDerivMap = GetDispatchMap();
    const AFX_DISPMAP* pDispMap;
    const AFX_DISPMAP_ENTRY* pEntry;
    .....

    pDispMap = pDerivMap;
#ifdef _AFXDLL
    for (;;)
#else
    while (pDispMap != NULL)
#endif
    {
        // find AFX_DISPMAP_ENTRY where (pEntry->lDispID == memid)
        pEntry = pDispMap->lpEntries;
        while (pEntry->nPropOffset != -1)
        {
            if (pEntry->lDispID == memid)
                return pEntry;

            ++pEntry;
        }
        // check base class
#ifdef _AFXDLL
        if (pDispMap->pfnGetBaseMap == NULL)
            break;
        pDispMap = (*pDispMap->pfnGetBaseMap)();
#else
        pDispMap = pDispMap->pBaseMap;
#endif
    }
}

const AFX_DISPMAP* CCmdTarget::GetDispatchMap() const
{
    return &CCmdTarget::dispatchMap; //dispatchMap point to the struct defined by the BEGIN_DISPATCH_MAP
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值