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 / bellhop / dialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  21.6 KB  |  764 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1996-1997 Microsoft Corporation.  All Rights Reserved.
  4.  *
  5.  *  File:       dialog.cpp
  6.  *  Content:    Creates a dialog to query the user for connection settings
  7.  *                and establish a connection.
  8.  *
  9.  ***************************************************************************/
  10.  
  11. #include <windows.h>
  12. #include <windowsx.h>
  13. #include <cguid.h>
  14.  
  15. #include "bellhop.h"
  16. #include "resource.h"
  17.  
  18. // constants
  19. const DWORD MAXNAMELEN        = 200;        // max size of a session or player name
  20. const UINT    TIMERID            = 1;        // timer ID to use
  21. const UINT    TIMERINTERVAL    = 1000;        // timer interval
  22.  
  23. // prototypes
  24. BOOL CALLBACK    ConnectWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  25. BOOL CALLBACK    SecurityCredentialsWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  26.  
  27. HRESULT            CreateDirectPlayInterface(LPDIRECTPLAY3A *lplpDirectPlay3A );
  28. HRESULT            CreateDirectPlayLobbyInterface(LPDIRECTPLAYLOBBY2A *lplpDirectPlayLobby2A );
  29. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(LPCGUID lpguidSP, LPVOID lpConnection, DWORD dwSize, LPCDPNAME lpName, 
  30.                                                   DWORD dwFlags, LPVOID lpContext);
  31. HRESULT            DestroyDirectPlayInterface(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A);
  32. HRESULT            DestroyDirectPlayLobbyInterface(HWND hWnd, LPDIRECTPLAYLOBBY2A lpDirectPlayLobby2A);
  33. HRESULT JoinSession(HWND hWnd,
  34.                     LPDIRECTPLAY3A lpDirectPlay3A,
  35.                     LPDIRECTPLAYLOBBY2A lpDirectPlayLobby2A,
  36.                     LPGUID lpguidSessionInstance,
  37.                     DWORD    dwSessionFlags,
  38.                     LPSTR lpszPlayerName,
  39.                     DWORD    dwPlayerFlags,
  40.                     LPDPLAYINFO lpDPInfo);
  41.  
  42. HRESULT            EnumSessions(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A);
  43.  
  44. HRESULT            GetConnection(HWND hWnd,  int idCombo, LPVOID *lplpConnection);
  45. HRESULT            GetConnectionSPGuid(HWND hWnd, int idCombo, GUID *lpGuidSP);
  46. void            DeleteConnectionList(HWND hWnd);
  47. HRESULT            GetSessionInfo(HWND hWnd, LPGUID lpguidSessionInstance, LPDWORD lpdwFlags);
  48. void            SelectSessionInstance(HWND hWnd, LPGUID lpguidSessionInstance);
  49. void            DeleteSessionInstanceList(HWND hWnd);
  50. void            EnableDlgButton(HWND hDlg, int nIDDlgItem, BOOL bEnable);
  51.  
  52.  
  53. ///////////////////////////////////////////////////////////////////////////////////////
  54. HRESULT ConnectUsingDialog(HINSTANCE hInstance, LPDPLAYINFO lpDPInfo)
  55. {
  56.     // ask user for connection settings
  57.     if (DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_CONNECTDIALOG),
  58.                        NULL, (DLGPROC) ConnectWndProc, (LPARAM) lpDPInfo))
  59.     {
  60.         return (DP_OK);
  61.     }
  62.     else
  63.     {
  64.         return (DPERR_USERCANCEL);
  65.     }
  66. }
  67.  
  68.  
  69. ///////////////////////////////////////////////////////////////////////////////////////
  70. BOOL CALLBACK ConnectWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  71. {
  72.     static LPDPLAYINFO            lpDPInfo;
  73.     static LPDIRECTPLAY3A        lpDirectPlay3A;
  74.     static LPDIRECTPLAYLOBBY2A    lpDirectPlayLobby2A;
  75.     static UINT                    idTimer;
  76.     GUID                        guidSessionInstance;
  77.     char                        szPlayerName[MAXNAMELEN];
  78.     DWORD                        dwNameSize;
  79.     HRESULT                        hr;
  80.     LPVOID                        lpConnection = NULL;
  81.     ENUMCONNSTRUCT                enStruct;
  82.     DWORD                        dwSessionFlags;
  83.     DWORD                        dwPlayerFlags = NULL;
  84.  
  85.     switch(uMsg)
  86.     {
  87.     case WM_INITDIALOG:
  88.         // save the connection info pointer
  89.         lpDPInfo = (LPDPLAYINFO) lParam;
  90.         lpDirectPlay3A = NULL;
  91.         lpDirectPlayLobby2A = NULL;
  92.  
  93.         // Create an IDirectPlay3 interface
  94.         hr = CreateDirectPlayInterface(&lpDirectPlay3A);
  95.         if FAILED(hr)
  96.             goto SETUP_FAILURE;
  97.  
  98.         // Create an IDirectLobby2 interface
  99.         hr = CreateDirectPlayLobbyInterface(&lpDirectPlayLobby2A);
  100.         if FAILED(hr)
  101.             goto SETUP_FAILURE;
  102.  
  103.         // set first item in the connections combo box
  104.         SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_ADDSTRING, (WPARAM) 0, (LPARAM) "<Select a lobby provider>");
  105.         SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_SETITEMDATA, (WPARAM) 0, (LPARAM) 0);
  106.         SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_SETCURSEL, (WPARAM) 0, (LPARAM) 0);
  107.  
  108.         // put all the available connections in a combo box
  109.         enStruct.hWnd = hWnd;
  110.         enStruct.idCombo = IDC_SPCOMBO;
  111.  
  112.         IDirectPlay3_EnumConnections(lpDirectPlay3A, &BELLHOP_GUID, DirectPlayEnumConnectionsCallback,
  113.                 &enStruct, DPCONNECTION_DIRECTPLAYLOBBY);
  114.  
  115.         // setup initial button state
  116.         EnableDlgButton(hWnd, IDC_JOINBUTTON, FALSE);
  117.         EnableDlgButton(hWnd, IDC_SPECTATORBUTTON, FALSE);
  118.         break;
  119.  
  120.     SETUP_FAILURE:
  121.         ErrorBox("Could not create DirectPlay object because of error 0x%08X", hr);
  122.         EndDialog(hWnd, FALSE);
  123.         break;
  124.  
  125.     case WM_DESTROY:
  126.         // delete information stored along with the lists
  127.         DeleteConnectionList(hWnd);
  128.         DeleteSessionInstanceList(hWnd);
  129.         break;
  130.  
  131.     case WM_TIMER:
  132.         // refresh the session list
  133.         hr = EnumSessions(hWnd, lpDirectPlay3A);
  134.         break;
  135.  
  136.     case WM_COMMAND:
  137.  
  138.       switch(LOWORD(wParam))
  139.         {
  140.         case IDC_SPCOMBO:
  141.             switch (HIWORD(wParam))
  142.             {
  143.             case CBN_SELCHANGE:
  144.                 // service provider changed, so rebuild display and
  145.                 // delete any existing DirectPlay interface
  146.                 KillTimer(hWnd, idTimer ); 
  147.                 hr = DestroyDirectPlayInterface(hWnd, lpDirectPlay3A);
  148.                 lpDirectPlay3A = NULL;
  149.  
  150.                 // get pointer to the selected connection
  151.                 hr = GetConnection(hWnd, IDC_SPCOMBO, &lpConnection);
  152.                 if FAILED(hr)
  153.                     goto SP_FAILURE;
  154.  
  155.                 if (lpConnection)
  156.                 {
  157.                   // Create a new DPlay interface.
  158.                     hr = CreateDirectPlayInterface(&lpDirectPlay3A);
  159.  
  160.                     if ((FAILED(hr)) || (NULL == lpDirectPlay3A))
  161.                         goto SP_FAILURE;
  162.  
  163.                     // initialize the connection
  164.                     hr = IDirectPlay3_InitializeConnection(lpDirectPlay3A, lpConnection, 0);
  165.                     if FAILED(hr)
  166.                         goto SP_FAILURE;
  167.  
  168.                     // start enumerating the sessions
  169.                     hr = EnumSessions(hWnd, lpDirectPlay3A);
  170.                     if FAILED(hr)
  171.                         goto SP_FAILURE;
  172.  
  173.                     // set a timer to refresh the session list
  174.                     idTimer = SetTimer(hWnd, TIMERID, TIMERINTERVAL, NULL);
  175.                 }
  176.                 else
  177.                 {
  178.                     // They've selected the generic option "<Select a service provider>"
  179.                     EnableDlgButton(hWnd, IDC_JOINBUTTON, FALSE);
  180.                     EnableDlgButton(hWnd, IDC_SPECTATORBUTTON, FALSE);
  181.                 }
  182.                 break;
  183.             }
  184.             break;
  185.  
  186.         SP_FAILURE:
  187.             if (hr != DPERR_USERCANCEL)
  188.                 ErrorBox("Could not select service provider because of error 0x%08X", hr);
  189.             break;
  190.  
  191.  
  192.         case IDC_SPECTATORBUTTON:
  193.             // Joining as a spectator is the same as a regular join
  194.             // just with different flags.
  195.             dwPlayerFlags = DPPLAYER_SPECTATOR;
  196.             // Fall through to case IDC_JOINBUTTON:
  197.         case IDC_JOINBUTTON:
  198.  
  199.             // should have an interface by now
  200.             if (lpDirectPlay3A == NULL)
  201.                 break;
  202.  
  203.             KillTimer(hWnd, idTimer ); 
  204.             // get guid of selected session instance
  205.  
  206.             hr = GetSessionInfo(hWnd, &guidSessionInstance, &dwSessionFlags);
  207.             if FAILED(hr)
  208.                 goto JOIN_FAILURE;
  209.  
  210.             // use computer name for player name
  211.             dwNameSize = MAXNAMELEN;
  212.             if (!GetComputerName(szPlayerName, &dwNameSize))
  213.                 lstrcpy(szPlayerName, "unknown");
  214.             _strlwr(szPlayerName);
  215.  
  216.             // join this session
  217.             hr = JoinSession(    hWnd,
  218.                                 lpDirectPlay3A,
  219.                                  lpDirectPlayLobby2A, 
  220.                                  &guidSessionInstance,
  221.                                  dwSessionFlags,
  222.                                  szPlayerName, 
  223.                                  dwPlayerFlags,
  224.                                  lpDPInfo);
  225.  
  226.             if FAILED(hr)
  227.                 goto JOIN_FAILURE;
  228.  
  229.             // dismiss dialog if we succeeded in joining
  230.             EndDialog(hWnd, TRUE);
  231.             break;
  232.  
  233.         JOIN_FAILURE:
  234.             ErrorBox("Could not join session because of error 0x%08X", hr);
  235.             break;
  236.  
  237.  
  238.         case IDCANCEL:
  239.             // delete any interface created if cancelling
  240.             KillTimer(hWnd, idTimer ); 
  241.             hr = DestroyDirectPlayInterface(hWnd, lpDirectPlay3A);
  242.             lpDirectPlay3A = NULL;
  243.  
  244.             hr = DestroyDirectPlayLobbyInterface(hWnd, lpDirectPlayLobby2A);
  245.             lpDirectPlayLobby2A = NULL;
  246.  
  247.             EndDialog(hWnd, FALSE);
  248.             break;
  249.         }
  250.  
  251.         break;
  252.     }
  253.  
  254.     // Allow for default processing
  255.     return FALSE;
  256. }
  257.  
  258. ///////////////////////////////////////////////////////////////////////////////////////
  259. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(
  260.                         LPCGUID            lpguidSP,
  261.                         LPVOID            lpConnection,
  262.                         DWORD            dwSize,
  263.                         LPCDPNAME        lpName,
  264.                         DWORD            dwFlags,
  265.                         LPVOID            lpContext)
  266. {
  267.     LPENUMCONNSTRUCT    lp = (LPENUMCONNSTRUCT) lpContext;
  268.     LRESULT            iIndex;
  269.     LPCONNECTIONINFO    lpConnectionBuffer = NULL;
  270.  
  271.     // store service provider name in combo box
  272.     iIndex = SendDlgItemMessage(lp->hWnd, lp->idCombo, CB_ADDSTRING, 0, 
  273.                                     (LPARAM) lpName->lpszShortNameA);
  274.     if (iIndex == CB_ERR)
  275.         goto FAILURE;
  276.  
  277.     // make space for Connection Shortcut
  278.     lpConnectionBuffer = (LPCONNECTIONINFO) GlobalAllocPtr(GHND, dwSize+sizeof(CONNECTIONINFO));
  279.     if (lpConnectionBuffer == NULL)
  280.         goto FAILURE;
  281.  
  282.     // store pointer to GUID in combo box
  283.     memcpy(lpConnectionBuffer->Connection, lpConnection, dwSize);
  284.     lpConnectionBuffer->guidSP = *lpguidSP;
  285.     SendDlgItemMessage(lp->hWnd, lp->idCombo, CB_SETITEMDATA, (WPARAM) iIndex, 
  286.                                     (LPARAM) lpConnectionBuffer);
  287.  
  288. FAILURE:
  289.     return (TRUE);
  290. }
  291.  
  292.  
  293. ///////////////////////////////////////////////////////////////////////////////////////
  294. HRESULT CreateDirectPlayInterface( LPDIRECTPLAY3A *lplpDirectPlay3A )
  295. {
  296.     HRESULT                hr;
  297.     LPDIRECTPLAY3A        lpDirectPlay3A = NULL;
  298.  
  299.     // Create an IDirectPlay3 interface
  300.     hr = CoCreateInstance(    CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, 
  301.                             IID_IDirectPlay3A, (LPVOID*)&lpDirectPlay3A);
  302.  
  303.     // return interface created
  304.     *lplpDirectPlay3A = lpDirectPlay3A;
  305.  
  306.     return (hr);
  307. }
  308.  
  309. ///////////////////////////////////////////////////////////////////////////////////////
  310. HRESULT CreateDirectPlayLobbyInterface( LPDIRECTPLAYLOBBY2A *lplpDirectPlayLobby2A )
  311. {
  312.     HRESULT                hr;
  313.     LPDIRECTPLAYLOBBY2A        lpDirectPlayLobby2A = NULL;
  314.  
  315.     // Create an IDirectPlay3 interface
  316.     hr = CoCreateInstance(    CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, 
  317.                             IID_IDirectPlayLobby2A, (LPVOID*)&lpDirectPlayLobby2A);
  318.  
  319.     // return interface created
  320.     *lplpDirectPlayLobby2A = lpDirectPlayLobby2A;
  321.  
  322.     return (hr);
  323. }
  324.  
  325.  
  326. ///////////////////////////////////////////////////////////////////////////////////////
  327. HRESULT DestroyDirectPlayInterface(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A)
  328. {
  329.     HRESULT        hr = DP_OK;
  330.  
  331.     if (lpDirectPlay3A)
  332.     {
  333.         DeleteSessionInstanceList(hWnd);
  334.         EnableDlgButton(hWnd, IDC_JOINBUTTON, FALSE);
  335.         EnableDlgButton(hWnd, IDC_SPECTATORBUTTON, FALSE);
  336.  
  337.         hr = IDirectPlay3_Release(lpDirectPlay3A);
  338.     }
  339.  
  340.     return (hr);
  341. }
  342.  
  343. ///////////////////////////////////////////////////////////////////////////////////////
  344. HRESULT DestroyDirectPlayLobbyInterface(HWND hWnd, LPDIRECTPLAYLOBBY2A lpDirectPlayLobby2A)
  345. {
  346.     HRESULT        hr = DP_OK;
  347.  
  348.     if (lpDirectPlayLobby2A)
  349.     {
  350.         hr = lpDirectPlayLobby2A->Release();
  351.     }
  352.  
  353.     return (hr);
  354. }
  355.  
  356. ///////////////////////////////////////////////////////////////////////////////////////
  357. HRESULT JoinSession(HWND hWnd,
  358.                     LPDIRECTPLAY3A lpDirectPlay3A,
  359.                     LPDIRECTPLAYLOBBY2A lpDirectPlayLobby2A,
  360.                     LPGUID lpguidSessionInstance,
  361.                     DWORD    dwSessionFlags,
  362.                     LPSTR lpszPlayerName,
  363.                     DWORD    dwPlayerFlags,
  364.                     LPDPLAYINFO lpDPInfo)
  365. {
  366.     DPID                dpidPlayer;
  367.     DPNAME                dpName;
  368.     DPSESSIONDESC2        sessionDesc;
  369.     HRESULT                hr;
  370.  
  371.     // check for valid interface
  372.     if (lpDirectPlay3A == NULL)
  373.         return (DPERR_INVALIDOBJECT);
  374.  
  375.     // Spectator or regular player
  376.     lpDPInfo->dwPlayerFlags = dwPlayerFlags;
  377.  
  378.     // prepare a session description
  379.     ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  380.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  381.     sessionDesc.guidInstance = *lpguidSessionInstance;
  382.     sessionDesc.dwFlags = dwSessionFlags;
  383.  
  384.     if (DPSESSION_SECURESERVER & dwSessionFlags )
  385.     {
  386.         hr = IDirectPlay3_SecureOpen(    lpDirectPlay3A,
  387.                                         &sessionDesc,
  388.                                         DPOPEN_JOIN,
  389.                                         NULL,
  390.                                         NULL );
  391.  
  392.         if ( DPERR_LOGONDENIED == hr )
  393.         {
  394.  
  395.             // we need to collect security credentials
  396.             // and try again.
  397.  
  398.             if (DialogBoxParam( ghInstance, 
  399.                                 MAKEINTRESOURCE(IDD_SECURITYCREDENTIALSDIALOG), 
  400.                                 hWnd,
  401.                                 (DLGPROC) SecurityCredentialsWndProc, 
  402.                                 (LPARAM) &lpDPInfo))
  403.             {
  404.  
  405.                 DPCREDENTIALS dpcr;
  406.                 dpcr.dwSize = sizeof(DPCREDENTIALS);
  407.                 dpcr.dwFlags = 0;
  408.                 dpcr.lpszUsernameA = lpDPInfo->szSecureName;
  409.                 dpcr.lpszPasswordA = lpDPInfo->szSecurePassword;
  410.                 dpcr.lpszDomainA = lpDPInfo->szSecureDomain;
  411.  
  412.                 hr = IDirectPlay3_SecureOpen(    lpDirectPlay3A,
  413.                                                 &sessionDesc,
  414.                                                 DPOPEN_JOIN,
  415.                                                 NULL,
  416.                                                 &dpcr );
  417.                 if (FAILED(hr))
  418.                 {
  419.                     // Conceivably, we could cycle back and try to get credentials again
  420.                     // but in this sample, we'll just drop out on the error.
  421.                     goto OPEN_FAILURE;
  422.                 }
  423.  
  424.                 lpDPInfo->bSecureSession = TRUE;
  425.             }
  426.             else
  427.             {
  428.                 // abort. user clicked cancel.
  429.                 goto OPEN_FAILURE;
  430.             }
  431.         }
  432.     }
  433.     else
  434.     {
  435.         // Session does not require security
  436.         hr = IDirectPlay3_Open(lpDirectPlay3A, &sessionDesc, DPOPEN_JOIN);
  437.         if FAILED(hr)
  438.             goto OPEN_FAILURE;
  439.     }
  440.  
  441.     // fill out name structure
  442.     ZeroMemory(&dpName, sizeof(DPNAME));
  443.     dpName.dwSize = sizeof(DPNAME);
  444.     dpName.lpszShortNameA = lpszPlayerName;
  445.     dpName.lpszLongNameA = NULL;
  446.  
  447.     // create a player with this name
  448.     hr = IDirectPlay3_CreatePlayer(lpDirectPlay3A, &dpidPlayer, &dpName, 
  449.                             lpDPInfo->hPlayerEvent,
  450.                             NULL, 0, dwPlayerFlags );
  451.     if FAILED(hr)
  452.         goto CREATEPLAYER_FAILURE;
  453.  
  454.     // return connection info
  455.     lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  456.     lpDPInfo->lpDirectPlayLobby2A = lpDirectPlayLobby2A;
  457.  
  458.     lpDPInfo->dpidPlayer = dpidPlayer;
  459.     lpDPInfo->bIsHost = FALSE;
  460.  
  461.     return (DP_OK);
  462.  
  463. CREATEPLAYER_FAILURE:
  464. OPEN_FAILURE:
  465.     IDirectPlay3_Close(lpDirectPlay3A);
  466.     return (hr);
  467. }
  468.  
  469. ///////////////////////////////////////////////////////////////////////////////////////
  470. BOOL FAR PASCAL EnumSessionsCallback(
  471.                         LPCDPSESSIONDESC2    lpSessionDesc,
  472.                         LPDWORD                lpdwTimeOut,
  473.                         DWORD                dwFlags,
  474.                         LPVOID                lpContext)
  475. {
  476.     HWND                hWnd = (HWND) lpContext;
  477.     LONG                iIndex;
  478.     char                szBuffer[256];
  479.     LPSESSIONINFO    lpSessionInfo = NULL;
  480.  
  481.     // see if last session has been enumerated
  482.     if (dwFlags & DPESC_TIMEDOUT)
  483.         return (FALSE);                        
  484.  
  485.     wsprintf(    szBuffer, 
  486.                 (DPSESSION_SECURESERVER & lpSessionDesc->dwFlags ? "%s (SECURE)" : "%s" ),
  487.                 lpSessionDesc->lpszSessionNameA );
  488.  
  489.     // store session name in list
  490.     iIndex = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_ADDSTRING, 
  491.                                 (WPARAM) 0, (LPARAM) szBuffer);
  492.  
  493.     if (iIndex == LB_ERR)
  494.         goto FAILURE;
  495.  
  496.  
  497.     // make space for session instance guid
  498.     lpSessionInfo = (LPSESSIONINFO) GlobalAllocPtr( GHND, sizeof(SESSIONINFO) );
  499.     if (lpSessionInfo == NULL)
  500.         goto FAILURE;
  501.  
  502.     // Extract the data we need from the session description
  503.     lpSessionInfo->guidInstance = lpSessionDesc->guidInstance;
  504.     lpSessionInfo->dwFlags = lpSessionDesc->dwFlags;
  505.  
  506.     // store pointer to guid in list
  507.     SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_SETITEMDATA, (WPARAM) iIndex, (LPARAM) lpSessionInfo);
  508.  
  509. FAILURE:
  510.     return (TRUE);
  511. }
  512.  
  513. ///////////////////////////////////////////////////////////////////////////////////////
  514. HRESULT EnumSessions(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A)
  515. {
  516.     DPSESSIONDESC2    sessionDesc;
  517.     GUID            guidSessionInstance;
  518.     DWORD            dwFlags;
  519.     LONG            iIndex;
  520.     HRESULT            hr;
  521.  
  522.     // check for valid interface
  523.     if (lpDirectPlay3A == NULL)
  524.         return (DPERR_INVALIDOBJECT);
  525.  
  526.     // get guid of currently selected session
  527.     guidSessionInstance = GUID_NULL;
  528.     hr = GetSessionInfo(hWnd, &guidSessionInstance, &dwFlags);
  529.  
  530.     // delete existing session list
  531.     DeleteSessionInstanceList(hWnd);
  532.  
  533.     // add sessions to session list
  534.     ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  535.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  536.     sessionDesc.guidApplication = BELLHOP_GUID;
  537.  
  538.     hr = IDirectPlay3_EnumSessions(lpDirectPlay3A, &sessionDesc, 0, EnumSessionsCallback,
  539.                                       hWnd, DPENUMSESSIONS_AVAILABLE | DPENUMSESSIONS_ASYNC);
  540.  
  541.     // select the session that was previously selected
  542.     SelectSessionInstance(hWnd, &guidSessionInstance);
  543.  
  544.     // hilite "Join" button only if there are sessions to join
  545.     iIndex = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETCOUNT,
  546.                            (WPARAM) 0, (LPARAM) 0);
  547.  
  548.     EnableDlgButton(hWnd, IDC_JOINBUTTON, (iIndex > 0) ? TRUE : FALSE);
  549.     EnableDlgButton(hWnd, IDC_SPECTATORBUTTON, (iIndex > 0) ? TRUE : FALSE);
  550.  
  551.     return (hr);
  552. }
  553.  
  554. ///////////////////////////////////////////////////////////////////////////////////////
  555. HRESULT GetConnection(HWND hWnd, int idCombo, LPVOID *lplpConnection)
  556. {
  557.     LONG    iIndex;
  558.  
  559.     // get index of the item currently selected in the combobox
  560.     iIndex = SendDlgItemMessage(hWnd,  idCombo, CB_GETCURSEL,
  561.                                 (WPARAM) 0, (LPARAM) 0);
  562.     if (iIndex == CB_ERR)
  563.         return (DPERR_GENERIC);
  564.  
  565.     // get the pointer to the connection shortcut associated with
  566.     // the item
  567.     iIndex = SendDlgItemMessage(hWnd, idCombo, CB_GETITEMDATA,
  568.                                 (WPARAM) iIndex, (LPARAM) 0);
  569.  
  570.     if ((CB_ERR == iIndex) || ( NULL == iIndex ))
  571.         return (DPERR_GENERIC);
  572.  
  573.     *lplpConnection = &((LPCONNECTIONINFO) iIndex)->Connection;
  574.  
  575.     return (DP_OK);
  576. }
  577.  
  578. ///////////////////////////////////////////////////////////////////////////////////////
  579. HRESULT GetConnectionSPGuid(HWND hWnd, int idCombo, GUID *lpGuidSP)
  580. {
  581.     LONG    iIndex;
  582.  
  583.     // get index of the item currently selected in the combobox
  584.     iIndex = SendDlgItemMessage(hWnd,  idCombo, CB_GETCURSEL,
  585.                                 (WPARAM) 0, (LPARAM) 0);
  586.     if (iIndex == CB_ERR)
  587.         return (DPERR_GENERIC);
  588.  
  589.     // get the pointer to the connection shortcut associated with
  590.     // the item
  591.     iIndex = SendDlgItemMessage(hWnd, idCombo, CB_GETITEMDATA,
  592.                                 (WPARAM) iIndex, (LPARAM) 0);
  593.  
  594.     if ((iIndex == CB_ERR) || (iIndex == NULL ))
  595.         return (DPERR_GENERIC);
  596.  
  597.     *lpGuidSP = ((LPCONNECTIONINFO) iIndex)->guidSP;
  598.  
  599.     return (DP_OK);
  600. }
  601.  
  602. ///////////////////////////////////////////////////////////////////////////////////////
  603. void DeleteConnectionList(HWND hWnd)
  604. {
  605.     WPARAM    i;
  606.     LONG    lpData;
  607.     
  608.     // destroy the GUID's stored with each service provider name
  609.     i = 0;
  610.     while (TRUE)
  611.     {
  612.         // get data pointer stored with item
  613.         lpData = SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_GETITEMDATA,
  614.                                     (WPARAM) i, (LPARAM) 0);
  615.         if (lpData == CB_ERR)        // error getting data
  616.             break;
  617.  
  618.         if (lpData != 0)            // no data to delete
  619.             GlobalFreePtr((LPVOID) lpData);
  620.  
  621.         i += 1;
  622.     }
  623.  
  624.     // delete all items in combo box
  625.     SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_RESETCONTENT,
  626.                                 (WPARAM) 0, (LPARAM) 0);
  627. }
  628.  
  629. ///////////////////////////////////////////////////////////////////////////////////////
  630. HRESULT GetSessionInfo(HWND hWnd, LPGUID lpguidSessionInstance, LPDWORD lpdwFlags)
  631. {
  632.     LONG            iIndex;
  633.     LPSESSIONINFO    lp;
  634.  
  635.     // get guid for session
  636.     iIndex = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETCURSEL,
  637.                                 (WPARAM) 0, (LPARAM) 0);
  638.     if (iIndex == LB_ERR)
  639.         return (DPERR_GENERIC);
  640.  
  641.     iIndex = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  642.                                 (WPARAM) iIndex, (LPARAM) 0);
  643.     if ((iIndex == LB_ERR) || (iIndex == 0))
  644.         return (DPERR_GENERIC);
  645.  
  646.     lp = (LPSESSIONINFO) iIndex;
  647.     *lpguidSessionInstance = lp->guidInstance;
  648.     *lpdwFlags = lp->dwFlags;
  649.  
  650.     return (DP_OK);
  651. }
  652.  
  653. ///////////////////////////////////////////////////////////////////////////////////////
  654. void DeleteSessionInstanceList(HWND hWnd)
  655. {
  656.     WPARAM    i;
  657.     LONG    lpData;
  658.     
  659.     // destroy the GUID's stored with each session name
  660.     i = 0;
  661.     while (TRUE)
  662.     {
  663.         // get data pointer stored with item
  664.         lpData = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  665.                                     (WPARAM) i, (LPARAM) 0);
  666.         if (lpData == CB_ERR)        // error getting data
  667.             break;
  668.  
  669.         if (lpData == 0)            // no data to delete
  670.             continue;
  671.  
  672.         GlobalFreePtr((LPVOID) lpData);
  673.         i += 1;
  674.     }
  675.  
  676.     // delete all items in list
  677.     SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_RESETCONTENT,
  678.                                 (WPARAM) 0, (LPARAM) 0);
  679. }
  680.  
  681. ///////////////////////////////////////////////////////////////////////////////////////
  682. void SelectSessionInstance(HWND hWnd, LPGUID lpguidSessionInstance)
  683. {
  684.     WPARAM    i, iIndex;
  685.     LONG    lpData;
  686.     
  687.     // loop over the GUID's stored with each session name
  688.     // to find the one that matches what was passed in
  689.     i = 0;
  690.     iIndex = 0;
  691.     while (TRUE)
  692.     {
  693.         // get data pointer stored with item
  694.         lpData = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  695.                                     (WPARAM) i, (LPARAM) 0);
  696.         if (lpData == CB_ERR)        // error getting data
  697.             break;
  698.  
  699.         if (lpData == 0)            // no data to compare to
  700.             continue;
  701.  
  702.         // guid matches
  703.         if (IsEqualGUID(*lpguidSessionInstance, *((LPGUID) lpData)))
  704.         {
  705.             iIndex = i;                // store index of this string
  706.             break;
  707.         }
  708.  
  709.         i += 1;
  710.     }
  711.  
  712.     // select this item
  713.     SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_SETCURSEL, (WPARAM) iIndex, (LPARAM) 0);
  714. }
  715.  
  716. ///////////////////////////////////////////////////////////////////////////////////////
  717. void EnableDlgButton(HWND hDlg, int nIDDlgItem, BOOL bEnable)
  718. {
  719.     EnableWindow(GetDlgItem(hDlg, nIDDlgItem), bEnable);
  720. }
  721.  
  722. ///////////////////////////////////////////////////////////////////////////////////////
  723. BOOL CALLBACK SecurityCredentialsWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  724. {
  725.     static LPDPLAYINFO            lpDPInfo;
  726.     HWND                hwndName        = NULL,
  727.                         hwndPassword    = NULL,
  728.                         hwndDomain        = NULL;
  729.  
  730.  
  731.     switch(uMsg)
  732.     {
  733.     case WM_INITDIALOG:
  734.         lpDPInfo = (LPDPLAYINFO) lParam;
  735.         break;
  736.  
  737.     case WM_COMMAND:
  738.  
  739.       switch(LOWORD(wParam))
  740.         {
  741.         case IDOK:
  742.             hwndName = GetDlgItem( hWnd, IDC_SECURENAME );
  743.             hwndPassword = GetDlgItem( hWnd, IDC_SECUREPASSWORD );
  744.             hwndDomain = GetDlgItem( hWnd, IDC_SECUREDOMAIN );
  745.  
  746.             Edit_GetText(hwndName, lpDPInfo->szSecureName, 256);
  747.             Edit_GetText(hwndPassword, lpDPInfo->szSecurePassword, 256);
  748.             Edit_GetText(hwndDomain, lpDPInfo->szSecureDomain, 256);
  749.             EndDialog(hWnd, TRUE);
  750.             break;
  751.  
  752.         case IDCANCEL:
  753.             EndDialog(hWnd, FALSE);
  754.             break;
  755.         }
  756.  
  757.         break;
  758.     }
  759.  
  760.     // Allow for default processing
  761.     return FALSE;
  762. }
  763.  
  764.