2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/synch/timer.c
5 * PURPOSE: Wrappers for the NT Timer Implementation
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *****************************************************************/
16 /* FUNCTIONS ****************************************************************/
23 CreateWaitableTimerExA(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL
,
24 IN LPCSTR lpTimerName OPTIONAL
,
26 IN DWORD dwDesiredAccess
)
30 PUNICODE_STRING UnicodeCache
;
31 LPCWSTR UnicodeName
= NULL
;
33 /* Check for a name */
37 UnicodeCache
= &NtCurrentTeb()->StaticUnicodeString
;
39 /* Convert to unicode */
40 RtlInitAnsiString(&AnsiName
, lpTimerName
);
41 Status
= RtlAnsiStringToUnicodeString(UnicodeCache
, &AnsiName
, FALSE
);
42 if (!NT_SUCCESS(Status
))
44 /* Conversion failed */
45 SetLastErrorByStatus(Status
);
49 /* Otherwise, save the buffer */
50 UnicodeName
= (LPCWSTR
)UnicodeCache
->Buffer
;
53 /* Call the Unicode API */
54 return CreateWaitableTimerExW(lpTimerAttributes
,
65 CreateWaitableTimerExW(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL
,
66 IN LPCWSTR lpTimerName OPTIONAL
,
68 IN DWORD dwDesiredAccess
)
71 OBJECT_ATTRIBUTES LocalAttributes
;
72 POBJECT_ATTRIBUTES ObjectAttributes
;
74 UNICODE_STRING ObjectName
;
77 /* Now check if we got a name */
78 if (lpTimerName
) RtlInitUnicodeString(&ObjectName
, lpTimerName
);
80 if (dwFlags
& ~(CREATE_WAITABLE_TIMER_MANUAL_RESET
))
82 SetLastError(ERROR_INVALID_PARAMETER
);
86 TimerType
= (dwFlags
& CREATE_WAITABLE_TIMER_MANUAL_RESET
) ? NotificationTimer
: SynchronizationTimer
;
88 /* Now convert the object attributes */
89 ObjectAttributes
= BasepConvertObjectAttributes(&LocalAttributes
,
91 lpTimerName
? &ObjectName
: NULL
);
93 /* Create the timer */
94 Status
= NtCreateTimer(&Handle
,
95 (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
);
129 CreateWaitableTimerW(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL
,
130 IN BOOL bManualReset
,
131 IN LPCWSTR lpTimerName OPTIONAL
)
136 dwFlags
|= CREATE_WAITABLE_TIMER_MANUAL_RESET
;
138 return CreateWaitableTimerExW(lpTimerAttributes
,
149 CreateWaitableTimerA(IN LPSECURITY_ATTRIBUTES lpTimerAttributes OPTIONAL
,
150 IN BOOL bManualReset
,
151 IN LPCSTR lpTimerName OPTIONAL
)
156 dwFlags
|= CREATE_WAITABLE_TIMER_MANUAL_RESET
;
158 return CreateWaitableTimerExA(lpTimerAttributes
,
169 OpenWaitableTimerW(IN DWORD dwDesiredAccess
,
170 IN BOOL bInheritHandle
,
171 IN LPCWSTR lpTimerName
)
173 OBJECT_ATTRIBUTES ObjectAttributes
;
174 UNICODE_STRING ObjectName
;
178 /* Make sure we got a name */
181 /* Fail without one */
182 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
186 /* Initialize the object name and attributes */
187 RtlInitUnicodeString(&ObjectName
, lpTimerName
);
188 InitializeObjectAttributes(&ObjectAttributes
,
190 bInheritHandle
? OBJ_INHERIT
: 0,
195 Status
= NtOpenTimer(&Handle
, dwDesiredAccess
, &ObjectAttributes
);
196 if (!NT_SUCCESS(Status
))
198 /* Convert the status and fail */
199 SetLastErrorByStatus(Status
);
203 /* Return the handle */
212 OpenWaitableTimerA(IN DWORD dwDesiredAccess
,
213 IN BOOL bInheritHandle
,
214 IN LPCSTR lpTimerName
)
217 ANSI_STRING AnsiName
;
218 PUNICODE_STRING UnicodeCache
;
220 /* Check for a name */
224 UnicodeCache
= &NtCurrentTeb()->StaticUnicodeString
;
226 /* Convert to unicode */
227 RtlInitAnsiString(&AnsiName
, lpTimerName
);
228 Status
= RtlAnsiStringToUnicodeString(UnicodeCache
, &AnsiName
, FALSE
);
229 if (!NT_SUCCESS(Status
))
231 /* Conversion failed */
232 SetLastErrorByStatus(Status
);
239 SetLastError(ERROR_INVALID_PARAMETER
);
243 /* Call the Unicode API */
244 return OpenWaitableTimerW(dwDesiredAccess
,
246 (LPCWSTR
)UnicodeCache
->Buffer
);
254 SetWaitableTimer(IN HANDLE hTimer
,
255 IN
const LARGE_INTEGER
*pDueTime
,
257 IN PTIMERAPCROUTINE pfnCompletionRoutine OPTIONAL
,
258 IN OPTIONAL LPVOID lpArgToCompletionRoutine
,
264 Status
= NtSetTimer(hTimer
,
265 (PLARGE_INTEGER
)pDueTime
,
266 (PTIMER_APC_ROUTINE
)pfnCompletionRoutine
,
267 lpArgToCompletionRoutine
,
271 if (NT_SUCCESS(Status
)) return TRUE
;
273 /* If we got here, then we failed */
274 SetLastErrorByStatus(Status
);
283 CancelWaitableTimer(IN HANDLE hTimer
)
287 /* Cancel the timer */
288 Status
= NtCancelTimer(hTimer
, NULL
);
289 if (NT_SUCCESS(Status
)) return TRUE
;
291 /* If we got here, then we failed */
292 SetLastErrorByStatus(Status
);