:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / kernel32 / synch / event.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS system libraries
5  * FILE:            lib/kernel32/synch/event.c
6  * PURPOSE:         Local string functions
7  * PROGRAMMER:      Ariadne ( ariadne@xs4all.nl)
8  * UPDATE HISTORY:
9  *                  Created 01/11/98
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <windows.h>
16
17 #define NDEBUG
18 #include <kernel32/kernel32.h>
19 #include <kernel32/error.h>
20
21 /* FUNCTIONS ****************************************************************/
22
23 HANDLE STDCALL
24 CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes,
25              WINBOOL bManualReset,
26              WINBOOL bInitialState,
27              LPCSTR lpName)
28 {
29    UNICODE_STRING EventNameU;
30    ANSI_STRING EventName;
31    HANDLE EventHandle;
32
33    RtlInitUnicodeString (&EventNameU, NULL);
34
35    if (lpName)
36      {
37         RtlInitAnsiString(&EventName,
38                           (LPSTR)lpName);
39         RtlAnsiStringToUnicodeString(&EventNameU,
40                                      &EventName,
41                                      TRUE);
42      }
43
44    EventHandle = CreateEventW(lpEventAttributes,
45                               bManualReset,
46                               bInitialState,
47                               EventNameU.Buffer);
48
49    if (lpName)
50      {
51         RtlFreeUnicodeString(&EventNameU);
52      }
53
54    return EventHandle;
55 }
56
57
58 HANDLE STDCALL
59 CreateEventW(LPSECURITY_ATTRIBUTES lpEventAttributes,
60              WINBOOL bManualReset,
61              WINBOOL bInitialState,
62              LPCWSTR lpName)
63 {
64    NTSTATUS Status;
65    HANDLE hEvent;
66    UNICODE_STRING EventNameString;
67    POBJECT_ATTRIBUTES PtrObjectAttributes;
68    OBJECT_ATTRIBUTES ObjectAttributes;
69
70    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
71    ObjectAttributes.RootDirectory = hBaseDir;
72    ObjectAttributes.ObjectName = NULL;
73    ObjectAttributes.Attributes = 0;
74    ObjectAttributes.SecurityDescriptor = NULL;
75    ObjectAttributes.SecurityQualityOfService = NULL;
76
77    if (lpName != NULL)
78      {
79         RtlInitUnicodeString(&EventNameString, (LPWSTR)lpName);
80         ObjectAttributes.ObjectName = &EventNameString;
81      }
82
83    Status = NtCreateEvent(&hEvent,
84                           STANDARD_RIGHTS_ALL|EVENT_READ_ACCESS|EVENT_WRITE_ACCESS,
85                           &ObjectAttributes,
86                           bManualReset,
87                           bInitialState);
88    DPRINT( "Called\n" );
89    if (!NT_SUCCESS(Status))
90      {
91         SetLastErrorByStatus(Status);
92         return NULL;
93      }
94
95    return hEvent;
96 }
97
98
99 HANDLE STDCALL
100 OpenEventA(DWORD dwDesiredAccess,
101            WINBOOL bInheritHandle,
102            LPCSTR lpName)
103 {
104    UNICODE_STRING EventNameU;
105    ANSI_STRING EventName;
106    HANDLE EventHandle;
107
108    RtlInitUnicodeString(&EventNameU,
109                         NULL);
110
111    if (lpName)
112      {
113         RtlInitAnsiString(&EventName,
114                           (LPSTR)lpName);
115         RtlAnsiStringToUnicodeString(&EventNameU,
116                                      &EventName,
117                                      TRUE);
118     }
119
120    EventHandle = OpenEventW(dwDesiredAccess,
121                             bInheritHandle,
122                             EventNameU.Buffer);
123
124    if (lpName)
125      {
126         RtlFreeUnicodeString(&EventNameU);
127      }
128
129    return EventHandle;
130 }
131
132
133 HANDLE STDCALL
134 OpenEventW(DWORD dwDesiredAccess,
135            WINBOOL bInheritHandle,
136            LPCWSTR lpName)
137 {
138    OBJECT_ATTRIBUTES ObjectAttributes;
139    UNICODE_STRING EventNameString;
140    NTSTATUS Status;
141    HANDLE hEvent = NULL;
142
143    if (lpName == NULL)
144      {
145         SetLastError(ERROR_INVALID_PARAMETER);
146         return NULL;
147      }
148
149    RtlInitUnicodeString(&EventNameString, (LPWSTR)lpName);
150
151    ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
152    ObjectAttributes.RootDirectory = hBaseDir;
153    ObjectAttributes.ObjectName = &EventNameString;
154    ObjectAttributes.Attributes = 0;
155    ObjectAttributes.SecurityDescriptor = NULL;
156    ObjectAttributes.SecurityQualityOfService = NULL;
157    if (bInheritHandle == TRUE)
158      {
159         ObjectAttributes.Attributes |= OBJ_INHERIT;
160      }
161
162    Status = NtOpenEvent(&hEvent,
163                         dwDesiredAccess,
164                         &ObjectAttributes);
165    if (!NT_SUCCESS(Status))
166      {
167         SetLastErrorByStatus(Status);
168         return NULL;
169      }
170
171    return hEvent;
172 }
173
174
175 WINBOOL STDCALL
176 PulseEvent(HANDLE hEvent)
177 {
178    ULONG Count;
179    NTSTATUS Status;
180
181    Status = NtPulseEvent(hEvent,
182                          &Count);
183    if (!NT_SUCCESS(Status))
184      {
185         SetLastErrorByStatus (Status);
186         return FALSE;
187      }
188
189    return TRUE;
190 }
191
192
193 WINBOOL STDCALL
194 ResetEvent(HANDLE hEvent)
195 {
196    NTSTATUS Status;
197    ULONG Count;
198
199    Status = NtResetEvent(hEvent,
200                          &Count);
201    if (!NT_SUCCESS(Status))
202      {
203         SetLastErrorByStatus(Status);
204         return FALSE;
205      }
206
207    return TRUE;
208 }
209
210
211 WINBOOL STDCALL
212 SetEvent(HANDLE hEvent)
213 {
214    NTSTATUS Status;
215    ULONG Count;
216
217    Status = NtSetEvent(hEvent,
218                        &Count);
219    if (!NT_SUCCESS(Status))
220      {
221         SetLastErrorByStatus(Status);
222         return FALSE;
223      }
224
225    return TRUE;
226 }
227
228 /* EOF */