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 / dpchat / dialog.cpp next >
C/C++ Source or Header  |  1997-07-14  |  17KB  |  621 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. #define INITGUID
  12. #include <windows.h>
  13. #include <windowsx.h>
  14. #include <cguid.h>
  15.  
  16. #include "dpchat.h"
  17. #include "resource.h"
  18.  
  19. // constants
  20. const DWORD MAXNAMELEN        = 200;        // max size of a session or player name
  21. const UINT    TIMERID            = 1;        // timer ID to use
  22. const UINT    TIMERINTERVAL    = 1000;        // timer interval
  23.  
  24. // prototypes
  25. BOOL CALLBACK    ConnectWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  26.  
  27. HRESULT            CreateDirectPlayInterface(LPDIRECTPLAY3A *lplpDirectPlay3A );
  28. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(LPCGUID lpguidSP,
  29.                             LPVOID lpConnection, DWORD dwConnectionSize,
  30.                             LPCDPNAME lpName, DWORD dwFlags, LPVOID lpContext);
  31. HRESULT            DestroyDirectPlayInterface(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A);
  32. HRESULT            HostSession(LPDIRECTPLAY3A lpDirectPlay3A,
  33.                             LPSTR lpszSessionName, LPSTR lpszPlayerName,
  34.                             LPDPLAYINFO lpDPInfo);
  35. HRESULT            JoinSession(LPDIRECTPLAY3A lpDirectPlay3A,
  36.                             LPGUID lpguidSessionInstance, LPSTR lpszPlayerName,
  37.                             LPDPLAYINFO lpDPInfo);
  38. HRESULT            EnumSessions(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A);
  39.  
  40. HRESULT            GetConnection(HWND hWnd, LPVOID *lplpConnection);
  41. void            DeleteConnectionList(HWND hWnd);
  42. HRESULT            GetSessionInstanceGuid(HWND hWnd, LPGUID lpguidSessionInstance);
  43. void            SelectSessionInstance(HWND hWnd, LPGUID lpguidSessionInstance);
  44. void            DeleteSessionInstanceList(HWND hWnd);
  45. void            EnableDlgButton(HWND hDlg, int nIDDlgItem, BOOL bEnable);
  46.  
  47. HRESULT ConnectUsingDialog(HINSTANCE hInstance, LPDPLAYINFO lpDPInfo)
  48. {
  49.     // ask user for connection settings
  50.     if (DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_CONNECTDIALOG),
  51.                        NULL, (DLGPROC) ConnectWndProc, (LPARAM) lpDPInfo))
  52.     {
  53.         return (DP_OK);
  54.     }
  55.     else
  56.     {
  57.         return (DPERR_USERCANCEL);
  58.     }
  59. }
  60.  
  61. BOOL CALLBACK ConnectWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  62. {
  63.     static LPDPLAYINFO        lpDPInfo;
  64.     static LPDIRECTPLAY3A    lpDirectPlay3A;
  65.     static UINT                idTimer;
  66.     GUID                    guidSessionInstance;
  67.     char                    szSessionName[MAXNAMELEN];
  68.     char                    szPlayerName[MAXNAMELEN];
  69.     DWORD                    dwNameSize;
  70.     HRESULT                    hr;
  71.     LPVOID                    lpConnection = NULL;
  72.  
  73.     switch(uMsg)
  74.     {
  75.     case WM_INITDIALOG:
  76.         // save the connection info pointer
  77.         lpDPInfo = (LPDPLAYINFO) lParam;
  78.         lpDirectPlay3A = NULL;
  79.  
  80.         // Create an IDirectPlay3 interface
  81.         hr = CreateDirectPlayInterface(&lpDirectPlay3A);
  82.         if (FAILED(hr))
  83.             goto SETUP_FAILURE;
  84.  
  85.         // set first item in the connections combo box
  86.         SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_ADDSTRING, (WPARAM) 0, (LPARAM) "<Select a service provider>");
  87.         SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_SETITEMDATA, (WPARAM) 0, (LPARAM) 0);
  88.         SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_SETCURSEL, (WPARAM) 0, (LPARAM) 0);
  89.  
  90.         // put all the available connections in a combo box
  91.         lpDirectPlay3A->EnumConnections(&DPCHAT_GUID, DirectPlayEnumConnectionsCallback, hWnd, 0);
  92.  
  93.         // setup initial button state
  94.         EnableDlgButton(hWnd, IDC_HOSTBUTTON, FALSE);
  95.         EnableDlgButton(hWnd, IDC_JOINBUTTON, FALSE);
  96.         break;
  97.  
  98.     SETUP_FAILURE:
  99.         MessageBox(NULL, "This application requires DirectX 5 or later.", NULL, MB_OK);
  100.         EndDialog(hWnd, FALSE);
  101.         break;
  102.  
  103.     case WM_DESTROY:
  104.         // delete information stored along with the lists
  105.         DeleteConnectionList(hWnd);
  106.         DeleteSessionInstanceList(hWnd);
  107.         break;
  108.  
  109.     case WM_TIMER:
  110.         // refresh the session list
  111.         hr = EnumSessions(hWnd, lpDirectPlay3A);
  112.         break;
  113.  
  114.     case WM_COMMAND:
  115.         switch(LOWORD(wParam))
  116.         {
  117.         case IDC_SPCOMBO:
  118.             switch (HIWORD(wParam))
  119.             {
  120.             case CBN_SELCHANGE:
  121.                 // service provider changed, so rebuild display and
  122.                 // delete any existing DirectPlay interface
  123.                 KillTimer(hWnd, idTimer ); 
  124.                 hr = DestroyDirectPlayInterface(hWnd, lpDirectPlay3A);
  125.                 lpDirectPlay3A = NULL;
  126.  
  127.                 // get pointer to the selected connection
  128.                 hr = GetConnection(hWnd, &lpConnection);
  129.                 if FAILED(hr)
  130.                     goto SP_FAILURE;
  131.  
  132.                 if (lpConnection)
  133.                 {
  134.                     /*
  135.                      * Create a new DPlay interface.
  136.                      */
  137.  
  138.                     hr = CreateDirectPlayInterface( &lpDirectPlay3A );
  139.  
  140.                     if ((FAILED(hr)) || (NULL == lpDirectPlay3A))
  141.                         goto SP_FAILURE;
  142.  
  143.                     // initialize the connection
  144.                     hr = lpDirectPlay3A->InitializeConnection(lpConnection, 0);
  145.                     if FAILED(hr)
  146.                         goto SP_FAILURE;
  147.  
  148.                     // OK to host now
  149.                     EnableDlgButton(hWnd, IDC_HOSTBUTTON, TRUE);
  150.  
  151.                     // start enumerating the sessions
  152.                     hr = EnumSessions(hWnd, lpDirectPlay3A);
  153.                     if FAILED(hr)
  154.                         goto SP_FAILURE;
  155.  
  156.                     // set a timer to refresh the session list
  157.                     idTimer = SetTimer(hWnd, TIMERID, TIMERINTERVAL, NULL);
  158.                 }
  159.                 else
  160.                 {
  161.                     // They've selected the generic option "<Select a service provider>"
  162.                     EnableDlgButton(hWnd, IDC_HOSTBUTTON, FALSE);
  163.                     EnableDlgButton(hWnd, IDC_JOINBUTTON, FALSE);
  164.                 }
  165.                 break;
  166.             }
  167.             break;
  168.  
  169.         SP_FAILURE:
  170.             if (hr != DPERR_USERCANCEL)
  171.                 ErrorBox("Could not select service provider because of error 0x%08X", hr);
  172.             break;
  173.  
  174.  
  175.         case IDC_HOSTBUTTON:
  176.                 // should have an interface by now
  177.             if (lpDirectPlay3A == NULL)
  178.                 break;
  179.  
  180.             KillTimer(hWnd, idTimer ); 
  181.             // use computer name for session name
  182.             dwNameSize = MAXNAMELEN;
  183.             if (!GetComputerName(szSessionName, &dwNameSize))
  184.                 lstrcpy(szSessionName, "Session");
  185.  
  186.             // use user name for player name
  187.             dwNameSize = MAXNAMELEN;
  188.             if (!GetUserName(szPlayerName, &dwNameSize))
  189.                 lstrcpy(szPlayerName, "unknown");
  190.  
  191.             // host a new session on this service provider
  192.             hr = HostSession(lpDirectPlay3A, szSessionName, szPlayerName, lpDPInfo);
  193.             if FAILED(hr)
  194.                 goto HOST_FAILURE;
  195.  
  196.             // dismiss dialog if we succeeded in hosting
  197.             EndDialog(hWnd, TRUE);
  198.             break;
  199.  
  200.         HOST_FAILURE:
  201.             ErrorBox("Could not host session because of error 0x%08X", hr);
  202.             break;
  203.  
  204.         case IDC_JOINBUTTON:
  205.  
  206.             // should have an interface by now
  207.             if (lpDirectPlay3A == NULL)
  208.                 break;
  209.  
  210.             KillTimer(hWnd, idTimer ); 
  211.             // get guid of selected session instance
  212.             hr = GetSessionInstanceGuid(hWnd, &guidSessionInstance);
  213.             if FAILED(hr)
  214.                 goto JOIN_FAILURE;
  215.  
  216.             // use user name for player name
  217.             dwNameSize = MAXNAMELEN;
  218.             if (!GetUserName(szPlayerName, &dwNameSize))
  219.                 lstrcpy(szPlayerName, "unknown");
  220.  
  221.             // join this session
  222.             hr = JoinSession(lpDirectPlay3A, &guidSessionInstance, szPlayerName, lpDPInfo);
  223.  
  224.             if FAILED(hr)
  225.                 goto JOIN_FAILURE;
  226.  
  227.             // dismiss dialog if we succeeded in joining
  228.             EndDialog(hWnd, TRUE);
  229.             break;
  230.  
  231.         JOIN_FAILURE:
  232.             ErrorBox("Could not join session because of error 0x%08X", hr);
  233.             break;
  234.  
  235.  
  236.         case IDCANCEL:
  237.             KillTimer(hWnd, idTimer ); 
  238.             // delete any interface created if cancelling
  239.             hr = DestroyDirectPlayInterface(hWnd, lpDirectPlay3A);
  240.             lpDirectPlay3A = NULL;
  241.  
  242.             EndDialog(hWnd, FALSE);
  243.             break;
  244.         }
  245.  
  246.         break;
  247.     }
  248.  
  249.     // Allow for default processing
  250.     return FALSE;
  251. }
  252.  
  253. BOOL FAR PASCAL DirectPlayEnumConnectionsCallback(
  254.                         LPCGUID     lpguidSP,
  255.                         LPVOID        lpConnection,
  256.                         DWORD        dwConnectionSize,
  257.                         LPCDPNAME   lpName,
  258.                         DWORD         dwFlags,
  259.                         LPVOID         lpContext)
  260. {
  261.  
  262.     HWND            hWnd = (HWND) lpContext;
  263.     LRESULT            iIndex;
  264.     LPVOID            lpConnectionBuffer;
  265.  
  266.     // store service provider name in combo box
  267.     iIndex = SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_ADDSTRING, 0, 
  268.                                     (LPARAM) lpName->lpszShortNameA);
  269.     if (iIndex == CB_ERR)
  270.         goto FAILURE;
  271.  
  272.     // make space for connection shortcut
  273.     lpConnectionBuffer = GlobalAllocPtr(GHND, dwConnectionSize);
  274.     if (lpConnectionBuffer == NULL)
  275.         goto FAILURE;
  276.  
  277.     // store pointer to connection shortcut in combo box
  278.     memcpy(lpConnectionBuffer, lpConnection, dwConnectionSize);
  279.     SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_SETITEMDATA, (WPARAM) iIndex, 
  280.                                     (LPARAM) lpConnectionBuffer);
  281.  
  282. FAILURE:
  283.     return (TRUE);
  284. }
  285.  
  286.  
  287. HRESULT CreateDirectPlayInterface( LPDIRECTPLAY3A *lplpDirectPlay3A )
  288. {
  289.     HRESULT                hr;
  290.     LPDIRECTPLAY3A        lpDirectPlay3A = NULL;
  291.  
  292.     // Create an IDirectPlay3 interface
  293.     hr = CoCreateInstance(    CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, 
  294.                             IID_IDirectPlay3A, (LPVOID*)&lpDirectPlay3A);
  295.  
  296.     // return interface created
  297.     *lplpDirectPlay3A = lpDirectPlay3A;
  298.  
  299.     return (hr);
  300. }
  301.  
  302.  
  303. HRESULT DestroyDirectPlayInterface(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A)
  304. {
  305.     HRESULT        hr = DP_OK;
  306.  
  307.     if (lpDirectPlay3A)
  308.     {
  309.         DeleteSessionInstanceList(hWnd);
  310.         EnableDlgButton(hWnd, IDC_JOINBUTTON, FALSE);
  311.  
  312.         hr = lpDirectPlay3A->Release();
  313.     }
  314.  
  315.     return (hr);
  316. }
  317.  
  318. HRESULT HostSession(LPDIRECTPLAY3A lpDirectPlay3A,
  319.                     LPSTR lpszSessionName, LPSTR lpszPlayerName,
  320.                     LPDPLAYINFO lpDPInfo)
  321. {
  322.     DPID                dpidPlayer;
  323.     DPNAME                dpName;
  324.     DPSESSIONDESC2        sessionDesc;
  325.     HRESULT                hr;
  326.  
  327.     // check for valid interface
  328.     if (lpDirectPlay3A == NULL)
  329.         return (DPERR_INVALIDOBJECT);
  330.  
  331.     // host a new session
  332.     ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  333.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  334.     sessionDesc.dwFlags = DPSESSION_MIGRATEHOST | DPSESSION_KEEPALIVE;
  335.     sessionDesc.guidApplication = DPCHAT_GUID;
  336.     sessionDesc.dwMaxPlayers = MAXPLAYERS;
  337.     sessionDesc.lpszSessionNameA = lpszSessionName;
  338.  
  339.     hr = lpDirectPlay3A->Open(&sessionDesc, DPOPEN_CREATE);
  340.     if FAILED(hr)
  341.         goto OPEN_FAILURE;
  342.  
  343.     // fill out name structure
  344.     ZeroMemory(&dpName, sizeof(DPNAME));
  345.     dpName.dwSize = sizeof(DPNAME);
  346.     dpName.lpszShortNameA = lpszPlayerName;
  347.     dpName.lpszLongNameA = NULL;
  348.  
  349.     // create a player with this name
  350.     hr = lpDirectPlay3A->CreatePlayer(&dpidPlayer, &dpName, 
  351.                             lpDPInfo->hPlayerEvent, NULL, 0, 0);
  352.     if FAILED(hr)
  353.         goto CREATEPLAYER_FAILURE;
  354.  
  355.     // return connection info
  356.     lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  357.     lpDPInfo->dpidPlayer = dpidPlayer;
  358.     lpDPInfo->bIsHost = TRUE;
  359.  
  360.     return (DP_OK);
  361.  
  362. CREATEPLAYER_FAILURE:
  363. OPEN_FAILURE:
  364.     lpDirectPlay3A->Close();
  365.     return (hr);
  366. }
  367.  
  368. HRESULT JoinSession(LPDIRECTPLAY3A lpDirectPlay3A,
  369.                     LPGUID lpguidSessionInstance, LPSTR lpszPlayerName,
  370.                     LPDPLAYINFO lpDPInfo)
  371. {
  372.     DPID                dpidPlayer;
  373.     DPNAME                dpName;
  374.     DPSESSIONDESC2        sessionDesc;
  375.     HRESULT                hr;
  376.  
  377.     // check for valid interface
  378.     if (lpDirectPlay3A == NULL)
  379.         return (DPERR_INVALIDOBJECT);
  380.  
  381.     // join existing session
  382.     ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  383.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  384.     sessionDesc.guidInstance = *lpguidSessionInstance;
  385.  
  386.     hr = lpDirectPlay3A->Open(&sessionDesc, DPOPEN_JOIN);
  387.     if FAILED(hr)
  388.         goto OPEN_FAILURE;
  389.  
  390.     // fill out name structure
  391.     ZeroMemory(&dpName, sizeof(DPNAME));
  392.     dpName.dwSize = sizeof(DPNAME);
  393.     dpName.lpszShortNameA = lpszPlayerName;
  394.     dpName.lpszLongNameA = NULL;
  395.  
  396.     // create a player with this name
  397.     hr = lpDirectPlay3A->CreatePlayer(&dpidPlayer, &dpName, 
  398.                             lpDPInfo->hPlayerEvent, NULL, 0, 0);
  399.     if FAILED(hr)
  400.         goto CREATEPLAYER_FAILURE;
  401.  
  402.     // return connection info
  403.     lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  404.     lpDPInfo->dpidPlayer = dpidPlayer;
  405.     lpDPInfo->bIsHost = FALSE;
  406.  
  407.     return (DP_OK);
  408.  
  409. CREATEPLAYER_FAILURE:
  410. OPEN_FAILURE:
  411.     lpDirectPlay3A->Close();
  412.     return (hr);
  413. }
  414.  
  415. BOOL FAR PASCAL EnumSessionsCallback(
  416.                         LPCDPSESSIONDESC2    lpSessionDesc,
  417.                         LPDWORD                lpdwTimeOut,
  418.                         DWORD                dwFlags,
  419.                         LPVOID                lpContext)
  420. {
  421.     HWND            hWnd = (HWND) lpContext;
  422.     LPGUID            lpGuid;
  423.     LONG            iIndex;
  424.  
  425.     // see if last session has been enumerated
  426.     if (dwFlags & DPESC_TIMEDOUT)
  427.         return (FALSE);                        
  428.  
  429.     // store session name in list
  430.     iIndex = SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_ADDSTRING, 
  431.                                 (WPARAM) 0, (LPARAM) lpSessionDesc->lpszSessionNameA);
  432.  
  433.     if (iIndex == LB_ERR)
  434.         goto FAILURE;
  435.  
  436.  
  437.     // make space for session instance guid
  438.     lpGuid = (LPGUID) GlobalAllocPtr( GHND, sizeof(GUID) );
  439.     if (lpGuid == NULL)
  440.         goto FAILURE;
  441.  
  442.     // store pointer to guid in list
  443.     *lpGuid = lpSessionDesc->guidInstance;
  444.     SendDlgItemMessage( hWnd, IDC_SESSIONLIST, LB_SETITEMDATA, (WPARAM) iIndex, (LPARAM) lpGuid);
  445.  
  446. FAILURE:
  447.     return (TRUE);
  448. }
  449.  
  450. HRESULT EnumSessions(HWND hWnd, LPDIRECTPLAY3A lpDirectPlay3A)
  451. {
  452.     DPSESSIONDESC2    sessionDesc;
  453.     GUID            guidSessionInstance;
  454.     LONG            iIndex;
  455.     HRESULT            hr;
  456.  
  457.     // check for valid interface
  458.     if (lpDirectPlay3A == NULL)
  459.         return (DPERR_INVALIDOBJECT);
  460.  
  461.     // get guid of currently selected session
  462.     guidSessionInstance = GUID_NULL;
  463.     hr = GetSessionInstanceGuid(hWnd, &guidSessionInstance);
  464.  
  465.     // delete existing session list
  466.     DeleteSessionInstanceList(hWnd);
  467.  
  468.     // add sessions to session list
  469.     ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  470.     sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  471.     sessionDesc.guidApplication = DPCHAT_GUID;
  472.  
  473.     hr = lpDirectPlay3A->EnumSessions(&sessionDesc, 0, EnumSessionsCallback,
  474.                                       hWnd, DPENUMSESSIONS_AVAILABLE | DPENUMSESSIONS_ASYNC);
  475.  
  476.     // select the session that was previously selected
  477.     SelectSessionInstance(hWnd, &guidSessionInstance);
  478.  
  479.     // hilite "Join" button only if there are sessions to join
  480.     iIndex = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETCOUNT,
  481.                            (WPARAM) 0, (LPARAM) 0);
  482.  
  483.     EnableDlgButton(hWnd, IDC_JOINBUTTON, (iIndex > 0) ? TRUE : FALSE);
  484.  
  485.     return (hr);
  486. }
  487.  
  488. HRESULT GetConnection(HWND hWnd, LPVOID *lplpConnection)
  489. {
  490.     LONG    iIndex;
  491.  
  492.     // get index of the item currently selected in the combobox
  493.     iIndex = SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_GETCURSEL,
  494.                                 (WPARAM) 0, (LPARAM) 0);
  495.     if (iIndex == CB_ERR)
  496.         return (DPERR_GENERIC);
  497.  
  498.     // get the pointer to the connection shortcut associated with
  499.     // the item
  500.     iIndex = SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_GETITEMDATA,
  501.                                 (WPARAM) iIndex, (LPARAM) 0);
  502.     if (iIndex == CB_ERR)
  503.         return (DPERR_GENERIC);
  504.  
  505.     *lplpConnection = (LPVOID) iIndex;
  506.  
  507.     return (DP_OK);
  508. }
  509.  
  510. void DeleteConnectionList(HWND hWnd)
  511. {
  512.     WPARAM    i;
  513.     LONG    lpData;
  514.     
  515.     // destroy the GUID's stored with each service provider name
  516.     i = 0;
  517.     while (TRUE)
  518.     {
  519.         // get data pointer stored with item
  520.         lpData = SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_GETITEMDATA,
  521.                                     (WPARAM) i, (LPARAM) 0);
  522.         if (lpData == CB_ERR)        // error getting data
  523.             break;
  524.  
  525.         if (lpData != 0)            // no data to delete
  526.             GlobalFreePtr((LPVOID) lpData);
  527.  
  528.         i += 1;
  529.     }
  530.  
  531.     // delete all items in combo box
  532.     SendDlgItemMessage(hWnd, IDC_SPCOMBO, CB_RESETCONTENT,
  533.                                 (WPARAM) 0, (LPARAM) 0);
  534. }
  535.  
  536. HRESULT GetSessionInstanceGuid(HWND hWnd, LPGUID lpguidSessionInstance)
  537. {
  538.     LONG    iIndex;
  539.  
  540.     // get guid for session
  541.     iIndex = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETCURSEL,
  542.                                 (WPARAM) 0, (LPARAM) 0);
  543.     if (iIndex == LB_ERR)
  544.         return (DPERR_GENERIC);
  545.  
  546.     iIndex = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  547.                                 (WPARAM) iIndex, (LPARAM) 0);
  548.     if ((iIndex == LB_ERR) || (iIndex == 0))
  549.         return (DPERR_GENERIC);
  550.  
  551.     *lpguidSessionInstance = *((LPGUID) iIndex);
  552.  
  553.     return (DP_OK);
  554. }
  555.  
  556. void DeleteSessionInstanceList(HWND hWnd)
  557. {
  558.     WPARAM    i;
  559.     LONG    lpData;
  560.     
  561.     // destroy the GUID's stored with each session name
  562.     i = 0;
  563.     while (TRUE)
  564.     {
  565.         // get data pointer stored with item
  566.         lpData = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  567.                                     (WPARAM) i, (LPARAM) 0);
  568.         if (lpData == CB_ERR)        // error getting data
  569.             break;
  570.  
  571.         if (lpData == 0)            // no data to delete
  572.             continue;
  573.  
  574.         GlobalFreePtr((LPVOID) lpData);
  575.         i += 1;
  576.     }
  577.  
  578.     // delete all items in list
  579.     SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_RESETCONTENT,
  580.                                 (WPARAM) 0, (LPARAM) 0);
  581. }
  582.  
  583. void SelectSessionInstance(HWND hWnd, LPGUID lpguidSessionInstance)
  584. {
  585.     WPARAM    i, iIndex;
  586.     LONG    lpData;
  587.     
  588.     // loop over the GUID's stored with each session name
  589.     // to find the one that matches what was passed in
  590.     i = 0;
  591.     iIndex = 0;
  592.     while (TRUE)
  593.     {
  594.         // get data pointer stored with item
  595.         lpData = SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_GETITEMDATA,
  596.                                     (WPARAM) i, (LPARAM) 0);
  597.         if (lpData == CB_ERR)        // error getting data
  598.             break;
  599.  
  600.         if (lpData == 0)            // no data to compare to
  601.             continue;
  602.  
  603.         // guid matches
  604.         if (IsEqualGUID(*lpguidSessionInstance, *((LPGUID) lpData)))
  605.         {
  606.             iIndex = i;                // store index of this string
  607.             break;
  608.         }
  609.  
  610.         i += 1;
  611.     }
  612.  
  613.     // select this item
  614.     SendDlgItemMessage(hWnd, IDC_SESSIONLIST, LB_SETCURSEL, (WPARAM) iIndex, (LPARAM) 0);
  615. }
  616.  
  617. void EnableDlgButton(HWND hDlg, int nIDDlgItem, BOOL bEnable)
  618. {
  619.     EnableWindow(GetDlgItem(hDlg, nIDDlgItem), bEnable);
  620. }
  621.