
Getting pointer to non-static member function from C callback function
the callback func needs to be a staticfunction...
a kind of 'hacky' approach is to pass your thisptr
as the idEvent in the call to SetTimer, see the CTimer
sample below...
by making the internal _TimerProc callback function
private, your 'forcing' users of the class to start timer
through your exposed Start(...) function, and not by
calling SetTimer(...) themselves, in which case you
cant be sure that theyre passing the thisptr in the idEvent
class CTimer
{
private:
static void CALLBACK _TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD
dwTime )
{
reinterpret_cast<CTimer*>(idEvent)->TimerProc(hwnd,uMsg,idEvent,dwTime);
}
public:
void Start( HWND hWnd,UINT uElapse )
{
SetTimer( hWnd,reinterpret_cast<UINT>(this),uElapse,&CTimer::_TimerProc);
}
void TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
MessageBox( hwnd,"Timetick","tock",MB_OK );
}
Quote:
};
Quote:
> I am using SetTimer function with its TimerProc to call periodically
> non-static member function of a C++ class.
> The problem that I've met here is that TimerProc is C function and there
is
> no mention of 'this' pointer for C - functions
> and I can not do it.
> How one can get pointer to non-static member function of a C++ class from
> callback function. It is kind of pain for
> me for the second day. TimerProc callback function has a fixed pre-defined
> signature:
> TimerProc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
> Looks like I can not pass a reference to C++ class as a parameter.
> Any advices? Thank you.