Skip to main content
gamedev.net
🔒 Locked

Dplay class

Started by djurens 5 replies 900+ views
Original Post
djurens
djurens
Hi! I am trying to create a directPlay class but am having problems with the WINAPI messagehandler function. I guess I am not the first to do this so maybe someone outthere can help me? In "procedural" code I have this: HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer); HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) { switch(dwMessageId) case ... ... } pDP->Initialize(NULL, DirectPlayMessageHandler, 0) And in "class" code I want the exact same thing but with cDPlay:: HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer); HRESULT WINAPI cDPlay::DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer ) { switch(dwMessageId) case ... ... } pDP->Initialize(NULL, DirectPlayMessageHandler, 0) But this generates cannot convert type1/type2 errors. So how it is done? Please Help!
JonStelly
JonStelly
Your handler needs to be static.

This is my DPlay client class declaration:

class CGameCli{/* ... other stuff ... */private:	IDirectPlay8Client *m_piCli;	static HRESULT WINAPI MSG_HANDLER(LPVOID pvCli, DWORD dwMsgType, LPVOID pvMsg);/* ... other stuff ... */};


and this is my initialization code:

	hr = m_piCli->Initialize(this, MSG_HANDLER, 0);


and this is the MSG_HANDLER code (the important part):

HRESULT WINAPI CGameCli::MSG_HANDLER(LPVOID pvCli, DWORD dwMsgType, LPVOID pvMsg){	CGameCli *pCli = static_cast<CGameCli*>(pvCli);/* ... other stuff ... */}

djurens
djurens
Thank you for the answer it helps me alot! but why does it have to be static?
Guimo
Guimo
Hi!
I found a funny solution checking the DXFramework. Like this:


//The DPCLASS.H
class DPCLASS
{ public:
LPDIRECTPLAY8 m_lpDPlay;
... etc etc... other members like the client or server
public:
DPCLASS();
HRESULT HandleMessage( LPVOID pvCli, DWORD dwMsgType,
LPVOID pvMsg);
HRESULT CreateDPlay( params you need );
... etc etc... other methods
}


ok... now the implementation

#include <dpclass.h>

DPCLASS* myClass = NULL; //just a simple pointer to a DPClass

//A proxy function, if myClass is set call its method
static HRESULT WINAPI DPMsgHandler(LPVOID pvCli, DWORD dwMsgType, LPVOID pvMsg)
{ if(myClass!=NULL)
myClass->HandleMessage( pvCli, dwMsgType, pvMsg);
}

//The constructor
DPCLASS::DPCLASS()
{ myClass = this; //set the proxy pointer to myself
}

HRESULT DPCLASS::CreateDPlay()
{ ... init code
pDP->Initialize(NULL, DPMsgHandler, 0);
...

}

... etc etc...



So, the idea is just to create a proxy function. Not that bad, is a lame but simple solution if you wish to use something quick. The problem is that it is limited to a singleton object (you usually have only one connection anyway). I recommend using the previous solution but... you have this one anyway.

Luck!
Guimo
djurens
djurens
Very interesting! I have come up with something similar myself. I have not been able to test it yet, so I am not sure it works. I am wondering if i have to pass DPN_MSGID_CREATE_PLAYER as the second parameter instead of dwMessageId? Anyhow here is my approach, does anyone think it will work?

...
// in .h file
static HRESULT WINAPI StaticDirectPlayMessageHandler
(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
{
return (reinterpret_cast(pvUserContext)->DirectPlayMessageHandler(pvUserContext, dwMessageId, pMsgBuffer));
}

HRESULT WINAPI DirectPlayMessageHandler
(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);

...

// in .cpp file
...
HRESULT WINAPI cDPlay::DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
...
// in init-dp function
pDP->Initialize(this, StaticDirectPlayMessageHandler, 0)
JonStelly
JonStelly
Yes, at first glance that's almost exactly what I do. I have some things I do in the static message handler, but it's really just meant for the callback, all the important logic goes into a virtual function for handling messages.
djurens
djurens
Great! I have not had the chance to test it yet because i am still writing some other classes i need. Thank you guys for your help, i post a new reply when i have tested it!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.