DSTRENUM.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

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