2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/synch/event.c
5 * PURPOSE: Wrappers for the NT Event Implementation
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *****************************************************************/
15 /* FUNCTIONS ****************************************************************/
19 CreateEventExA(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL
,
20 IN LPCSTR lpName OPTIONAL
,
22 IN DWORD dwDesiredAccess
)
26 PUNICODE_STRING UnicodeCache
;
27 LPCWSTR UnicodeName
= NULL
;
29 /* Check for a name */
33 UnicodeCache
= &NtCurrentTeb()->StaticUnicodeString
;
35 /* Convert to unicode */
36 RtlInitAnsiString(&AnsiName
, lpName
);
37 Status
= RtlAnsiStringToUnicodeString(UnicodeCache
, &AnsiName
, FALSE
);
38 if (!NT_SUCCESS(Status
))
40 /* Conversion failed */
41 SetLastErrorByStatus(Status
);
45 /* Otherwise, save the buffer */
46 UnicodeName
= (LPCWSTR
)UnicodeCache
->Buffer
;
49 /* Call the Unicode API */
50 return CreateEventExW(lpEventAttributes
,
59 CreateEventExW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL
,
60 IN LPCWSTR lpName OPTIONAL
,
62 IN DWORD dwDesiredAccess
)
65 OBJECT_ATTRIBUTES LocalAttributes
;
66 POBJECT_ATTRIBUTES ObjectAttributes
;
68 UNICODE_STRING ObjectName
;
72 /* Now check if we got a name */
73 if (lpName
) RtlInitUnicodeString(&ObjectName
, lpName
);
75 /* Check for invalid flags */
76 if (dwFlags
& ~(CREATE_EVENT_INITIAL_SET
| CREATE_EVENT_MANUAL_RESET
))
79 SetLastError(ERROR_INVALID_PARAMETER
);
83 /* Set initial state and event type */
84 InitialState
= (dwFlags
& CREATE_EVENT_INITIAL_SET
) ? TRUE
: FALSE
;
85 EventType
= (dwFlags
& CREATE_EVENT_MANUAL_RESET
) ? NotificationEvent
: SynchronizationEvent
;
87 /* Now convert the object attributes */
88 ObjectAttributes
= BasepConvertObjectAttributes(&LocalAttributes
,
90 lpName
? &ObjectName
: NULL
);
92 /* Create the event */
93 Status
= NtCreateEvent(&Handle
,
94 (ACCESS_MASK
)dwDesiredAccess
,
98 if (NT_SUCCESS(Status
))
100 /* Check if the object already existed */
101 if (Status
== STATUS_OBJECT_NAME_EXISTS
)
103 /* Set distinguished Win32 error code */
104 SetLastError(ERROR_ALREADY_EXISTS
);
108 /* Otherwise, set success */
109 SetLastError(ERROR_SUCCESS
);
112 /* Return the handle */
117 /* Convert the NT Status and fail */
118 SetLastErrorByStatus(Status
);
126 CreateEventA(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL
,
127 IN BOOL bManualReset
,
128 IN BOOL bInitialState
,
129 IN LPCSTR lpName OPTIONAL
)
134 if (bManualReset
) dwFlags
|= CREATE_EVENT_MANUAL_RESET
;
135 if (bInitialState
) dwFlags
|= CREATE_EVENT_INITIAL_SET
;
137 /* Call the newer API */
138 return CreateEventExA(lpEventAttributes
,
146 CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL
,
147 IN BOOL bManualReset
,
148 IN BOOL bInitialState
,
149 IN LPCWSTR lpName OPTIONAL
)
154 if (bManualReset
) dwFlags
|= CREATE_EVENT_MANUAL_RESET
;
155 if (bInitialState
) dwFlags
|= CREATE_EVENT_INITIAL_SET
;
157 /* Call the newer API */
158 return CreateEventExW(lpEventAttributes
,
166 OpenEventA(IN DWORD dwDesiredAccess
,
167 IN BOOL bInheritHandle
,
171 ANSI_STRING AnsiName
;
172 PUNICODE_STRING UnicodeCache
;
174 /* Check for a name */
178 UnicodeCache
= &NtCurrentTeb()->StaticUnicodeString
;
180 /* Convert to unicode */
181 RtlInitAnsiString(&AnsiName
, lpName
);
182 Status
= RtlAnsiStringToUnicodeString(UnicodeCache
, &AnsiName
, FALSE
);
183 if (!NT_SUCCESS(Status
))
185 /* Conversion failed */
186 SetLastErrorByStatus(Status
);
193 SetLastError(ERROR_INVALID_PARAMETER
);
197 /* Call the Unicode API */
198 return OpenEventW(dwDesiredAccess
,
200 (LPCWSTR
)UnicodeCache
->Buffer
);
205 OpenEventW(IN DWORD dwDesiredAccess
,
206 IN BOOL bInheritHandle
,
209 OBJECT_ATTRIBUTES ObjectAttributes
;
210 UNICODE_STRING ObjectName
;
214 /* Make sure we got a name */
217 /* Fail without one */
218 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
222 /* Initialize the object name and attributes */
223 RtlInitUnicodeString(&ObjectName
, lpName
);
224 InitializeObjectAttributes(&ObjectAttributes
,
226 bInheritHandle
? OBJ_INHERIT
: 0,
231 Status
= NtOpenEvent(&Handle
, dwDesiredAccess
, &ObjectAttributes
);
232 if (!NT_SUCCESS(Status
))
234 /* Convert the status and fail */
235 SetLastErrorByStatus(Status
);
239 /* Return the handle */
248 PulseEvent(IN HANDLE hEvent
)
252 /* Pulse the event */
253 Status
= NtPulseEvent(hEvent
, NULL
);
254 if (NT_SUCCESS(Status
)) return TRUE
;
256 /* If we got here, then we failed */
257 SetLastErrorByStatus(Status
);
266 ResetEvent(IN HANDLE hEvent
)
270 /* Clear the event */
271 Status
= NtResetEvent(hEvent
, NULL
);
272 if (NT_SUCCESS(Status
)) return TRUE
;
274 /* If we got here, then we failed */
275 SetLastErrorByStatus(Status
);
284 SetEvent(IN HANDLE hEvent
)
289 Status
= NtSetEvent(hEvent
, NULL
);
290 if (NT_SUCCESS(Status
)) return TRUE
;
292 /* If we got here, then we failed */
293 SetLastErrorByStatus(Status
);