Platform SDK: DirectX

Step 1: Initialize DirectPlay and Enumerate Providers

[C++]

This tutorial pertains only to applications written in Visual Basic. See DirectPlay C/C++ Tutorials

[Visual Basic]

The first step toward implementing DirectPlay in any application is to create a DirectPlay4 object and enumerate the available connections, or service providers. After some global declarations, the Memory sample takes these steps in the InitDPlay procedure:

Public gObjDX As New DirectX7
Public gObjDPlay As DirectPlay4
Public gObjEnumConnections As DirectPlayEnumConnections
 
Public Sub InitDPlay()
 
  On Error GoTo FAILED
  Set gObjDPlay = gObjDX.DirectPlayCreate("")
  Set gObjEnumConnections = gObjDPlay.GetDPEnumConnections( _
          "", DPCONNECTION_DIRECTPLAY)
  Exit Sub
  
FAILED:
  MsgBox ("Failed to initialize DirectPlay.")
  End
End Sub

The call to DirectPlay4.GetDPEnumConnections builds a collection of service providers. An empty string is passed in as the guid parameter. You would pass the application GUID only if you were looking for a service provider specifically designed for your application. The DP_CONNECTION_DIRECTPLAY flag indicates that the application is not interested in lobby providers.

The InitDPlay procedure is called from the InitConnectionList function, which then goes on to populate a list box with the available service providers. The complete function follows:

Public Function InitConnectionList() As Boolean

  Dim NumConnections As Long
  Dim strName As String
  Dim X As Long
 
  Call InitDPlay     ' Program aborts on failure
 
  lstConnections.Clear
  
  On Error GoTo FAILED
  NumConnections = gObjEnumConnections.GetCount
  For X = 1 To NumConnections
    strName = gObjEnumConnections.GetName(X)
    Call lstConnections.AddItem(strName)
  Next X 
 
  ' Initialize selection
  lstConnections.ListIndex = 0
  InitConnectionList = True
  Exit Function
  
  ' Error handlers
FAILED:
  InitConnectionList = False
  Exit Function
 
End Function

Note that the DirectPlayEnumConnections collection is 1-based, like other enumeration collections in DirectX for Visual Basic.

Next: Step 2: Initialize the Connection and Enumerate Sessions