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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

abk102299Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.