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

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