Platform SDK: DirectX

Step 2: Get Connection Settings

In this step you will initialize DirectPlay in your application and determine whether the application was launched from a lobby. If it was, you will retrieve connection settings immediately; otherwise you will wait for them.

First, create the DirectPlayLobby3 object, using the initialized DirectX7 object objDX:

Public Function InitDPlay() As Boolean
  
  InitDPlay = True
  
  On Local Error GoTo FAILED
  Set objDPLobby = objDX.DirectPlayLobbyCreate

Then call DirectPlayLobby3.GetConnectionSettings. If the application was launched in response to a call to DirectPlayLobby3.RunApplication in the lobby client, GetConnectionSettings should succeed. Otherwise it should return DPERR_NOTLOBBIED.

  On Local Error Resume Next
  Set objDPLConnection = objDPLobby.GetConnectionSettings(0)
  
  ' The app was launched by a lobby.
  If Err.Number = 0 Then GoTo GOTCONNECTION
  

If the application was not launched by a lobby, at this point you might want to give the user the option of starting a single-player game, or of connecting to a session through an internal lobby client. The sample application instead waits for a lobby connection. First it enters wait mode by calling DirectPlayLobby3.WaitForConnectionSettings. This step simply ensures that RunApplication will not launch another instance of the program but will make connection settings available to this one when the user starts or joins an open application session in the lobby client.

  On Local Error GoTo FAILED
  Call objDPLobby.WaitForConnectionSettings(DPLWAIT_DEFAULT)

The sample application now shows a dialog box that remains active until the connection is established or the user cancels:

  frmWaiting.Show
  Call WaitForConnectionMessage

This is where the application actually waits. The WaitForConnectionMessage function blocks until it receives a DPLSYS_NEWCONNECTIONSETTINGS message from the lobby, or until the user cancels:

Public Sub WaitForConnectionMessage()
  Dim msg As DirectPlayMessage
  Dim flags As Long
  Dim MsgType As Long
 
  On Local Error Resume Next
  gGotConnection = False
  Do While DoEvents
      Set msg = objDPLobby.ReceiveLobbyMessage(0, flags)
      If Not (msg Is Nothing) Then
        MsgType = msg.ReadLong
        If MsgType = DPLSYS_NEWCONNECTIONSETTINGS Then
          gGotConnection = True
          Exit Do
        End If
      End If
  Loop
  Exit Sub
End Sub

Note that in the sample application, this function returns False when the user cancels out of the dialog, because there are no other visible forms and DoEvents returns False. An application with another visible form would have to use some other means of breaking out of the loop in the event of a user cancel.

Back in the calling procedure, if DPLSYS_NEWCONNECTIONSETTINGS was received, the status dialog is closed and the DirectPlayLobbyConnection object is retrieved:

  If gGotConnection Then
    frmWaiting.Hide
    Set objDPLConnection = objDPLobby.GetConnectionSettings(0)
  Else
    GoTo FAILED  ' User cancelled
  End If

Next: Step 3: Join the Session