Advertisement

DirectPlay Troubles...

Started by May 13, 2000 10:22 PM
1 comment, last by ziplux 24 years, 7 months ago
Hello all. I''ve been having some problems with getting DirectPlay to work. It is supposed to prompt for Host or Join, and it does that fine. Host also seems to work, but when I launch another instance of the application and type join, it gives me the DPERR_INVALIDPARAMS error. Wheat it should do is run the callback function and print out all the sessions. Any help would be greatly appreciated. Thanks in advance, the code is here:

#define INITGUID

#include    // include important windows stuff
#include  
#include 
#include  // include important C/C++ stuff
#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include      // directplay main
#include    // the directplay lobby


// {C852A171-28E4-11d4-833A-444553546170}
DEFINE_GUID(test_app, 
0xc852a171, 0x28e4, 0x11d4, 0x83, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x61, 0x70);

// interface pointer to directplay
LPDIRECTPLAY4A      lpdp = NULL;      
// lobby interface pointer
LPDIRECTPLAYLOBBY3A lpdplobby = NULL;

BOOL gameServer;       // flag for client/server
char player_name[10];  // local player name
char session_name[10]; // name of session
char tcp_address[15];  // ip address to connect to

int Create_TCP_Connection(char *IP_address);
int DirectPlay_Shutdown();
EnumSessions(LPDPSESSIONDESC2 lpsd, DWORD dwTimeOut,
             LPDPENUMSESSIONSCALLBACK2 lpEnumSessionCallback2,
             LPVOID context, DWORD dwFlags);
BOOL FAR PASCAL EnumSessionsCallback(LPCDPSESSIONDESC2 lpThisSD,
                                     LPDWORD lpdwTimeOut,
                                     DWORD dwFlags, LPVOID lpContext);

void main(void)
	{
	DPSESSIONDESC2   dpsd;    // session desc
	char choice;
	cout << "(H)ost or (J)oin?" << endl;
	scanf("%c", &choice);

	if(choice == ''J'')
		{

		cout << Create_TCP_Connection("127.0.0.1") << endl;

		// we defined test_app earlier
		dpsd.guidApplication = test_app;
		HRESULT hr;
		if(FAILED(hr = lpdp->EnumSessions(&dpsd, 0, EnumSessionsCallback, NULL, DPENUMSESSIONS_ALL)))
			{
			if(hr == DPERR_CONNECTING)
				cout << "DPERR_CONNECTING" << endl;
			if(hr == DPERR_CONNECTIONLOST)
				cout << "DPERR_CONNECTIONLOST" << endl;
			if(hr == DPERR_GENERIC)
				cout << "DPERR_GENERIC" << endl;
			if(hr == DPERR_INVALIDOBJECT)
				cout << "DPERR_INVALIDOBJECT" << endl;
			if(hr == DPERR_INVALIDPARAMS)
				cout << "DPERR_INVALIDPARAMS" << endl;
			if(hr == DPERR_NOCONNECTION)
				cout << "DPERR_NOCONNECTION" << endl;
			if(hr == DPERR_UNINITIALIZED)
				cout << "DPERR_UNINITIALIZED" << endl;
			if(hr == DPERR_USERCANCEL)
				cout << "DPERR_USERCANCEL" << endl;
			}
		}
		else
			{
			cout << Create_TCP_Connection("") << endl;
			ZeroMemory( &dpsd, sizeof(dpsd) );
			dpsd.dwSize           = sizeof(dpsd);
			dpsd.guidApplication  = test_app;
			dpsd.lpszSessionNameA = "Jared";
			dpsd.dwMaxPlayers     = 10;

			// The DPSESSION_KEEPALIVE flag keeps the session alive 
			//  if players abnormally exit.
			dpsd.dwFlags          = DPSESSION_KEEPALIVE / DPSESSION_MIGRATEHOST / DPSESSION_DIRECTPLAYPROTOCOL;
 
			if(FAILED(lpdp->Open( &dpsd, DPOPEN_CREATE )))
				cout << "Open failed" << endl;
			DPNAME name;  // name type
			DPID   dpid;  // the dpid of the player, given by directplay

			ZeroMemory(&name,sizeof(DPNAME));   // clear out structure
			name.dwSize = sizeof(DPNAME);
			name.lpszShortNameA = "Test";  // the name the from the user
			name.lpszLongNameA = NULL;

			lpdp->CreatePlayer(&dpid, &name, NULL, NULL, 0, DPPLAYER_SERVERPLAYER);

			// clear the desc
			ZeroMemory(&dpsd, sizeof(DPSESSIONDESC2));
			dpsd.dwSize = sizeof(DPSESSIONDESC2);
			choice = ''R'';
			while(choice != ''Q'')
				{
				scanf("%c", &choice);
				}
			}
		lpdp->Close();
		cout << DirectPlay_Shutdown() << endl;
	return;
	}

int Create_TCP_Connection(char *IP_address)
{
    LPDIRECTPLAYLOBBYA        old_lpdplobbyA = NULL;    // old lobby pointer
    DPCOMPOUNDADDRESSELEMENT  Address[2];               // to create compound addr
    DWORD     AddressSize = 0;       // size of compound address
    LPVOID    lpConnection= NULL;    // pointer to make connection
	HRESULT hr;

    CoInitialize(NULL);    // registering COM

    // creating directplay object
    if  ( CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER,
         IID_IDirectPlay4A,(LPVOID*)&lpdp ) != S_OK)
    {
        // return  a messagebox error
        CoUninitialize();   // unregister the comp
        return(0);
    }

    // creating lobby object
    DirectPlayLobbyCreate(NULL, &old_lpdplobbyA, NULL, NULL, 0);

    // get new interface of lobby
    old_lpdplobbyA->QueryInterface(IID_IDirectPlayLobby3A, (LPVOID *)&lpdplobby);

    old_lpdplobbyA->Release();   // release old interface since we have new one

    // fill in data for address
    Address[0].guidDataType = DPAID_ServiceProvider;
    Address[0].dwDataSize   = sizeof(GUID);
    Address[0].lpData       = (LPVOID)&DPSPGUID_TCPIP;  // TCP ID

    Address[1].guidDataType = DPAID_INet;
    Address[1].dwDataSize   = lstrlen(IP_address)+1;
    Address[1].lpData       = IP_address;

    // get size to create address
    // this method will return DPERR_BUFFERTOOSMALL not an error
    if(FAILED(hr = lpdplobby->CreateCompoundAddress(Address, 2, NULL, &AddressSize)))
		{
		if(hr == DPERR_BUFFERTOOSMALL)
			{}
		else
			return(0);
		}

    lpConnection = GlobalAllocPtr(GHND, AddressSize);  // allocating mem

    // now creating the address
    if(FAILED(lpdplobby->CreateCompoundAddress(Address, 2, lpConnection, &AddressSize)))
		return(0);

    // initialize the tcp connection
    lpdp->InitializeConnection(lpConnection, 0);

    GlobalFreePtr(lpConnection);	// free allocated memory

    return(1);	// success

}	// end int Create_TCP_Connection(..)

int DirectPlay_Shutdown()
{
    if (lpdp)   // if connection already up, so it won''t be null
    {
        if (lpdplobby)
            lpdplobby->Release();

        lpdp->Release();

        CoUninitialize();  // unregister the COM
    }

    lpdp       = NULL;  // set to NULL, safe practice here
    lpdplobby  = NULL;

    return(1);  // always success
}  // end int DirectPlay_Shutdown();

// our callback function
BOOL FAR PASCAL EnumSessionsCallback(LPCDPSESSIONDESC2 lpThisSD,
                                     DWORD* lpdwTimeOut,
                                     DWORD dwFlags, VOID * lpContext)
{

    if (dwFlags & DPESC_TIMEDOUT)  // if finished enumerating stop
        return(FALSE);

    cout << lpThisSD-> lpszSessionNameA;
    //cout << lpThisSD->guidInstance;         // store this, the instance of the host

    return(TRUE);  // keep enumerating
} // end callback
Visit our web site: Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
Come on, guys and gals! Someone must be able to help me!

Visit our web site:
Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
Advertisement
I just figured it out , I wasn''t setting the dwSize member of the dpsd struct in the Join code, stupid Jared! So it works now, and you can ignore this thread.

Visit our web site:
Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment

This topic is closed to new replies.

Advertisement