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 / dsstream / dstrenum.c < prev    next >
C/C++ Source or Header  |  1997-07-14  |  5KB  |  142 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:   dstrenum.c
  6.  *  Content:   Illustrates enumerating DirectSound drivers
  7.  *
  8.  ***************************************************************************/
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <mmsystem.h>
  13. #include <dsound.h>
  14. #include <memory.h>
  15.  
  16. #include "dsstream.h"
  17.  
  18. extern HINSTANCE hInst;
  19. extern HWND      hWndMain;
  20.  
  21.  
  22.  
  23. /****************************************************************************/
  24. /* DoDSoundEnumerate()                                                      */
  25. /*                                                                          */
  26. /*   This function takes care of handling the DirectSound enumeration, which*/
  27. /* simply means creating a dialog box, at this point...                     */
  28. /****************************************************************************/
  29. BOOL DoDSoundEnumerate( LPGUID lpGUID )
  30.     {
  31.     if( DialogBoxParam( hInst, MAKEINTRESOURCE(IDD_DSENUMBOX), hWndMain,
  32.                         DSEnumDlgProc, (LPARAM)lpGUID ))
  33.         return( TRUE );
  34.  
  35.     return( FALSE );
  36.     }
  37.  
  38. /****************************************************************************/
  39. /* DSEnumDlgProc()                                                          */
  40. /*                                                                          */
  41. /*   Dialog procedure for the DSound enumeration choice dialog. Allows the  */
  42. /* user to choose from installed drivers.  Returns TRUE on error.           */
  43. /****************************************************************************/
  44. BOOL CALLBACK DSEnumDlgProc( HWND hDlg, UINT msg,
  45.                                 WPARAM wParam, LPARAM lParam )
  46.     {
  47.     static HWND   hCombo;
  48.     static LPGUID lpGUID;
  49.     LPGUID        lpTemp;
  50.     int           i;
  51.  
  52.     switch( msg )
  53.         {
  54.         case WM_INITDIALOG:
  55.             hCombo = GetDlgItem( hDlg, IDC_DSENUM_COMBO );
  56.             lpGUID = (LPGUID)lParam;
  57.  
  58.             if( DirectSoundEnumerate( (LPDSENUMCALLBACK)DSEnumProc, &hCombo ) != DS_OK )
  59.                 {
  60.                 EndDialog( hDlg, TRUE );
  61.                 return( TRUE );
  62.                 }
  63.             if( ComboBox_GetCount( hCombo ))
  64.                 ComboBox_SetCurSel( hCombo, 0 );
  65.             else
  66.                 {
  67.                 EndDialog( hDlg, TRUE );
  68.                 return( TRUE );
  69.                 }
  70.             return( TRUE );
  71.  
  72.  
  73.         case WM_COMMAND:
  74.             switch( LOWORD( wParam ))
  75.                 {
  76.                 case IDOK:
  77.                     for( i = 0; i < ComboBox_GetCount( hCombo ); i++ )
  78.                         {
  79.                         lpTemp = (LPGUID)ComboBox_GetItemData( hCombo, i );
  80.                         if( i == ComboBox_GetCurSel( hCombo ))
  81.                             {
  82.                             if( lpTemp != NULL )
  83.                                 memcpy( lpGUID, lpTemp, sizeof(GUID));
  84.                 else
  85.                     lpGUID = NULL;
  86.                 }
  87.                         if( lpTemp )
  88.                             LocalFree( lpTemp );
  89.                         }
  90.                     // If we got the NULL GUID, then we want to open the default
  91.                     // sound driver, so return with an error and the init code
  92.             // will know not to pass in the guID and will send NULL
  93.             // instead.
  94.                     if( lpGUID == NULL )
  95.                 EndDialog( hDlg, TRUE );
  96.                     else
  97.                         EndDialog( hDlg, FALSE );
  98.                     return( TRUE );
  99.  
  100.                 case IDCANCEL:
  101.                     // Force a NULL GUID
  102.                     EndDialog( hDlg, TRUE );
  103.                     return( TRUE );
  104.                 }
  105.             break;
  106.  
  107.  
  108.         default:
  109.             return( FALSE );
  110.         }
  111.  
  112.     return( FALSE );
  113.     }
  114.  
  115.  
  116. /****************************************************************************/
  117. /* DSEnumProc()                                                             */
  118. /*                                                                          */
  119. /*   This is the Enumeration procedure called by DirectSoundEnumerate with  */
  120. /* the parameters of each DirectSound Object available in the system.       */
  121. /****************************************************************************/
  122. BOOL CALLBACK DSEnumProc( LPGUID lpGUID, LPSTR lpszDesc,
  123.                                 LPSTR lpszDrvName, LPVOID lpContext )
  124.     {
  125.     HWND   hCombo = *(HWND *)lpContext;
  126.     LPGUID lpTemp = NULL;
  127.  
  128.     if( lpGUID != NULL )
  129.         {
  130.         if(( lpTemp = LocalAlloc( LPTR, sizeof(GUID))) == NULL )
  131.         return( TRUE );
  132.  
  133.         memcpy( lpTemp, lpGUID, sizeof(GUID));
  134.     }
  135.  
  136.     ComboBox_AddString( hCombo, lpszDesc );
  137.     ComboBox_SetItemData( hCombo,
  138.                 ComboBox_FindString( hCombo, 0, lpszDesc ),
  139.                 lpTemp );
  140.     return( TRUE );
  141.     }
  142.