home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / dpslots / lobby.cpp < prev    next >
C/C++ Source or Header  |  1997-07-14  |  3KB  |  119 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1996-1997 Microsoft Corporation.  All Rights Reserved.
  4.  *
  5.  *  File:       lobby.cpp
  6.  *  Content:    Uses information from the lobby to establish a connection.
  7.  *
  8.  ***************************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <dplobby.h>
  13.  
  14. #include "dpslots.h"
  15.  
  16. HRESULT ConnectUsingLobby(LPDPLAYINFO lpDPInfo)
  17. {
  18.     LPDIRECTPLAY2A        lpDirectPlay2A = NULL;
  19.     LPDIRECTPLAY3A        lpDirectPlay3A = NULL;
  20.     LPDIRECTPLAYLOBBYA    lpDirectPlayLobbyA = NULL;
  21.     LPDPLCONNECTION        lpConnectionSettings = NULL;
  22.     DPID                dpidPlayer;
  23.     DWORD                dwSize;
  24.     BOOL                bIsHost;
  25.     DWORD                dwPlayerFlags;
  26.     HRESULT                hr;
  27.  
  28.     // get an ANSI DirectPlay lobby interface
  29.     hr = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, 
  30.                           IID_IDirectPlayLobbyA, (LPVOID*)&lpDirectPlayLobbyA);
  31.     if FAILED(hr)
  32.         goto FAILURE;
  33.  
  34.     // get connection settings from the lobby
  35.     // if this routine returns DPERR_NOTLOBBIED, then a lobby did not
  36.     // launch this application and the user needs to configure the connection.
  37.  
  38.     // pass in a NULL pointer to just get the size of the connection setttings
  39.     hr = lpDirectPlayLobbyA->GetConnectionSettings(0, NULL, &dwSize);
  40.     if (DPERR_BUFFERTOOSMALL != hr)
  41.         goto FAILURE;
  42.  
  43.     // allocate memory for the connection setttings
  44.     lpConnectionSettings = (LPDPLCONNECTION) GlobalAllocPtr(GHND, dwSize);
  45.     if (NULL == lpConnectionSettings)
  46.     {
  47.         hr = DPERR_OUTOFMEMORY;
  48.         goto FAILURE;
  49.     }
  50.  
  51.     // get the connection settings
  52.     hr = lpDirectPlayLobbyA->GetConnectionSettings(0, lpConnectionSettings, &dwSize);
  53.     if FAILED(hr)
  54.         goto FAILURE;
  55.  
  56.     // see if we are the host or not
  57.     if (lpConnectionSettings->dwFlags & DPLCONNECTION_CREATESESSION)
  58.     {
  59.         lpConnectionSettings->lpSessionDesc->dwFlags = NONSECURESESSIONFLAGS;
  60.         bIsHost = TRUE;
  61.     }
  62.     else
  63.     {
  64.         bIsHost = FALSE;
  65.     }
  66.  
  67.     // store the updated connection settings
  68.     hr = lpDirectPlayLobbyA->SetConnectionSettings(0, 0, lpConnectionSettings);
  69.     if FAILED(hr)
  70.         goto FAILURE;
  71.  
  72.     // connect to the session - returns an ANSI IDirectPlay2A interface
  73.     hr = lpDirectPlayLobbyA->Connect(0, &lpDirectPlay2A, NULL);
  74.     if FAILED(hr)
  75.         goto FAILURE;
  76.  
  77.     // Obtain an IDirectPlay3A interface, the IDirectPlay2A interface will
  78.     // be released at the end of the function
  79.     hr = lpDirectPlay2A->QueryInterface(IID_IDirectPlay3A, (LPVOID *) &lpDirectPlay3A);
  80.     if FAILED(hr)
  81.         goto FAILURE;
  82.  
  83.     // if we are hosting then we need to create a server player
  84.     if (bIsHost)
  85.         dwPlayerFlags = SERVERPLAYERFLAGS;
  86.     else
  87.         dwPlayerFlags = CLIENTPLAYERFLAGS;
  88.  
  89.     // create a player with the name returned in the connection settings
  90.     hr = lpDirectPlay3A->CreatePlayer(&dpidPlayer,
  91.                             lpConnectionSettings->lpPlayerName, 
  92.                             lpDPInfo->hPlayerEvent, NULL, 0, dwPlayerFlags);
  93.     if FAILED(hr)
  94.         goto FAILURE;
  95.  
  96.     // return connection info
  97.     lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  98.     lpDPInfo->dpidPlayer = dpidPlayer;
  99.     lpDPInfo->bIsHost = bIsHost;
  100.     lpDPInfo->bIsSecure = FALSE;
  101.  
  102.     lpDirectPlay3A = NULL;    // set to NULL here so it won't release below
  103.  
  104. FAILURE:
  105.     if (lpDirectPlay2A)
  106.         lpDirectPlay2A->Release();
  107.  
  108.     if (lpDirectPlay3A)
  109.         lpDirectPlay3A->Release();
  110.  
  111.     if (lpDirectPlayLobbyA)
  112.         lpDirectPlayLobbyA->Release();
  113.  
  114.     if (lpConnectionSettings)
  115.         GlobalFreePtr(lpConnectionSettings);
  116.  
  117.     return (hr);
  118. }
  119.