beep.c
Upload User: caisha3
Upload Date: 2013-09-21
Package Size: 208739k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /*++
  2. Copyright (c) 1990  Microsoft Corporation
  3. Module Name:
  4.     beep.c
  5. Abstract:
  6.     This module contains the Win32 Beep APIs
  7. Author:
  8.     Steve Wood (stevewo)  5-Oct-1991
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. #pragma hdrstop
  13. #include <ntddbeep.h>
  14. #include "conapi.h"
  15. /*
  16.  * Forward declaration
  17.  */
  18. VOID NotifySoundSentry(VOID);
  19. BOOL
  20. APIENTRY
  21. Beep(
  22.     DWORD dwFreq,
  23.     DWORD dwDuration
  24.     )
  25. {
  26.     OBJECT_ATTRIBUTES ObjectAttributes;
  27.     UNICODE_STRING NameString;
  28.     NTSTATUS Status;
  29.     IO_STATUS_BLOCK IoStatus;
  30.     BEEP_SET_PARAMETERS BeepParameters;
  31.     HANDLE hBeepDevice;
  32.     if ( IsTerminalServer() && NtCurrentPeb()->SessionId ) {
  33.         //
  34.         // Non-zero sessionid implies remote session.
  35.         //
  36.         if ( !pWinStationBeepOpen ) {
  37.             HMODULE hwinsta = NULL;
  38.             /*
  39.              *  Get handle to winsta.dll
  40.              */
  41.             if ( (hwinsta = LoadLibraryW( L"WINSTA" )) != NULL ) {
  42.                 pWinStationBeepOpen   = (PWINSTATIONBEEPOPEN)
  43.                     GetProcAddress( hwinsta, "_WinStationBeepOpen" );
  44.             }
  45.         }
  46.         hBeepDevice = NULL;
  47.         if ( pWinStationBeepOpen )
  48.             hBeepDevice = (*pWinStationBeepOpen)( -1 ); //Current Session
  49.         if ( hBeepDevice == NULL )
  50.             Status = STATUS_ACCESS_DENIED;
  51.         else
  52.             Status = STATUS_SUCCESS;
  53.     }
  54.     else {
  55.         RtlInitUnicodeString( &NameString, DD_BEEP_DEVICE_NAME_U );
  56.         InitializeObjectAttributes( &ObjectAttributes,
  57.                                     &NameString,
  58.                                     0,
  59.                                     NULL,
  60.                                     NULL
  61.                                   );
  62.         Status = NtCreateFile( &hBeepDevice,
  63.                                FILE_READ_DATA | FILE_WRITE_DATA,
  64.                                &ObjectAttributes,
  65.                                &IoStatus,
  66.                                NULL,
  67.                                0,
  68.                                FILE_SHARE_READ | FILE_SHARE_WRITE,
  69.                                FILE_OPEN_IF,
  70.                                0,
  71.                                (PVOID) NULL,
  72.                                0L
  73.                              );
  74.     }
  75.     if (!NT_SUCCESS( Status )) {
  76.         BaseSetLastNTError( Status );
  77.         return( FALSE );
  78.         }
  79.     //
  80.     // 0,0 is a special case used to turn off a beep.  Otherwise
  81.     // validate the dwFreq parameter to be in range.
  82.     //
  83.     if ((dwFreq != 0 || dwDuration != 0) &&
  84.         (dwFreq < (ULONG)0x25 || dwFreq > (ULONG)0x7FFF)
  85.        ) {
  86.         Status = STATUS_INVALID_PARAMETER;
  87.         }
  88.     else {
  89.         BeepParameters.Frequency = dwFreq;
  90.         BeepParameters.Duration = dwDuration;
  91.         Status = NtDeviceIoControlFile( hBeepDevice,
  92.                                         NULL,
  93.                                         NULL,
  94.                                         NULL,
  95.                                         &IoStatus,
  96.                                         IOCTL_BEEP_SET,
  97.                                         &BeepParameters,
  98.                                         sizeof( BeepParameters ),
  99.                                         NULL,
  100.                                         0
  101.                                       );
  102.         }
  103.     NotifySoundSentry();
  104.     if (!NT_SUCCESS( Status )) {
  105.         BaseSetLastNTError( Status );
  106.         NtClose( hBeepDevice );
  107.         return( FALSE );
  108.         }
  109.     else {
  110.         //
  111.         // Beep device is asynchronous, so sleep for duration
  112.         // to allow this beep to complete.
  113.         //
  114.         if (dwDuration != (DWORD)-1 && (dwFreq != 0 || dwDuration != 0)) {
  115.             SleepEx( dwDuration, TRUE );
  116.             }
  117.         NtClose( hBeepDevice );
  118.         return( TRUE );
  119.         }
  120. }
  121. VOID
  122. NotifySoundSentry(VOID)
  123. {
  124. #if defined(BUILD_WOW6432)
  125.     ULONG VideoMode;
  126.     if (!GetConsoleDisplayMode(&VideoMode)) {
  127.         VideoMode = 0;
  128.     }
  129.     //
  130.     // SoundSentry is currently only supported for Windows mode - no
  131.     // full screen support.
  132.     //
  133.      
  134.     if (VideoMode == 0) {    
  135.         CsrBasepSoundSentryNotification(VideoMode);
  136.     }
  137. #else
  138.     BASE_API_MSG m;
  139.     PBASE_SOUNDSENTRY_NOTIFICATION_MSG e =
  140.             (PBASE_SOUNDSENTRY_NOTIFICATION_MSG)&m.u.SoundSentryNotification;
  141.     if (!GetConsoleDisplayMode(&e->VideoMode)) {
  142.         e->VideoMode = 0;
  143.     }
  144.     //
  145.     // SoundSentry is currently only supported for Windows mode - no
  146.     // full screen support.
  147.     //
  148.     if (e->VideoMode == 0) {
  149.         CsrClientCallServer((PCSR_API_MSG)&m,
  150.                             NULL,
  151.                             CSR_MAKE_API_NUMBER( BASESRV_SERVERDLL_INDEX,
  152.                                                  BasepSoundSentryNotification ),
  153.                             sizeof( *e )
  154.                             );
  155.     }
  156. #endif
  157. }