2 * Win32 waitable timers
4 * Copyright 1999 Alexandre Julliard
11 #include "wine/unicode.h"
12 #include "file.h" /* for FILETIME routines */
16 /***********************************************************************
17 * CreateWaitableTimerA (KERNEL32.861)
19 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
22 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
25 SetLastError( ERROR_FILENAME_EXCED_RANGE );
30 struct create_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
33 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
34 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
36 server_call( REQ_CREATE_TIMER );
40 if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */
45 /***********************************************************************
46 * CreateWaitableTimerW (KERNEL32.862)
48 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
51 DWORD len = name ? strlenW(name) : 0;
54 SetLastError( ERROR_FILENAME_EXCED_RANGE );
59 struct create_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
62 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
63 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
65 server_call( REQ_CREATE_TIMER );
69 if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */
74 /***********************************************************************
75 * OpenWaitableTimerA (KERNEL32.881)
77 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
80 DWORD len = name ? MultiByteToWideChar( CP_ACP, 0, name, strlen(name), NULL, 0 ) : 0;
83 SetLastError( ERROR_FILENAME_EXCED_RANGE );
88 struct open_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
91 req->inherit = inherit;
92 if (len) MultiByteToWideChar( CP_ACP, 0, name, strlen(name), server_data_ptr(req), len );
93 server_call( REQ_OPEN_TIMER );
97 if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */
102 /***********************************************************************
103 * OpenWaitableTimerW (KERNEL32.882)
105 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
108 DWORD len = name ? strlenW(name) : 0;
111 SetLastError( ERROR_FILENAME_EXCED_RANGE );
116 struct open_timer_request *req = server_alloc_req( sizeof(*req), len * sizeof(WCHAR) );
118 req->access = access;
119 req->inherit = inherit;
120 memcpy( server_data_ptr(req), name, len * sizeof(WCHAR) );
121 server_call( REQ_OPEN_TIMER );
125 if (ret == INVALID_HANDLE_VALUE) ret = 0; /* must return 0 on failure, not -1 */
130 /***********************************************************************
131 * SetWaitableTimer (KERNEL32.894)
133 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
134 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
137 LARGE_INTEGER exp = *when;
139 if (exp.s.HighPart < 0) /* relative time */
142 NtQuerySystemTime( &now );
143 exp.QuadPart = RtlLargeIntegerSubtract( now.QuadPart, exp.QuadPart );
148 struct set_timer_request *req = server_alloc_req( sizeof(*req), 0 );
150 if (!exp.s.LowPart && !exp.s.HighPart)
152 /* special case to start timeout on now+period without too many calculations */
159 req->sec = DOSFS_FileTimeToUnixTime( (FILETIME *)&exp, &remainder );
160 req->usec = remainder / 10; /* convert from 100-ns to us units */
162 req->handle = handle;
163 req->period = period;
164 req->callback = callback;
166 if (resume) SetLastError( ERROR_NOT_SUPPORTED ); /* set error but can still succeed */
167 ret = !server_call( REQ_SET_TIMER );
174 /***********************************************************************
175 * CancelWaitableTimer (KERNEL32.857)
177 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
182 struct cancel_timer_request *req = server_alloc_req( sizeof(*req), 0 );
183 req->handle = handle;
184 ret = !server_call( REQ_CANCEL_TIMER );