Archived

This topic is now archived and is closed to further replies.

Pipo DeClown

Whats wrong? DirectPlay8

Recommended Posts

Is there a stupid mistake maken here? I also have a server class but I think the problem lies here...
    // Constructor

CDPlayClient::CDPlayClient()
{
	this->mDPClient = NULL;
	this->mDPAddress = NULL;
	this->mDPDevice = NULL;

	CoInitialize(0);					// Init COM

}
// Destructor

CDPlayClient::~CDPlayClient()
{
	mDPClient->Close(0);
	ReleaseCOM( this->mDPDevice );
	ReleaseCOM( this->mDPAddress );
	ReleaseCOM( this->mDPClient );

	CoUninitialize();					// Uninit COM

}
// Enumerate TCPIP network devices

GUID CDPlayClient::EnumDevices( IDirectPlay8Client* pDPClient )
{
	IDirectPlay8Address* pDPDevice = NULL;
	DPN_SERVICE_PROVIDER_INFO* pSP = NULL;
	DPN_SERVICE_PROVIDER_INFO* pSPPtr;
	GUID guiddev;
	DWORD Size = 0;
	DWORD dwNumSP = 0;

	if( pDPClient == NULL )
		MessageBox( NULL,"PDPCLIENT == NULL","ERROR",MB_OK);

	if( SUCCEEDED( pDPClient->EnumServiceProviders( &CLSID_DP8SP_TCPIP, NULL, pSP,
													&Size, &dwNumSP, 0 )))
	{
		pSP = (DPN_SERVICE_PROVIDER_INFO*)new BYTE[Size];

		if( SUCCEEDED( pDPClient->EnumServiceProviders( &CLSID_DP8SP_TCPIP, NULL, pSP,
														&Size, &dwNumSP, 0)))
		{
			pSPPtr = pSP;
			guiddev=pSPPtr->guid;
		}
		delete[] pSP;
	}
	
	return guiddev;
}
// Initializes the client

IDirectPlay8Client* CDPlayClient::StartClient( char* myName, char* gameName, char* gamePass,
											   char* gameIP, DWORD gamePort )
{

	IDirectPlay8Client* pDPClient;

	// Create COM object: IDirectPlay8Client

	if( FAILED( CoCreateInstance( CLSID_DirectPlay8Client, NULL, CLSCTX_INPROC,
								  IID_IDirectPlay8Client, (LPVOID*)&pDPClient )))
	{
		return NULL;
	}
	// Initialize the client

	if( FAILED( pDPClient->Initialize( pDPClient, ClientMsgHandler, 0)))
	{
		pDPClient->Release();
		return NULL;
	}
	// Configure the DPN_PLAYYER_INFO

	ZeroMemory( &dppi, sizeof(DPN_PLAYER_INFO) );
	dppi.dwSize = sizeof( DPN_PLAYER_INFO );
	dppi.dwInfoFlags = DPNINFO_NAME | DPNINFO_DATA;
	mbstowcs( PlayerName, myName, strlen(myName)+1 );
	dppi.pwszName = PlayerName;
	
	// Set DPN_PLAYER_INFO

	pDPClient->SetClientInfo( &dppi, NULL, NULL, DPNSETCLIENTINFO_SYNC );
	
	// Create COM object: IDirectPlay8Address

	if( FAILED( CoCreateInstance( CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC,
								  IID_IDirectPlay8Address, (LPVOID*)&this->mDPAddress )))
	{
		pDPClient->Release();
		return NULL;
	}
	if( FAILED( CoCreateInstance( CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC,
								  IID_IDirectPlay8Address, (LPVOID*)&this->mDPDevice )))
	{
		pDPClient->Release();
		return NULL;
	}

	// ENUM DEVICES

	guiddev = EnumDevices( pDPClient );
	
	// Configure mDPAddress

	mDPAddress->SetSP( &CLSID_DP8SP_TCPIP );
	mDPDevice->SetSP( &CLSID_DP8SP_TCPIP );
	
	// Resolve/Add HostIP

	mbstowcs( ServerIP, gameIP, strlen(gameIP)+1 );
	mDPAddress->AddComponent( DPNA_KEY_HOSTNAME, ServerIP, (wcslen(dppi.pwszName)+1)*sizeof(WCHAR),
							  DPNA_DATATYPE_STRING );
	mDPAddress->AddComponent( DPNA_KEY_PORT, &gamePort, sizeof(DWORD), DPNA_DATATYPE_DWORD );
	mDPAddress->AddComponent( DPNA_KEY_DEVICE, &guiddev, sizeof(GUID), DPNA_DATATYPE_GUID );
	mDPDevice->AddComponent( DPNA_KEY_DEVICE, &guiddev, sizeof(GUID), DPNA_DATATYPE_GUID );
		
	// Setup session info

	ZeroMemory( &dpad, sizeof(DPN_APPLICATION_DESC));
	dpad.dwSize = sizeof( DPN_APPLICATION_DESC );
	dpad.dwFlags = DPNSESSION_CLIENT_SERVER;
	mbstowcs( ServerName, gameName, sizeof(gameName)+1 );
	dpad.pwszSessionName = ServerName;
	dpad.guidApplication = AppGuid;
	
	if( gamePass != NULL )
	{
		mbstowcs( ServerPass, gamePass, sizeof(gamePass)+1);
		dpad.pwszPassword = ServerPass;
		dpad.dwFlags |= DPNSESSION_REQUIREPASSWORD;
	}
	
	return pDPClient;
}

HRESULT WINAPI CDPlayClient::ClientMsgHandler( PVOID pvUserContext, DWORD dwMsgID, PVOID pvMsgBuf )
{
	switch( dwMsgID )
	{
	default: break;
	}
	return E_FAIL;
}

int CDPlayClient::ConnectTo()
{
	// Connect To

	if( FAILED( mDPClient->Connect( &dpad, mDPAddress, mDPDevice, NULL,
									NULL, NULL, 0, NULL, NULL, DPNCONNECT_SYNC )))
		{
			return 0;
		}
		
	return 1;

}    
The way I call it: myClient.mDPClient = myClient.StartClient( "ClientPlayer", "q", NULL, "192.168.1.2", 12344 ); assert( myClient.mDPClient!= NULL ); assert( myClient.mDPAddress != NULL ); assert( myClient.mDPDevice != NULL ); if(!myClient.ConnectTo()) MessageBox( 0,"Can't ConnectTo()","FAILED",MB_OK); Help??? [edited by - Pipo DeClown on November 4, 2002 12:42:38 PM]

Share this post


Link to post
Share on other sites