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
}
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
}