fix Beep() to make it work with xp's beep.sys
authorThomas Bluemel <thomas@reactsoft.com>
Thu, 27 Oct 2005 20:00:27 +0000 (20:00 +0000)
committerThomas Bluemel <thomas@reactsoft.com>
Thu, 27 Oct 2005 20:00:27 +0000 (20:00 +0000)
svn path=/trunk/; revision=18798

reactos/lib/kernel32/misc/error.c

index 63759ec..509cc85 100644 (file)
@@ -42,33 +42,73 @@ STDCALL
 Beep (DWORD dwFreq, DWORD dwDuration)
 {
     HANDLE hBeep;
 Beep (DWORD dwFreq, DWORD dwDuration)
 {
     HANDLE hBeep;
+    UNICODE_STRING BeepDevice;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    IO_STATUS_BLOCK IoStatusBlock;
     BEEP_SET_PARAMETERS BeepSetParameters;
     BEEP_SET_PARAMETERS BeepSetParameters;
-    DWORD dwReturned;
-
-    hBeep = CreateFileW(L"\\\\.\\Beep",
-                       FILE_GENERIC_READ | FILE_GENERIC_WRITE,
-                       0,
-                       NULL,
-                       OPEN_EXISTING,
-                       0,
-                       NULL);
-    if (hBeep == INVALID_HANDLE_VALUE)
-        return FALSE;
+    NTSTATUS Status;
+
+    /* check the parameters */
+    if ((dwFreq >= 0x25 && dwFreq <= 0x7FFF) ||
+        (dwFreq == 0x0 && dwDuration == 0x0))
+    {
+        /* open the device */
+        RtlInitUnicodeString(&BeepDevice,
+                             L"\\Device\\Beep");
+
+        InitializeObjectAttributes(&ObjectAttributes,
+                                   &BeepDevice,
+                                   0,
+                                   NULL,
+                                   NULL);
 
 
-    /* Set beep data */
-    BeepSetParameters.Frequency = dwFreq;
-    BeepSetParameters.Duration  = dwDuration;
+        Status = NtCreateFile(&hBeep,
+                              FILE_READ_DATA | FILE_WRITE_DATA,
+                              &ObjectAttributes,
+                              &IoStatusBlock,
+                              NULL,
+                              0,
+                              FILE_SHARE_READ | FILE_SHARE_WRITE,
+                              FILE_OPEN_IF,
+                              0,
+                              NULL,
+                              0);
+        if (NT_SUCCESS(Status))
+        {
+            /* Set beep data */
+            BeepSetParameters.Frequency = dwFreq;
+            BeepSetParameters.Duration = dwDuration;
 
 
-    DeviceIoControl(hBeep,
-                    IOCTL_BEEP_SET,
-                    &BeepSetParameters,
-                    sizeof(BEEP_SET_PARAMETERS),
-                    NULL,
-                    0,
-                    &dwReturned,
-                    NULL);
+            Status = NtDeviceIoControlFile(hBeep,
+                                           NULL,
+                                           NULL,
+                                           NULL,
+                                           &IoStatusBlock,
+                                           IOCTL_BEEP_SET,
+                                           &BeepSetParameters,
+                                           sizeof(BEEP_SET_PARAMETERS),
+                                           NULL,
+                                           0);
 
 
-    CloseHandle (hBeep);
+            /* do an alertable wait if necessary */
+            if (NT_SUCCESS(Status) &&
+                (dwFreq != 0x0 || dwDuration != 0x0) && dwDuration != (DWORD)-1)
+            {
+                SleepEx(dwDuration,
+                        TRUE);
+            }
+
+            NtClose(hBeep);
+        }
+    }
+    else
+        Status = STATUS_INVALID_PARAMETER;
+
+    if (!NT_SUCCESS(Status))
+    {
+        SetLastErrorByStatus (Status);
+        return FALSE;
+    }
 
     return TRUE;
 }
 
     return TRUE;
 }