Original Post
first I would like to appologise for listing a lot of code here, but I need to figure out what is not working in these 3 functions. I'm modifyin Directplay sample from DX7 (dont ask why DX7, I need this version). but I'm able to launch lobby application, but it is not connecting to host or even hosting a session. How can I debug DX code? please review the code and help me. First enumerate lobbyable applications on system, then try to create address object (I think that is a place of my problems, but cannot see where). then create connection infor and finally launch application with given connection info. application launches fine but is not hosting a session. game message saying unable to join a game. [Edited by - m_shuhrat on February 26, 2007 10:51:34 PM]
BOOL FAR PASCAL EnumAddressTypes( REFGUID guidAddressType, VOID* pContext,
DWORD dwFlags )
{
ADDRESSTYPELIST* pAddressTypes = (ADDRESSTYPELIST*)pContext;
if( pAddressTypes->dwCount < 10 )
{
// Save the address type guid in the list
pAddressTypes->guidAddressTypes[pAddressTypes->dwCount] = guidAddressType;
pAddressTypes->dwCount++;
}
return TRUE;
}
HRESULT CreateAddress( HWND hWnd, GUID* pServiceProviderGUID, VOID** ppAddress,
DWORD* pdwAddressSize )
{
ADDRESSTYPELIST addressTypeList;
DPCOMPOUNDADDRESSELEMENT addressElements[11];
GUID guidAddressType;
VOID* pAddress = NULL;
DWORD dwAddressSize = 0;
DWORD i;
DWORD dwElementCount;
HRESULT hr;
char *ipAddress="192.168.1.2";
// Get the list of address types for this service provider
ZeroMemory( &addressTypeList, sizeof(ADDRESSTYPELIST) );
hr = pDPLClient->EnumAddressTypes( EnumAddressTypes, *pServiceProviderGUID,
&addressTypeList, 0L );
if( FAILED(hr) )
return hr;
dwElementCount = 0;
// All DPADDRESS's must have a service provider chunk
addressElements[dwElementCount].guidDataType = DPAID_ServiceProvider;
addressElements[dwElementCount].dwDataSize = sizeof(GUID);
addressElements[dwElementCount].lpData = pServiceProviderGUID;
dwElementCount++;
_stprintf(str, _T("dwCount 2 is %d"), addressTypeList.dwCount);
MessageBox(0, str, 0, MB_OK);
// Loop over the address types
for( i = 0; i < addressTypeList.dwCount; i++ )
{
guidAddressType = addressTypeList.guidAddressTypes;
if( IsEqualGUID( guidAddressType, DPAID_INet ) )
{
// Add an IP address chunk
addressElements[dwElementCount].guidDataType = DPAID_INet;
addressElements[dwElementCount].dwDataSize = lstrlen(ipAddress) + 1;
addressElements[dwElementCount].lpData = ipAddress;
dwElementCount++;
}
}
// Bail if no address data is available
if( dwElementCount == 1 )
return DPERR_GENERIC;
// See how much room is needed to store this address
hr = pDPLClient->CreateCompoundAddress( addressElements, dwElementCount,
NULL, &dwAddressSize );
if( hr != DPERR_BUFFERTOOSMALL )
return hr;
// Allocate space
pAddress = GlobalAllocPtr( GHND, dwAddressSize );
if( pAddress == NULL )
return DPERR_NOMEMORY;
// Create the address
hr = pDPLClient->CreateCompoundAddress( addressElements, dwElementCount,
pAddress, &dwAddressSize );
if( FAILED(hr) )
{
GlobalFreePtr( pAddress );
return hr;
}
// Return the address info
*ppAddress = pAddress;
*pdwAddressSize = dwAddressSize;
return DP_OK;
}
HRESULT RunProgram(){
HRESULT hr;
TCHAR str[255];
CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay4A, (VOID**)&pDPLobby);
CoCreateInstance(CLSID_DirectPlayLobby, NULL,CLSCTX_INPROC_SERVER, IID_IDirectPlayLobby3A, (VOID**)&pDPLClient);
pDPLClient->EnumLocalApplications(ApplicationEnumerationCallBack, NULL, 0);
//This is one way of creating address object
GUID inet=DPSPGUID_TCPIP;
CreateAddress( NULL, &inet,&pAddress,&addressSize );
//This is other way of creating address object
//DPCOMPOUNDADDRESSELEMENT addEls[2];
//char *ipAddress="192.168.1.2";
//addEls[0].guidDataType=DPAID_ServiceProvider;
//addEls[0].dwDataSize=sizeof(GUID);
//addEls[0].lpData=(LPVOID)&DPSPGUID_TCPIP;
//addEls[1].guidDataType=DPAID_INet;
//addEls[1].dwDataSize=strlen(ipAddress)+1;
//addEls[1].lpData=ipAddress;
//pDPLClient->CreateCompoundAddress(addEls, 2, NULL, &addressSize);
//pDPLClient->CreateCompoundAddress(addEls, 2, &pAddress, &addressSize);
DWORD appID;
DPLCONNECTION cInfo;
DPSESSIONDESC2 sessionInfo;
DPNAME playerName;
// Fill out session description
ZeroMemory( &sessionInfo, sizeof(DPSESSIONDESC2) );
sessionInfo.dwSize = sizeof(DPSESSIONDESC2);
sessionInfo.dwFlags = 0; // DPSESSION_xxx flags
sessionInfo.guidApplication = gameGUID; // GUID of the DirectPlay application.
sessionInfo.dwMaxPlayers = 0; // Maximum # players allowed in session
sessionInfo.dwCurrentPlayers = 0; // Current # players in session (read only)
sessionInfo.lpszSessionNameA = "Sameple"; // ANSI name of the session
sessionInfo.lpszPasswordA = NULL; // ANSI password of the session (optional)
sessionInfo.dwReserved1 = 0; // Reserved for future MS use.
sessionInfo.dwReserved2 = 0;
sessionInfo.dwUser1 = 0; // For use by the application
sessionInfo.dwUser2 = 0;
sessionInfo.dwUser3 = 0;
sessionInfo.dwUser4 = 0;
// Fill out player name
ZeroMemory( &playerName, sizeof(DPNAME) );
playerName.dwSize = sizeof(DPNAME);
playerName.dwFlags = 0; // Not used. Must be zero.
playerName.lpszShortNameA = "Test"; // ANSI short or friendly name
playerName.lpszLongNameA = "Test"; // ANSI long or formal name
// Fill out connection description
ZeroMemory( &cInfo, sizeof(DPLCONNECTION) );
cInfo.dwSize = sizeof(DPLCONNECTION);
cInfo.lpSessionDesc = &sessionInfo // Pointer to session desc to use on connect
cInfo.lpPlayerName = &playerName // Pointer to Player name structure
cInfo.guidSP = DPSPGUID_TCPIP; // GUID of the DPlay SP to use
cInfo.lpAddress = pAddress; // Address for service provider
cInfo.dwAddressSize = addressSize; // Size of address data
cInfo.dwFlags = DPLCONNECTION_CREATESESSION;
hr=pDPLClient->RunApplication( 0, &appID, &cInfo, NULL );
if(hr==DPERR_CANTCREATEPROCESS )
MessageBoxA(NULL, "Game is not installed", "", MB_OK);
else if(hr==DPERR_GENERIC )
MessageBoxA(NULL, "Game is not installed", "", MB_OK);
else if(hr==DPERR_INVALIDINTERFACE )
MessageBoxA(NULL, "DPERR_INVALIDINTERFACE ", "", MB_OK);
else if(hr==DPERR_INVALIDOBJECT )
MessageBoxA(NULL, "DPERR_INVALIDOBJECT ", "", MB_OK);
else if(hr==DPERR_INVALIDPARAMS )
MessageBoxA(NULL, "DPERR_INVALIDPARAMS ", "", MB_OK);
else if(hr==DPERR_OUTOFMEMORY )
MessageBoxA(NULL, "DPERR_OUTOFMEMORY ", "", MB_OK);
else if(hr==DPERR_UNKNOWNAPPLICATION )
MessageBoxA(NULL, "DPERR_UNKNOWNAPPLICATION ", "", MB_OK);
if(pDPLobby)
SAFE_RELEASE(pDPLobby);
if(pDPLClient)
SAFE_RELEASE(pDPLClient);
return DP_OK;
}