How do I convert a member function pointer to the TIMERPROC type for use with the WINAPI SetTimer? The code snippet below shows how I’m doing it now, but when I compile I get this error:
error C2664: ‘SetTimer’ : cannot convert parameter 4 from ‘void (__stdcall CBuildAndSend::* )(HWND,UINT,UINT_PTR,DWORD)’ to ‘TIMERPROC’
The callback needs to be tied to its originating class instance. If there is a better way to do that, I’m all ears. Thanks.
class CMyClass
{
public:
void (CALLBACK CBuildAndSend::*TimerCbfn)( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
private:
void CALLBACK TimeoutTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
};
CMyClass::CMyClass()
{
...
this->TimerCbfn = &CBuildAndSend::TimeoutTimerProc;
...
::CreateThread(
NULL, // no security attributes
0, // use default initial stack size
reinterpret_cast<LPTHREAD_START_ROUTINE>(BasThreadFn), // function to execute in new thread
this, // thread parameters
0, // use default creation settings
NULL // thread ID is not needed
)
}
void CALLBACK CMyClass::TimeoutTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
...
}
static DWORD MyThreadFn( LPVOID pParam )
{
CMyClass * pMyClass = (CMyClass *)pParam;
...
::SetTimer( NULL, 0, BAS_DEFAULT_TIMEOUT, pMyClass->TimerCbfn ); // <-- Error Here
...
}
Member-function and TIMEPROC are not compatible types.
You need to make the member function
static. Then it will work, assuming parameter-list is same in both, static member function and TIMEPROC.Function pointer as well as member function both are modified. Now it should work.
Now since the callback function became static, it cannot access the non-static members of the class, because you don’t have
thispointer in the function.To access the non-static members, you can do this:
I removed
TimerCbfnfrom my implementation, as it doesn’t really needed. You can passTimerProcdirectly toSetTimeras last argument.