Step 2: Enumerating and Initializing the Service Providers

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:

lpDirectPlay3A->EnumConnections(&DPCHAT_GUID, DirectPlayEnumConnectionsCallback, hWnd, 0);

The second parameter in the EnumConnections method is a callback that enumerates service providers registered with DirectPlay. The following example shows one possible way of implementing this callback function:

BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(

LPCGUID lpguidSP, LPVOID lpConnection, DWORD dwConnectionSize,

LPCDPNAME lpName, DWORD dwFlags, LPVOID lpContext)

{

HWND hWnd = (HWND) lpContext;

LRESULT iIndex;

LPVOID lpConnectionBuffer;

// Store service provider name in combo box

iIndex = SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_ADDSTRING, 0,

(LPARAM) lpName->lpszShortNameA);

if (iIndex == CB_ERR)

goto FAILURE;

// make space for connection

lpConnectionBuffer = GlobalAllocPtr(GHND, dwConnectionSize);

if (lpConnectionBuffer == NULL)

goto FAILURE;

// Store pointer to connection in combo box

memcpy(lpConnectionBuffer, lpConnection, dwConnectionSize);

SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_SETITEMDATA, (WPARAM) iIndex,

(LPARAM) lpConnectionBuffer);

FAILURE:

return (TRUE);

}

Once the user selects which connection to use, the DirectPlay object must be initialized with the connection buffer associated with it.

hr = lpDirectPlay3A->InitializeConnection(lpConnection, 0);