Re: C++ class access from event handler/callback?
kevin_waite <kevin001.waite@gmail.com> wrote:
Hello (newbie here),
I am using Phidgets USB Interface API that sets up 6 or so event
handlers. I really want to have acces to my main control class
within these event handlers, when they run. How does one do
this in C++ --- consider the following:
-------------------------------------------------------------------------------------------------------------------------
the following compiles and works -- but is not that good/pure C++/OPP
pre initialized global pointer to my main control class. . .
int __stdcall MomPC::InputChangeHandler(CPhidgetInterfaceKitHandle
IFK, void
*usrptr, int Index, int State)
{
char txt[128];
sprintf_s( txt, "kit inp %d %d|", Index, State);
gp_xmpc->btn_trace_txt( txt );
return 0;
}
...... setting to handler/callback
CPhidgetInterfaceKit_set_OnInputChange_Handler (ifKit,
InputChangeHandler,
NULL);
If Phidgets API is anything like most APIs supporting callbacks, I would
venture a guess that the last parameter to
CPhidgetInterfaceKit_set_OnInputChange_Handler is passed back to the
handler, unchanged, as usrptr. So, simply pass the 'this' pointer to
your object instance, then inside InputChangeHandler cast usrptr to
MomPC*.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925