| Platform SDK: DirectX |
This tutorial pertains only to applications written in C++. See DirectPlay Visual Basic Tutorials.
The next step in creating a manual connection is to request that the user select a communication medium for the application. Your application can identify the service providers installed on a personal computer by using the EnumConnections method.
The following example shows how to enumerate the service providers:
// Enumerate all DirectPlay connections and store them
// in the listbox
if ( FAILED( hr = pDP->EnumConnections( &g_AppGUID,
DPConnect_EnumConnectionsCallback, hWndListBox, 0 ) ) )
{
SAFE_RELEASE( pDP );
return hr;
}
The second parameter in the EnumConnections method is a callback that enumerates service providers registered with DirectPlay. The following example shows how the SimpleConnect Sample implements this callback function:
BOOL FAR PASCAL DPConnect_EnumConnectionsCallback(
LPCGUID pguidSP,
VOID* pConnection,
DWORD dwConnectionSize,
LPCDPNAME pName,
DWORD dwFlags,
VOID* pvContext )
{
HRESULT hr;
LPDIRECTPLAY4 pDP = NULL;
VOID* pConnectionBuffer = NULL;
HWND hWndListBox = (HWND)pvContext;
LRESULT iIndex;
// Create a IDirectPlay object.
if ( FAILED( hr = CoCreateInstance( CLSID_DirectPlay, NULL,
CLSCTX_ALL, IID_IDirectPlay4A, (VOID**)&pDP ) ) )
return FALSE; // Error, stop enumerating
// Test the if the connection is available
// by attempting to initialize.
// the connection
if( FAILED( hr = pDP->InitializeConnection( pConnection, 0 ) ) )
{
SAFE_RELEASE( pDP );
return TRUE; // Unavailable connection, keep enumerating
}
// Don't need the IDirectPlay interface any more, so release it.
SAFE_RELEASE( pDP );
// Found a good connection, so put it in the listbox.
iIndex = SendMessage( hWndListBox, LB_ADDSTRING, 0,
(LPARAM)pName->lpszShortNameA );
if( iIndex == CB_ERR )
return FALSE; // Error, stop enumerating
pConnectionBuffer = new BYTE[ dwConnectionSize ];
if( pConnectionBuffer == NULL )
return FALSE; // Error, stop enumerating
// Store pointer to GUID in listbox.
memcpy( pConnectionBuffer, pConnection, dwConnectionSize );
SendMessage( hWndListBox, LB_SETITEMDATA, iIndex,
(LPARAM)pConnectionBuffer );
return TRUE; // Keep enumerating
}
Once the user selects which connection to use, you need to retrieve the connection data that comes back with the enumeration, and initialize the DirectPlay object with this data.
if ( FAILED( hr = g_pDP->InitializeConnection( pConnection, 0 ) ) )
return hr;
Next: Step 3: Join a Session