Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing User Data with SetTimer

I am calling SetTimer in a function of a Class.

SetTimer(NULL, 0, 10000, (TIMERPROC) TimerCallBack);  

Where TimerCallBack is:

static VOID CALLBACK TimerCallBack(HWND, UINT, UINT, DWORD)

Now my need is to call one of the method of class which initiated timer, since TimerCallBack is static it has no access to the class object anymore.

I cant find any way to pass object pointer along with the SetTimer so that I can receive it back on Callback function.

Is there any other way to achieve this, if its not supported using SetTimer then which other way I can implement this.

like image 455
GJ. Avatar asked Jan 07 '11 11:01

GJ.


1 Answers

You don't need a map. Use the idEvent parameter. Microsoft gave it the UINT_PTR type so it fits a pointer, even in 64 bits:

SetTimer(hwnd, (UINT_PTR)bar, 1000, MyTimerProc);

void CALLBACK MyTimerProc(HWND, UINT, UINT_PTR idEvent, DWORD)
{
    Bar* bar = (Bar*)idEvent;
}

Note that you need to use an actual HWND. If the HWND is null, Windows will generate the idEvent internally.

like image 91
Bruno Martinez Avatar answered Sep 20 '22 09:09

Bruno Martinez