fix Beep() to make it work with xp's beep.sys
[reactos.git] / reactos / lib / kernel32 / misc / error.c
1 /* $Id$
2 *
3 * reactos/lib/kernel32/misc/error.c
4 *
5 */
6
7 #include <k32.h>
8
9 #define NDEBUG
10 #include "../include/debug.h"
11
12
13 /*
14 * @implemented
15 */
16 VOID
17 STDCALL
18 SetLastError (
19 DWORD dwErrorCode
20 )
21 {
22 NtCurrentTeb ()->LastErrorValue = (ULONG) dwErrorCode;
23 }
24
25
26 /*
27 * @implemented
28 */
29 DWORD
30 STDCALL
31 GetLastError (VOID)
32 {
33 return (DWORD) (NtCurrentTeb ()->LastErrorValue);
34 }
35
36
37 /*
38 * @implemented
39 */
40 BOOL
41 STDCALL
42 Beep (DWORD dwFreq, DWORD dwDuration)
43 {
44 HANDLE hBeep;
45 UNICODE_STRING BeepDevice;
46 OBJECT_ATTRIBUTES ObjectAttributes;
47 IO_STATUS_BLOCK IoStatusBlock;
48 BEEP_SET_PARAMETERS BeepSetParameters;
49 NTSTATUS Status;
50
51 /* check the parameters */
52 if ((dwFreq >= 0x25 && dwFreq <= 0x7FFF) ||
53 (dwFreq == 0x0 && dwDuration == 0x0))
54 {
55 /* open the device */
56 RtlInitUnicodeString(&BeepDevice,
57 L"\\Device\\Beep");
58
59 InitializeObjectAttributes(&ObjectAttributes,
60 &BeepDevice,
61 0,
62 NULL,
63 NULL);
64
65 Status = NtCreateFile(&hBeep,
66 FILE_READ_DATA | FILE_WRITE_DATA,
67 &ObjectAttributes,
68 &IoStatusBlock,
69 NULL,
70 0,
71 FILE_SHARE_READ | FILE_SHARE_WRITE,
72 FILE_OPEN_IF,
73 0,
74 NULL,
75 0);
76 if (NT_SUCCESS(Status))
77 {
78 /* Set beep data */
79 BeepSetParameters.Frequency = dwFreq;
80 BeepSetParameters.Duration = dwDuration;
81
82 Status = NtDeviceIoControlFile(hBeep,
83 NULL,
84 NULL,
85 NULL,
86 &IoStatusBlock,
87 IOCTL_BEEP_SET,
88 &BeepSetParameters,
89 sizeof(BEEP_SET_PARAMETERS),
90 NULL,
91 0);
92
93 /* do an alertable wait if necessary */
94 if (NT_SUCCESS(Status) &&
95 (dwFreq != 0x0 || dwDuration != 0x0) && dwDuration != (DWORD)-1)
96 {
97 SleepEx(dwDuration,
98 TRUE);
99 }
100
101 NtClose(hBeep);
102 }
103 }
104 else
105 Status = STATUS_INVALID_PARAMETER;
106
107 if (!NT_SUCCESS(Status))
108 {
109 SetLastErrorByStatus (Status);
110 return FALSE;
111 }
112
113 return TRUE;
114 }
115
116 /* EOF */