Advertisement

AddComponent Troubles

Started by March 10, 2003 04:55 PM
-1 comments, last by joshdw1 21 years, 10 months ago
EDIT: Nevermind, I was stupid and forgot to set it up with CoCreateInstance() ----- What would cause me to get memory access violation errors when using AddComponent in DirectPlay? The problem happens when I try to join a game, hosting game has no progrems with AddComponent(); Here's my Join Function:
      
HRESULT Connection::hrJoinGame( HWND hWnd )
{
	WCHAR				 HostName[16];
	WCHAR				 PeerName[256];
	char				 szPeerName[256];
	char				 IP[16];
	char				 Port[256];
	DWORD				 dwPort;
	DWORD				 Length = 256;
	DPN_APPLICATION_DESC AppDesc;
	DPN_PLAYER_INFO      PlayerInfo;
	HWND                 hIP;
	HWND                 hPort;
	HWND                 hName;

	PutText( "Attempting to connect" );

	// Get Windows Handles

	hIP   = GetDlgItem( hWnd, EDIT_IP );
	hPort = GetDlgItem( hWnd, EDIT_PORT );
	hName = GetDlgItem( hWnd, EDIT_NAME );

	// Get Player Name

	GetWindowText( hName, szPeerName, 36 );
	DXUtil_ConvertGenericStringToWide( PeerName, szPeerName );

	// Set Player Name

	ZeroMemory( &PlayerInfo, sizeof( DPN_PLAYER_INFO ) );
	PlayerInfo.dwSize      = sizeof( DPN_PLAYER_INFO );
	PlayerInfo.dwInfoFlags = DPNINFO_NAME;
	PlayerInfo.pwszName    = PeerName;
	if( FAILED( g_pDP->SetPeerInfo( &PlayerInfo, NULL, NULL, DPNSETPEERINFO_SYNC ) ) )
	{
		PutText( "Failed to Set Peer" );
		return -1;
	}

	// Prepare the application description

	ZeroMemory( &AppDesc, sizeof( DPN_APPLICATION_DESC ) );
	AppDesc.dwSize          = sizeof( DPN_APPLICATION_DESC );
	AppDesc.guidApplication = CONN_GUID;

	// Get the IP we are connecting too

	GetWindowText( hIP, IP, 16 );
	DXUtil_ConvertGenericStringToWide( HostName, IP );

	// Get the port we are connecting too

	GetWindowText( hPort, Port, 6 );
	dwPort = atol( Port );
	
	// Add Host Name

	if( FAILED( g_pHostAddress->AddComponent( DPNA_KEY_HOSTNAME, HostName, ( wcslen( HostName) + 1 ) * sizeof( WCHAR ), DPNA_DATATYPE_STRING ) ) )
	{
		PutText( "Failed to Add Host Name" );
		return -1;
	}

	// Add Port

	if( FAILED( g_pHostAddress->AddComponent( DPNA_KEY_PORT, &Port, sizeof( DWORD ), DPNA_DATATYPE_DWORD ) ) )
	{
		PutText( "Failed to Add Port" );
		return -1;
	}

	// Connect to the session!

	HRESULT hReturn = g_pDP->Connect( &AppDesc, g_pHostAddress,	g_pDeviceAddress, NULL,	NULL, NULL,	0, 
		                              NULL, NULL, &g_hConnectAsyncOp, NULL);

	if( hReturn != E_PENDING && FAILED( hReturn ) ) 
	{
		PutText( "Failed to Connect" );
		return -1;
	}

	SetTimer( hWnd, TIMERID_CONNECT_COMPLETE, 100, NULL );

	return S_OK;
}
      
PutText() is just a function dumping error messages into a list box. BTW, if it helps - I'm using the multiplayer game programming book by lostlogic to learn how to do this stuff, I got most of the code for the preceeding function from it. Thanks in advance for any help [edited by - joshdw1 on March 10, 2003 5:55:33 PM] [edited by - joshdw1 on March 10, 2003 6:34:50 PM]

This topic is closed to new replies.

Advertisement