I want to implement an event handler of a third party SDK. All I want to do is call a function in another class when the event occurs. I do not want to display a dialog or anything of that sort.
I am new to C++ and I am confused... it was quite easy to implement an event handler in VB.
I am trying to accomplish this using the CCmdTarget, the class wizard and the message map feautures. Is this the best route to go?
How will my main class know about the class that is handling the event?
ClassA is my main class. (I am not sure what to add to the files for this class)
ClassB is the class that handles the event.
ClassB.h
class ClassB : public CmdTarget
{
public:
//{{AFX_VIRTUAL(classB)
public:
virtual void OnFinalRelease();
//}}AFX_WIRTUAL
virtual ~ClassB;
protected:
//{{AFX_MSG(classB)
// NOTE - the classWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
//{{AFX_DISPATH(classB)
afx_msg void TheEvent(IThirdPartyInterface* p1, Long* p2);
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
private:
DECLARE_DYNCREATE(classB)
classB();
}
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert more
#end if
ClassB.cpp
#define DISPID_THEEVENT 1
IMPLEMENT_DYNCREATE(classB, CCmdTarget)
classB::ClassB()
{
EnableAutomation();
}
/* virtual */ classB::~classB()
{
}
void
classB::OnFinalRelease()
{
CCmdTarget::OnFinalRelease();
}
BEGIN_MESSAGE_MAP(classB, CCmdTarget)
//{{AFX_MSG_MAP(classB)
// NOTE - ClassWizard will add/remove macros.
//{{AFX_MSG_MAP
END_MESSAGE_MAP()
BEING_DISPATCH_MAP(classB, CCmdTarget)
//{{AFX_DISPATCH_MAP(classB)
DISP_FUNCTION_ID(classB, "TheEvent", DISPID_TheEvent, TheEvent, VT_EMPTY, VTS_PI4 VTS_PUNKNOWN)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP
BEGIN_INTERFACE_MAP(classB, CCmdTarget)
INTERFACE_PART(classB, DIID__IThirtyPartyEvents, Dispatch)
END_INTERFACE_MAP()
void
classB::TheEvent(IThirdPartyInterface* p1, Long* p2)
{
//handle the event how I want
}
Do I need to have a .odl file. Is this just like an idl file? Why is it necessary? What do I need to add to my main class ClassA? In VB I would just do a implements on the interface to intercept it.
Please please help.
I am new to C++ and I am confused... it was quite easy to implement an event handler in VB.
I am trying to accomplish this using the CCmdTarget, the class wizard and the message map feautures. Is this the best route to go?
How will my main class know about the class that is handling the event?
ClassA is my main class. (I am not sure what to add to the files for this class)
ClassB is the class that handles the event.
ClassB.h
class ClassB : public CmdTarget
{
public:
//{{AFX_VIRTUAL(classB)
public:
virtual void OnFinalRelease();
//}}AFX_WIRTUAL
virtual ~ClassB;
protected:
//{{AFX_MSG(classB)
// NOTE - the classWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
//{{AFX_DISPATH(classB)
afx_msg void TheEvent(IThirdPartyInterface* p1, Long* p2);
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
private:
DECLARE_DYNCREATE(classB)
classB();
}
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert more
#end if
ClassB.cpp
#define DISPID_THEEVENT 1
IMPLEMENT_DYNCREATE(classB, CCmdTarget)
classB::ClassB()
{
EnableAutomation();
}
/* virtual */ classB::~classB()
{
}
void
classB::OnFinalRelease()
{
CCmdTarget::OnFinalRelease();
}
BEGIN_MESSAGE_MAP(classB, CCmdTarget)
//{{AFX_MSG_MAP(classB)
// NOTE - ClassWizard will add/remove macros.
//{{AFX_MSG_MAP
END_MESSAGE_MAP()
BEING_DISPATCH_MAP(classB, CCmdTarget)
//{{AFX_DISPATCH_MAP(classB)
DISP_FUNCTION_ID(classB, "TheEvent", DISPID_TheEvent, TheEvent, VT_EMPTY, VTS_PI4 VTS_PUNKNOWN)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP
BEGIN_INTERFACE_MAP(classB, CCmdTarget)
INTERFACE_PART(classB, DIID__IThirtyPartyEvents, Dispatch)
END_INTERFACE_MAP()
void
classB::TheEvent(IThirdPartyInterface* p1, Long* p2)
{
//handle the event how I want
}
Do I need to have a .odl file. Is this just like an idl file? Why is it necessary? What do I need to add to my main class ClassA? In VB I would just do a implements on the interface to intercept it.
Please please help.