Whats wrong with this code?

I am trying to modify the EventSinkMap of an ActiveX control at runtime. I need a different function to be called when a certain event occurs (ie data received from a serial port).

After delving into MFC I have found several functions that will allow you to get to the EventSinkMap and its entries;

      AFX_EVENTSINKMAP* pSinkMap = (AFX_EVENTSINKMAP *)GetEventSinkMap();
      AFX_EVENTSINKMAP_ENTRY* pEntry;

      pEntry = (AFX_EVENTSINKMAP_ENTRY *)pSinkMap->lpEntries;
      while (pEntry->dispEntry.nPropOffset != -1)
      {
            if (pEntry->dispEntry.lDispID == someID)
      ((*pEntry).dispEntry).pfn = (AFX_PMSG)SomeFunction;
                        ++pEntry;

      }


The problem is I keep getting an access violation at the '.pfn =' statement.
I have also tried the following with the same results;

pEntry->dispEntry.pfn = (AFX_PMSG)SomeFunction;

SomeFunction has been declared as the same type and return value as the one I am trying to replace.

Is it not possible to modify these values at runtime? Or is there a simpiler solution that I am missing.

johndixsonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
abk102299Connect With a Mentor Commented:
I'm afraid your map is allocated in the protected readonly memory - that's why you keep getting an access violation.

CMDTARG.CPP(450 and below):

const AFX_EVENTSINKMAP* CCmdTarget::GetEventSinkMap() const
{
      return &CCmdTarget::eventsinkMap;
}

const AFX_EVENTSINKMAP_ENTRY CCmdTarget::_eventsinkEntries[] =
{
      { NULL, -1, NULL, 0, (AFX_PMSG)NULL, (AFX_PMSG)NULL, (size_t)-1,
        afxDispCustom, (UINT)-1, (UINT)-1 }
      // nothing here
};
0
 
KangaRooCommented:
What are the declarations of pfn and SomeFunction?
0
 
johndixsonAuthor Commented:
pfn is defined in <afxwin.h>

struct AFX_DISPMAP_ENTRY
{
      LPCTSTR lpszName;       // member/property name
      long lDispID;           // DISPID (may be DISPID_UNKNOWN)
      LPCSTR lpszParams;      // member parameter description
      WORD vt;                // return value type / or type of property
      AFX_PMSG pfn;           // normal member On<membercall> or, OnGet<property>
      AFX_PMSG pfnSet;        // special member for OnSet<property>
      size_t nPropOffset;     // property offset
      AFX_DISPMAP_FLAGS flags;// flags (e.g. stock/custom)
};

Here is the original function that I am trying to replace;

void CSomeCtrl::OnIncomingFlagCharPortctl1()
{
      // TODO: Add your control notification handler code here
      AfxMessageBox("Original Function: Incoming Flag Char");
      CString data = m_ctlPort.ReadString(20);
}

Here is the replacement;
void CSomeCtrl::SomeFunction()
{
      // TODO: Add your control notification handler code here
      AfxMessageBox("Second function: Incoming Flag Char");
      CString data = m_ctlPort.ReadString(20);
}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
azamiCommented:
I think the only possible flaw is in the pointer cast: the left part of the assignment is necessarily correct (barring multithreading issues) because we just used it in the if statement.

Have you compiled with maximum warning level?  That may complain about the cast and offer a clue.
0
 
johndixsonAuthor Commented:
No warnings are displayed on any warning level setting.

I guess I should also state that this ActiveX control is actually embedded in another ActiveX control so multithreading could be an issue.
0
 
johndixsonAuthor Commented:
I knew there was a reason behide this madness. I can't beleive MicroSoft would never anticipate somebody wanting to do this. I guess I will take an alternate approach.

Thanks,

John
0
 
abk102299Commented:
Hi John,

But why don't you shift the problem from MS to your code level and make the ONLY map's function that provides necessary control flow switch just inside it's implementation.
0
All Courses

From novice to tech pro — start learning today.