Click to See Complete Forum and Search --> : How to fire an OCX event from a non-member function


Russ
April 23rd, 1999, 02:54 PM
Hi all you OCX experts,

I'm trying to figure out how to fire an event defined within an OCX from
a function that has been defines as a friend function. The OCX event I
declared works fine when called from any of the class member functions
within the OCX. If I call the event handler from my friend function I
get a run time error.

I'm still a C++ newbie so if I'm missing something obvious let me know.
I implimented this idea by creating a pointer to the class type of the
OCX and then loaded it with the address of the OCX by using 'this'.

Is there an easier way to call class members from non-member functions?
I've been looking for an example of this but haven't found one. Here's
what I did:


In the class def .h file I have an event created using the MFC
ClassWizard. At the bottom I added my friend function definition:

class CXCtrl : public COleControl
{
DECLARE_DYNCREATE(CXCtrl)
.
.
//Event maps
//{{AFX_EVENT(CXCtrl)
void FireOnComm(long Istate)
{FireEvent(eventidOnComm, EVENT_PARAM(VTS_I4), Istate);}
//}}AFX_EVENT
DECLARE_EVENT_MAP()
.
.

friend void CallOnCommEvent(long st);
}



In the .cpp source file I declare global pointer for a CXCtrl control:

CXCtrl * this_OCX;

In the constructor function I load the pointer with the address:

CXCtrl::CXCtrl()

{
.
this_OCX = this;
.
}




the non-member calling code:

void CallOnCommEvent(long st)
{
this_OCX->FireOnComm(st);
}




The code compiles fine no errors or warnings. If I call the
CallOnCommEvent() I get a run time error message. I traced through the
debugger and found that the error occurs in the event handling function:


void COleDispatchDriver::InvokeHelperV(DISPID dwDispID, WORD wFlags,
VARTYPE vtRet, void* pvRet, const BYTE* pbParamInfo, va_list argList)




This function is located in OLEDISP2.CPP. I compared the pass parameter
values when this function is called from class member functions and then
from my non-member function and can see no difference in values. Is
there something I have to do before calling this_OCX->FireOnComm(st) to
set things up before the call to 'fool it into thinking' its from a
class memeber?

Any and all help is greatly appreciated,

Russ