Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implement event handler

Status
Not open for further replies.

mangocinn

Programmer
Jul 26, 2001
66
US
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.
 
Ok... I figured out what needs to go in my main (classA) cpp file. So, that seems to be ok now.

I am still not sure about the purpose of the odl file...? If you know how this file works, can you please tell me about it.

Thanks.
 
The odl is the file used to create the tlb info, describing the COM inteface.



/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Thank you Perfnurt.

Is it necessary for me to create that file in order to implement a message handler?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top