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 / connset.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  7.9 KB  |  296 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1997 Microsoft Corporation.  All Rights Reserved.
  4.  *
  5.  *  File:       ConnSet.cpp
  6.  *  Content:    A dialog to set and get Group connection settings for a lobby
  7.  *
  8.  ***************************************************************************/
  9.  
  10. #include "Bellhop.h"
  11.  
  12. HRESULT    GetConnectionSPGuid(HWND hWnd, int idCombo, GUID *lpGuidSP);
  13. HRESULT    GetComboBoxGuid(HWND hWnd, LONG iDialogItem, LPGUID lpguidReturn);
  14. HRESULT SetGroupConnection(HWND hWnd, LPLOBBYGROUPCONTEXT lpContext);
  15. BOOL CALLBACK LobbyGroupWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  16. BOOL InitConnectionSettingsDialog(  HWND hWnd, LPLOBBYGROUPCONTEXT lpContext);
  17. BOOL FAR PASCAL    EnumApp(LPCDPLAPPINFO lpAppInfo, LPVOID lpContext, DWORD dwFlags);
  18. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(
  19.                         LPCGUID            lpguidSP,
  20.                         LPVOID            lpConnection,
  21.                         DWORD            dwSize,
  22.                         LPCDPNAME        lpName,
  23.                         DWORD            dwFlags,
  24.                         LPVOID            lpContext);
  25.  
  26.  
  27. ///////////////////////////////////////////////////////////////////////////////////////
  28. BOOL InitConnectionSettingsDialog( HWND hWnd, LPLOBBYGROUPCONTEXT lpContext)
  29. {
  30.     BOOL                bRet    = FALSE,
  31.                         bFound;
  32.     int                    iNum    = 0,
  33.                         i,
  34.                         lParam;
  35.     LPCONNECTIONINFO    lpci    = NULL;
  36.     LPGUID                lpGuid    = NULL;
  37.     LPDPLCONNECTION        lpdpconn = NULL;
  38.     HRESULT                hr;
  39.     ENUMCONNSTRUCT        enStruct;
  40.     DWORD                dwSize;
  41.  
  42.     HWND    hSPComboBox = GetDlgItem( hWnd, IDC_GROUPCONNECTIONSPCOMBO ); 
  43.     HWND    hAppComboBox = GetDlgItem( hWnd, IDC_APPCOMBO ); 
  44.  
  45.     // put all the DirectPlay applications in a combo box
  46.     lpContext->lpDPInfo->lpDirectPlayLobby2A->EnumLocalApplications(EnumApp, hWnd, 0);
  47.  
  48.     // put all the available connections in a combo box
  49.     enStruct.hWnd = hWnd;
  50.     enStruct.idCombo = IDC_GROUPCONNECTIONSPCOMBO;
  51.  
  52.     hr = IDirectPlay3_EnumConnections(    lpContext->lpDPInfo->lpDirectPlay3A, 
  53.                                         &BELLHOP_GUID, 
  54.                                         DirectPlayEnumConnectionsCallback,
  55.                                         &enStruct, DPCONNECTION_DIRECTPLAY );
  56.     if (FAILED(hr))
  57.         goto FAILURE;
  58.  
  59.     dwSize = 0;
  60.     hr = IDirectPlay3_GetGroupConnectionSettings( lpContext->lpDPInfo->lpDirectPlay3A, 
  61.                                                     0, lpContext->dpidRoom, lpdpconn, &dwSize );
  62.  
  63.     if (DPERR_BUFFERTOOSMALL != hr)
  64.         goto FAILURE;
  65.  
  66.     lpdpconn = (LPDPLCONNECTION) GlobalAllocPtr( GHND, dwSize );
  67.     if (NULL == lpdpconn)
  68.         goto FAILURE;
  69.  
  70.     hr = IDirectPlay3_GetGroupConnectionSettings( lpContext->lpDPInfo->lpDirectPlay3A, 
  71.                                                     0, lpContext->dpidRoom, lpdpconn, &dwSize );
  72.  
  73.     if (FAILED(hr))
  74.         goto FAILURE;
  75.  
  76.  
  77.     iNum = ComboBox_GetCount( hSPComboBox );
  78.  
  79.     bFound = FALSE;
  80.     for (i=0; i<iNum; i++)
  81.     {
  82.         lParam =  ComboBox_GetItemData( hSPComboBox, i );
  83.  
  84.         if ((lParam) && (CB_ERR != lParam))
  85.         {
  86.             lpci = (LPCONNECTIONINFO) lParam;
  87.             if (IsEqualGUID( lpci->guidSP, lpdpconn->guidSP ))
  88.             {
  89.                 bFound = TRUE;
  90.                 ComboBox_SetCurSel( hSPComboBox, i );
  91.                 break;
  92.             }
  93.             lpci = NULL;
  94.         }
  95.  
  96.         lParam = NULL;
  97.     }
  98.  
  99.     if (FALSE == bFound)
  100.     {
  101.         //No match.
  102.         ComboBox_AddString( hSPComboBox, "<Unknown Service Provider>");
  103.         ComboBox_SetItemData( hSPComboBox, 0, 0);
  104.         ComboBox_SetCurSel( hSPComboBox, 0 );
  105.     }
  106.  
  107.     iNum = ComboBox_GetCount( hAppComboBox );
  108.  
  109.     bFound = FALSE;
  110.     for (i=0; i<iNum; i++)
  111.     {
  112.         lParam = ComboBox_GetItemData( hAppComboBox, i );
  113.  
  114.         if ((lParam) && (CB_ERR != lParam))
  115.         {
  116.             lpGuid = (LPGUID) lParam;
  117.             if (IsEqualGUID( *lpGuid, lpdpconn->lpSessionDesc->guidApplication ))
  118.             {
  119.                 bFound = TRUE;
  120.                 ComboBox_SetCurSel( hAppComboBox, i );
  121.                 break;
  122.             }
  123.             lpGuid = NULL;
  124.         }
  125.  
  126.         lParam = NULL;
  127.  
  128.     }
  129.  
  130.     if (FALSE == bFound)
  131.     {
  132.         //No match.
  133.         ComboBox_AddString( hAppComboBox, "<Unknown Application>");
  134.         ComboBox_SetItemData( hAppComboBox, 0, 0);
  135.         ComboBox_SetCurSel( hAppComboBox, 0 );
  136.     }
  137.  
  138.     bRet = TRUE;
  139.  
  140.     // initialize max players
  141.     SetDlgItemInt(hWnd, IDC_MAXPLAYERSEDIT, 0, FALSE);
  142.     SetDlgItemText( hWnd, IDC_PASSWORDEDIT,lpdpconn->lpSessionDesc->lpszPasswordA );
  143.  
  144. FAILURE:
  145.     return bRet;
  146. }
  147.  
  148.  
  149. ///////////////////////////////////////////////////////////////////////////////////////
  150. HRESULT SetGroupConnection(HWND hWnd, LPLOBBYGROUPCONTEXT lpContext)
  151. {
  152.     CHAR                szPassword[MAXSTRLEN];
  153.     DWORD                dwMaxPlayers;
  154.     LPDPLCONNECTION        lp = NULL;
  155.     HRESULT                hr;
  156.     GUID                guidApplication,
  157.                         guidSP;
  158.     DWORD                dwSize = 0;
  159.  
  160.     //Pull the info from the dialog
  161.     GetDlgItemText(hWnd, IDC_PASSWORDEDIT, szPassword, MAXSTRLEN);
  162.     dwMaxPlayers = GetDlgItemInt(hWnd, IDC_MAXPLAYERSEDIT, NULL, FALSE);
  163.     hr = GetComboBoxGuid(hWnd, IDC_APPCOMBO, &guidApplication);
  164.  
  165.     if (FAILED( hr))
  166.     {
  167.         ErrorBox( "Please select a different application.", hr );
  168.         return hr;
  169.     }
  170.  
  171.     GetConnectionSPGuid(hWnd, IDC_GROUPCONNECTIONSPCOMBO, &guidSP);
  172.  
  173.     if (FAILED( hr))
  174.     {
  175.         ErrorBox( "Please select a different service provider.", hr );
  176.         return hr;
  177.     }
  178.  
  179.  
  180.     //Get the old connection settings.
  181.     hr = IDirectPlay3_GetGroupConnectionSettings(lpContext->lpDPInfo->lpDirectPlay3A,
  182.                                                  0, lpContext->dpidRoom, NULL, &dwSize );
  183.  
  184.     lp = (LPDPLCONNECTION) GlobalAllocPtr( GHND, dwSize );
  185.  
  186.     if (lp)
  187.     {
  188.         hr = IDirectPlay3_GetGroupConnectionSettings(lpContext->lpDPInfo->lpDirectPlay3A,
  189.                                                      0, lpContext->dpidRoom, (LPVOID) lp, &dwSize );
  190.  
  191.         lp->lpSessionDesc->dwMaxPlayers = dwMaxPlayers;
  192.         lp->lpSessionDesc->lpszPasswordA = szPassword;
  193.         lp->lpSessionDesc->guidApplication = guidApplication;
  194.         lp->guidSP = guidSP;
  195.  
  196.         hr = IDirectPlay3_SetGroupConnectionSettings(lpContext->lpDPInfo->lpDirectPlay3A,
  197.                                                      0, lpContext->dpidRoom, lp );
  198.     }
  199.     else
  200.     {
  201.         hr = DPERR_OUTOFMEMORY;
  202.     }
  203.  
  204.     return (hr);
  205. }
  206.  
  207.  
  208. ///////////////////////////////////////////////////////////////////////////////////////
  209. BOOL CALLBACK ConnectionSettingsDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  210. {
  211.     LPLOBBYGROUPCONTEXT    lpContext = (LPLOBBYGROUPCONTEXT) GetWindowLong(hWnd, DWL_USER);
  212.     HRESULT    hr;
  213.  
  214.     switch(uMsg)
  215.     {
  216.         case WM_INITDIALOG:
  217.  
  218.             // context passed in lParam
  219.             lpContext = (LPLOBBYGROUPCONTEXT) lParam;
  220.  
  221.             // save the globals with the window
  222.             SetWindowLong(hWnd, DWL_USER, (LONG) lpContext);
  223.             InitConnectionSettingsDialog( hWnd, lpContext );  
  224.             break;
  225.  
  226.         case WM_DESTROY:
  227.             {
  228.                 WPARAM    index;
  229.                 LRESULT    lpData;
  230.  
  231.                 // destroy the GUID's stored with each app name
  232.                 index = 0;
  233.                 while (TRUE)
  234.                 {
  235.                     lpData = SendDlgItemMessage(hWnd, IDC_APPCOMBO, CB_GETITEMDATA, (WPARAM) index, 0);
  236.                     if ((lpData == CB_ERR) || (lpData == 0))
  237.                         break;
  238.  
  239.                     GlobalFreePtr((LPVOID) lpData);
  240.                     index += 1;
  241.                 }
  242.  
  243.                 // destroy the connection info in the combo box.
  244.                 index = 0;
  245.                 while (TRUE)
  246.                 {
  247.                     lpData = SendDlgItemMessage(hWnd, IDC_GROUPCONNECTIONSPCOMBO, CB_GETITEMDATA, (WPARAM) index, 0);
  248.                     if ((lpData == CB_ERR) || (lpData == 0))
  249.                         break;
  250.  
  251.                     GlobalFreePtr((LPVOID) lpData);
  252.                     index += 1;
  253.                 }
  254.             }
  255.             break;
  256.  
  257.         case WM_COMMAND:
  258.             switch(LOWORD(wParam))
  259.             {
  260.                 case IDOK:
  261.                     // save changes they made
  262.                     hr = SetGroupConnection( hWnd, lpContext);
  263.  
  264.                     if (SUCCEEDED(hr))
  265.                     {
  266.                         // Return success
  267.                         EndDialog(hWnd, TRUE);
  268.                     }
  269.                     break;
  270.  
  271.                 case IDCANCEL:
  272.                     // Return failure
  273.                     EndDialog(hWnd, FALSE);
  274.  
  275.                     break;
  276.  
  277.                 case IDC_STAGINGAREA:
  278.                     {
  279.                         int i = SendDlgItemMessage( hWnd, IDC_STAGINGAREA, BM_GETCHECK, 0, 0 );
  280.                         EnableWindow( GetDlgItem( hWnd, IDC_PASSWORDEDIT ), (BST_CHECKED==i));
  281.                         EnableWindow( GetDlgItem( hWnd, IDC_APPCOMBO ), (BST_CHECKED==i));
  282.                         EnableWindow( GetDlgItem( hWnd, IDC_MAXPLAYERSEDIT ), (BST_CHECKED==i));
  283.                         EnableWindow( GetDlgItem( hWnd, IDC_GROUPCONNECTIONSPCOMBO ), (BST_CHECKED==i));
  284.                     }
  285.                     break;
  286.  
  287.             }
  288.  
  289.             break;
  290.     }
  291.  
  292.     // Allow for default processing
  293.     return FALSE;
  294. }
  295.  
  296.