B
Bj?rn Toft Madsen
Hi all,
The network library I use communicates with my app using a callback
function. This callback function is called with the type of message, a
void pointer to he actual message and a user defined void pointer.
The callback function expected is a standard C callback function. I
have used a static class function, which I certainly know is not
entirely correct. Bear with me, I don't think my troubles are related
to this hack.
When the class is constructed, I set up the network library by
supplying the address of the static method as the callback and I also
pass along "this" as the user defined void pointer.
In the static method, the following takes place:
HRESULT CDPServer::MessageHandler(PVOID pvUserContext, DWORD
dwMessageType, PVOID pMessage) {
switch(dwMessageType) {
case DPN_MSGID_ADD_PLAYER_TO_GROUP:
return ((CDPServer*)pvUserContext)->AddPlayerToGroup((PDPNMSG_ADD_PLAYER_TO_GROUP)pMessage);
break;
//many more cases
}
}
As you can see, I cast the user defined pointer to CDPServer*.
The issue is that naturally all these "event handlers" like
AddPlayerToGroup() are meant to be overridden in sub-classes of
CDPServer:
class CMyServer : public CDPServer {
protected:
//other stuff
virtual HRESULT AddPlayerToGroup(/*...*/);
}
So in effect, my static message handler casts the pointer to
CMyServer's super class (CDPServer) and then calls the overridden
function AddPlayerToGroup()
The application behaves as expected when running, but my debugger (a
rather old MSVC++ 6.0) jumps into CDPServer::AddPlayerToGroup()
instead of CMyServer::AddPlayerToGroup(). Once in there, it goes about
executing empty lines and generally behaves weird.
Questions that I hope somebody cleverer than I can answer:
1) Is this approach canon?
2) Is the fact that the debugger jumps into
CDPServer::AddPlayerToGroup() related to me using a bad approach, or
can I chalk it up to my rather old debugger? When running, after all,
things happen the way they are suppose to.
My suggestions as to how these issues could be alleviated; please
don't hesitate to comment:
1) Use typeid to discern the correct type and thus make sure that I
call the correct method. Isn't this what virtual does?
2) Visitor pattern (supply a message handler object and then have
CDPServer call functions in this) instead of deriving from CDPServer.
Thanks in advance for any pointers you might have.
Best regards
Bjørn Toft Madsen
The network library I use communicates with my app using a callback
function. This callback function is called with the type of message, a
void pointer to he actual message and a user defined void pointer.
The callback function expected is a standard C callback function. I
have used a static class function, which I certainly know is not
entirely correct. Bear with me, I don't think my troubles are related
to this hack.
When the class is constructed, I set up the network library by
supplying the address of the static method as the callback and I also
pass along "this" as the user defined void pointer.
In the static method, the following takes place:
HRESULT CDPServer::MessageHandler(PVOID pvUserContext, DWORD
dwMessageType, PVOID pMessage) {
switch(dwMessageType) {
case DPN_MSGID_ADD_PLAYER_TO_GROUP:
return ((CDPServer*)pvUserContext)->AddPlayerToGroup((PDPNMSG_ADD_PLAYER_TO_GROUP)pMessage);
break;
//many more cases
}
}
As you can see, I cast the user defined pointer to CDPServer*.
The issue is that naturally all these "event handlers" like
AddPlayerToGroup() are meant to be overridden in sub-classes of
CDPServer:
class CMyServer : public CDPServer {
protected:
//other stuff
virtual HRESULT AddPlayerToGroup(/*...*/);
}
So in effect, my static message handler casts the pointer to
CMyServer's super class (CDPServer) and then calls the overridden
function AddPlayerToGroup()
The application behaves as expected when running, but my debugger (a
rather old MSVC++ 6.0) jumps into CDPServer::AddPlayerToGroup()
instead of CMyServer::AddPlayerToGroup(). Once in there, it goes about
executing empty lines and generally behaves weird.
Questions that I hope somebody cleverer than I can answer:
1) Is this approach canon?
2) Is the fact that the debugger jumps into
CDPServer::AddPlayerToGroup() related to me using a bad approach, or
can I chalk it up to my rather old debugger? When running, after all,
things happen the way they are suppose to.
My suggestions as to how these issues could be alleviated; please
don't hesitate to comment:
1) Use typeid to discern the correct type and thus make sure that I
call the correct method. Isn't this what virtual does?
2) Visitor pattern (supply a message handler object and then have
CDPServer call functions in this) instead of deriving from CDPServer.
Thanks in advance for any pointers you might have.
Best regards
Bjørn Toft Madsen