Callback to member function

F

Frank Neuhaus

Hi
Iam trying to make a class where you can register callbacks to member
functions of other classes.
I declared the functions I wish to use like this:

void SetMouseProc(ONMOUSEPROC proc, void* pUserData);

where ONMOUSEPROC is this type:

typedef void (*ONMOUSEPROC) (void* pUserData, int iX, int iY);

The type is supposed to "emulate" the __thiscall calling convention, where
the first parameter is always the "this" pointer to the class instance.
That way i should be able to pass any member function to this function, and
the this pointer as userdata and it should work.
Unfortunately it does not.

This is how I call SetMouseProc:

void (CGame::*pf3)(int,int)=&CGame::OnMouse;
ONMOUSEPROC b=*(ONMOUSEPROC*)&pf3;
m_Input.SetMouseProc(b,g_pGame);

For some reason, directly casting the &CGame::OnMouse to (ONMOUSEPROC) didnt
work, so I had to "force" it to the other type :)

The code above is syntactically correct ( and admittedly evil looking ;/ ),
but it crashes as soon as one tries to actually call the callback function.
Any ideas how I could either fix my problem or replace my current hack
solution by a cleaner method?

Thanks in advance
 
V

Victor Bazarov

Frank said:
Iam trying to make a class where you can register callbacks to member
functions of other classes.
I declared the functions I wish to use like this:

void SetMouseProc(ONMOUSEPROC proc, void* pUserData);

where ONMOUSEPROC is this type:

typedef void (*ONMOUSEPROC) (void* pUserData, int iX, int iY);
[...]

Your question is covered in the FAQ.
 
F

Frank Neuhaus

Hmm ok thanks ;)
Just wondering:
Ive just been playing around with asm a little and I managed to fake the
thiscall calling convention properly by changing my stuff to this:
typedef void (_cdecl *ONMOUSEPROC) (int iX, int iY);

now a call to this function works like this:

mov esi,esp ;

mov eax,dword ptr [dwY] ;// param 2

push eax ;

mov ecx,dword ptr [dwX] ;// param 1

push ecx ;

mov ecx, dword ptr[userdata];// userdata contains the this pointer to the
class that was passed to the SetMouseProc function earlier

mov edx,dword ptr [this] ;

call dword ptr [edx+8] ; // just the location of the function inside the
class



Now this code works. The callback is issued correctly. Obviously a very
unclean way of doing it. But what Iam asking myself is: Why is there no way
in C++ to do the same? Why shouldnt the user be allowed to "fake" thiscalls?

Victor Bazarov said:
Frank said:
Iam trying to make a class where you can register callbacks to member
functions of other classes.
I declared the functions I wish to use like this:

void SetMouseProc(ONMOUSEPROC proc, void* pUserData);

where ONMOUSEPROC is this type:

typedef void (*ONMOUSEPROC) (void* pUserData, int iX, int iY);
[...]

Your question is covered in the FAQ.
 
F

Frank Neuhaus

It is already virtual, because the CGame class is an abstract base class.
 
P

Pete Becker

Frank said:
It is already virtual, because the CGame class is an abstract base class.

I didn't read your code closely enough. You've actually simulated a
simple virtual call there. The comment on the actual call is slightly
incorrect: it's not the location of the function inside the class, but
the location of the function pointer in the class's vtable. But that
particular calling sequence won't work with a non-virtual function, and
might or might not work when the this pointer points to a virtual base,
and won't work with an implementation that doesn't lay objects out the
same way as your compiler. Pointers to member functions hide all this
detail, all of which is implementation-specific, anyway.

This sort of asm-level hacking should only be done in the privacy of
your own home. <g>
 
F

Frank Neuhaus

Of course thats not a nice code and I wouldnt use it in my program anyway.
But iam simply wondering why its not possible to use the thiscall calling
convention just like any other?
 
P

Pete Becker

Frank said:
Of course thats not a nice code and I wouldnt use it in my program anyway.
But iam simply wondering why its not possible to use the thiscall calling
convention just like any other?

You'll have to ask Microsoft. There's no thiscall calling convention in
Standard C++. Just extern "C" and extern "C++", both of which
potentially involve the calling convention, among other things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top