您的位置:首页 > 其它

bug in MFC Ocx BEGIN_DISPATCH_MAP

2008-04-21 08:31 393 查看
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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: