Getting pointer to non-static member function from C callback function 
Author Message
 Getting pointer to non-static member function from C callback function

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.



Tue, 21 Oct 2003 04:24:53 GMT  
 Getting pointer to non-static member function from C callback function
use the map to map a timer id to a this pointer.  The map should be a static
global as well.  once inside the timer you can get the this pointer back
based on the timer id that you are in and then call your member functions.

header file
global >  typedef map<UINT,MyClass*> MAP_ATIMER;

implementation file
global>  static MAP_ATIMER aTimer;

in the call back
MyClass* someparam = aTimer[idEvent];

start the timer and set the map.
  uResult = SetTimer(NULL, 0, (nInterval * 1000), (TIMERPROC) DownloadTags);
  aTimer[uResult] = this;

regs.
Steven Szelei


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.



Tue, 21 Oct 2003 04:43:45 GMT  
 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.



Tue, 21 Oct 2003 15:32:57 GMT  
 Getting pointer to non-static member function from C callback function
Thank you Steven for writing. When I am placing definition of aTimer static
variable into
implementation:

HEADER
=======

#include <map>

class ATL_NO_VTABLE CSomeName
{

Quote:
}

using namespace std;
typedef map<UINT,CSomeName*> MAP_ATIMER;

IMPLEMENTATION FILE
====================

static MAP_ATIMER aTimer;  //after includes before function implementations

I am driving into the error:

 error LNK2001: unresolved external symbol _main

Is it possible this is because of ATL specifics? Sorry for not pure C\C++
question but I am trying
to work with SetTimer when it is inside ATL DLL server and does not look
that MSDN have a thing about it.



Tue, 21 Oct 2003 22:37:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Using Non-Static Callback Functions as member Functions VC5.0

2. CALLBACK and non-static member function problem!

3. Using Non-Static Callbacks as member functions VC 5.0

4. Pointers to non-static vs static functions

5. One Problem in Calling Static function from Non static function

6. Accessing non-static functions from a static function

7. static function access member variable and member function

8. illegal call of non-static member function

9. using non-static member functions using delegates

10. illegal call of non-static member function

11. illegal call of non-static member function *HELP*

12. iilegal call of non-static member function

 

 
Powered by phpBB® Forum Software