mstask: Add support for event triggers to ITask::GetNextRunTime().
[wine.git] / dlls / mstask / task.c
blob57ad0f7005f756055e122a9ffa98f31c5fd1134d
1 /*
2 * Copyright (C) 2008 Google (Roy Shea)
3 * Copyright (C) 2018 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winternl.h"
27 #include "objbase.h"
28 #include "taskschd.h"
29 #include "mstask.h"
30 #include "mstask_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mstask);
35 typedef struct
37 USHORT product_version;
38 USHORT file_version;
39 UUID uuid;
40 USHORT name_size_offset;
41 USHORT trigger_offset;
42 USHORT error_retry_count;
43 USHORT error_retry_interval;
44 USHORT idle_deadline;
45 USHORT idle_wait;
46 UINT priority;
47 UINT maximum_runtime;
48 UINT exit_code;
49 HRESULT status;
50 UINT flags;
51 SYSTEMTIME last_runtime;
52 } FIXDLEN_DATA;
54 typedef struct
56 ITask ITask_iface;
57 IPersistFile IPersistFile_iface;
58 LONG ref;
59 ITaskDefinition *task;
60 IExecAction *action;
61 LPWSTR task_name;
62 HRESULT status;
63 WORD idle_minutes, deadline_minutes;
64 DWORD flags, priority, maxRunTime, exit_code;
65 LPWSTR accountName;
66 DWORD trigger_count;
67 TASK_TRIGGER *trigger;
68 BOOL is_dirty;
69 USHORT instance_count;
70 } TaskImpl;
72 static inline TaskImpl *impl_from_ITask(ITask *iface)
74 return CONTAINING_RECORD(iface, TaskImpl, ITask_iface);
77 static inline TaskImpl *impl_from_IPersistFile( IPersistFile *iface )
79 return CONTAINING_RECORD(iface, TaskImpl, IPersistFile_iface);
82 static void TaskDestructor(TaskImpl *This)
84 TRACE("%p\n", This);
85 if (This->action)
86 IExecAction_Release(This->action);
87 ITaskDefinition_Release(This->task);
88 heap_free(This->task_name);
89 heap_free(This->accountName);
90 heap_free(This->trigger);
91 heap_free(This);
92 InterlockedDecrement(&dll_ref);
95 static HRESULT WINAPI MSTASK_ITask_QueryInterface(
96 ITask* iface,
97 REFIID riid,
98 void **ppvObject)
100 TaskImpl * This = impl_from_ITask(iface);
102 TRACE("IID: %s\n", debugstr_guid(riid));
103 if (ppvObject == NULL)
104 return E_POINTER;
106 if (IsEqualGUID(riid, &IID_IUnknown) ||
107 IsEqualGUID(riid, &IID_ITask))
109 *ppvObject = &This->ITask_iface;
110 ITask_AddRef(iface);
111 return S_OK;
113 else if (IsEqualGUID(riid, &IID_IPersistFile))
115 *ppvObject = &This->IPersistFile_iface;
116 ITask_AddRef(iface);
117 return S_OK;
120 WARN("Unknown interface: %s\n", debugstr_guid(riid));
121 *ppvObject = NULL;
122 return E_NOINTERFACE;
125 static ULONG WINAPI MSTASK_ITask_AddRef(
126 ITask* iface)
128 TaskImpl *This = impl_from_ITask(iface);
129 ULONG ref;
130 TRACE("\n");
131 ref = InterlockedIncrement(&This->ref);
132 return ref;
135 static ULONG WINAPI MSTASK_ITask_Release(
136 ITask* iface)
138 TaskImpl * This = impl_from_ITask(iface);
139 ULONG ref;
140 TRACE("\n");
141 ref = InterlockedDecrement(&This->ref);
142 if (ref == 0)
143 TaskDestructor(This);
144 return ref;
147 HRESULT task_set_trigger(ITask *task, WORD idx, const TASK_TRIGGER *src)
149 TaskImpl *This = impl_from_ITask(task);
150 TIME_FIELDS field_time;
151 LARGE_INTEGER sys_time;
152 TASK_TRIGGER dst;
154 TRACE("(%p, %u, %p)\n", task, idx, src);
156 if (idx >= This->trigger_count)
157 return E_FAIL;
159 /* Verify valid structure size */
160 if (src->cbTriggerSize != sizeof(*src))
161 return E_INVALIDARG;
162 dst.cbTriggerSize = src->cbTriggerSize;
164 /* Reserved field must be zero */
165 dst.Reserved1 = 0;
167 /* Verify and set valid start date and time */
168 memset(&field_time, 0, sizeof(field_time));
169 field_time.Year = src->wBeginYear;
170 field_time.Month = src->wBeginMonth;
171 field_time.Day = src->wBeginDay;
172 field_time.Hour = src->wStartHour;
173 field_time.Minute = src->wStartMinute;
174 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
175 return E_INVALIDARG;
176 dst.wBeginYear = src->wBeginYear;
177 dst.wBeginMonth = src->wBeginMonth;
178 dst.wBeginDay = src->wBeginDay;
179 dst.wStartHour = src->wStartHour;
180 dst.wStartMinute = src->wStartMinute;
182 /* Verify valid end date if TASK_TRIGGER_FLAG_HAS_END_DATE flag is set */
183 if (src->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE)
185 memset(&field_time, 0, sizeof(field_time));
186 field_time.Year = src->wEndYear;
187 field_time.Month = src->wEndMonth;
188 field_time.Day = src->wEndDay;
189 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
190 return E_INVALIDARG;
193 /* Set valid end date independent of TASK_TRIGGER_FLAG_HAS_END_DATE flag */
194 dst.wEndYear = src->wEndYear;
195 dst.wEndMonth = src->wEndMonth;
196 dst.wEndDay = src->wEndDay;
198 /* Verify duration and interval pair */
199 if (src->MinutesDuration <= src->MinutesInterval && src->MinutesInterval > 0)
200 return E_INVALIDARG;
201 dst.MinutesDuration = src->MinutesDuration;
202 dst.MinutesInterval = src->MinutesInterval;
204 /* Copy over flags */
205 dst.rgFlags = src->rgFlags;
207 /* Set TriggerType dependent fields of Type union */
208 dst.TriggerType = src->TriggerType;
209 switch (src->TriggerType)
211 case TASK_TIME_TRIGGER_DAILY:
212 dst.Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
213 break;
214 case TASK_TIME_TRIGGER_WEEKLY:
215 dst.Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
216 dst.Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
217 break;
218 case TASK_TIME_TRIGGER_MONTHLYDATE:
219 dst.Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
220 dst.Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
221 break;
222 case TASK_TIME_TRIGGER_MONTHLYDOW:
223 dst.Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
224 dst.Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
225 dst.Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
226 break;
227 case TASK_TIME_TRIGGER_ONCE:
228 case TASK_EVENT_TRIGGER_ON_IDLE:
229 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
230 case TASK_EVENT_TRIGGER_AT_LOGON:
231 default:
232 dst.Type = src->Type;
233 break;
236 /* Reserved field must be zero */
237 dst.Reserved2 = 0;
239 /* wRandomMinutesInterval not currently used and is initialized to zero */
240 dst.wRandomMinutesInterval = 0;
242 This->trigger[idx] = dst;
244 return S_OK;
247 HRESULT task_get_trigger(ITask *task, WORD idx, TASK_TRIGGER *dst)
249 TaskImpl *This = impl_from_ITask(task);
250 TASK_TRIGGER *src;
252 TRACE("(%p, %u, %p)\n", task, idx, dst);
254 if (idx >= This->trigger_count)
255 return SCHED_E_TRIGGER_NOT_FOUND;
257 src = &This->trigger[idx];
259 /* Native implementation doesn't verify equivalent cbTriggerSize fields */
261 /* Copy relevant fields of the structure */
262 dst->cbTriggerSize = src->cbTriggerSize;
263 dst->Reserved1 = 0;
264 dst->wBeginYear = src->wBeginYear;
265 dst->wBeginMonth = src->wBeginMonth;
266 dst->wBeginDay = src->wBeginDay;
267 dst->wEndYear = src->wEndYear;
268 dst->wEndMonth = src->wEndMonth;
269 dst->wEndDay = src->wEndDay;
270 dst->wStartHour = src->wStartHour;
271 dst->wStartMinute = src->wStartMinute;
272 dst->MinutesDuration = src->MinutesDuration;
273 dst->MinutesInterval = src->MinutesInterval;
274 dst->rgFlags = src->rgFlags;
275 dst->TriggerType = src->TriggerType;
276 switch (src->TriggerType)
278 case TASK_TIME_TRIGGER_DAILY:
279 dst->Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
280 break;
281 case TASK_TIME_TRIGGER_WEEKLY:
282 dst->Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
283 dst->Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
284 break;
285 case TASK_TIME_TRIGGER_MONTHLYDATE:
286 dst->Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
287 dst->Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
288 break;
289 case TASK_TIME_TRIGGER_MONTHLYDOW:
290 dst->Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
291 dst->Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
292 dst->Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
293 break;
294 case TASK_TIME_TRIGGER_ONCE:
295 case TASK_EVENT_TRIGGER_ON_IDLE:
296 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
297 case TASK_EVENT_TRIGGER_AT_LOGON:
298 default:
299 break;
301 dst->Reserved2 = 0;
302 dst->wRandomMinutesInterval = 0;
304 return S_OK;
307 static HRESULT WINAPI MSTASK_ITask_CreateTrigger(ITask *iface, WORD *idx, ITaskTrigger **task_trigger)
309 TaskImpl *This = impl_from_ITask(iface);
310 TASK_TRIGGER *new_trigger;
311 SYSTEMTIME time;
312 HRESULT hr;
314 TRACE("(%p, %p, %p)\n", iface, idx, task_trigger);
316 hr = TaskTriggerConstructor(iface, This->trigger_count, task_trigger);
317 if (hr != S_OK) return hr;
319 if (This->trigger)
320 new_trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * (This->trigger_count + 1));
321 else
322 new_trigger = heap_alloc(sizeof(This->trigger[0]));
323 if (!new_trigger)
325 ITaskTrigger_Release(*task_trigger);
326 return E_OUTOFMEMORY;
329 This->trigger = new_trigger;
331 new_trigger = &This->trigger[This->trigger_count];
333 /* Most fields default to zero. Initialize other fields to default values. */
334 memset(new_trigger, 0, sizeof(*new_trigger));
335 GetLocalTime(&time);
336 new_trigger->cbTriggerSize = sizeof(*new_trigger);
337 new_trigger->wBeginYear = time.wYear;
338 new_trigger->wBeginMonth = time.wMonth;
339 new_trigger->wBeginDay = time.wDay;
340 new_trigger->wStartHour = time.wHour;
341 new_trigger->wStartMinute = time.wMinute;
342 new_trigger->rgFlags = TASK_TRIGGER_FLAG_DISABLED;
343 new_trigger->TriggerType = TASK_TIME_TRIGGER_DAILY,
344 new_trigger->Type.Daily.DaysInterval = 1;
346 *idx = This->trigger_count++;
348 return hr;
351 static HRESULT WINAPI MSTASK_ITask_DeleteTrigger(ITask *iface, WORD idx)
353 TaskImpl *This = impl_from_ITask(iface);
355 TRACE("(%p, %u)\n", iface, idx);
357 if (idx >= This->trigger_count)
358 return SCHED_E_TRIGGER_NOT_FOUND;
360 This->trigger_count--;
361 memmove(&This->trigger[idx], &This->trigger[idx + 1], (This->trigger_count - idx) * sizeof(This->trigger[0]));
362 /* this shouldn't fail in practice since we're shrinking the memory block */
363 This->trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * This->trigger_count);
365 return S_OK;
368 static HRESULT WINAPI MSTASK_ITask_GetTriggerCount(ITask *iface, WORD *count)
370 TaskImpl *This = impl_from_ITask(iface);
372 TRACE("(%p, %p)\n", iface, count);
374 *count = This->trigger_count;
375 return S_OK;
378 static HRESULT WINAPI MSTASK_ITask_GetTrigger(ITask *iface, WORD idx, ITaskTrigger **trigger)
380 TaskImpl *This = impl_from_ITask(iface);
382 TRACE("(%p, %u, %p)\n", iface, idx, trigger);
384 if (idx >= This->trigger_count)
385 return SCHED_E_TRIGGER_NOT_FOUND;
387 return TaskTriggerConstructor(iface, idx, trigger);
390 static HRESULT WINAPI MSTASK_ITask_GetTriggerString(
391 ITask* iface,
392 WORD iTrigger,
393 LPWSTR *ppwszTrigger)
395 FIXME("(%p, %d, %p): stub\n", iface, iTrigger, ppwszTrigger);
396 return E_NOTIMPL;
399 static HRESULT WINAPI MSTASK_ITask_GetRunTimes(
400 ITask* iface,
401 const LPSYSTEMTIME pstBegin,
402 const LPSYSTEMTIME pstEnd,
403 WORD *pCount,
404 LPSYSTEMTIME *rgstTaskTimes)
406 FIXME("(%p, %p, %p, %p, %p): stub\n", iface, pstBegin, pstEnd, pCount,
407 rgstTaskTimes);
408 return E_NOTIMPL;
411 static void get_begin_time(const TASK_TRIGGER *trigger, FILETIME *ft)
413 SYSTEMTIME st;
415 st.wYear = trigger->wBeginYear;
416 st.wMonth = trigger->wBeginMonth;
417 st.wDay = trigger->wBeginDay;
418 st.wDayOfWeek = 0;
419 st.wHour = 0;
420 st.wMinute = 0;
421 st.wSecond = 0;
422 st.wMilliseconds = 0;
423 SystemTimeToFileTime(&st, ft);
426 static void get_end_time(const TASK_TRIGGER *trigger, FILETIME *ft)
428 SYSTEMTIME st;
430 if (!(trigger->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE))
432 ft->dwHighDateTime = ~0u;
433 ft->dwLowDateTime = ~0u;
434 return;
437 st.wYear = trigger->wEndYear;
438 st.wMonth = trigger->wEndMonth;
439 st.wDay = trigger->wEndDay;
440 st.wDayOfWeek = 0;
441 st.wHour = 0;
442 st.wMinute = 0;
443 st.wSecond = 0;
444 st.wMilliseconds = 0;
445 SystemTimeToFileTime(&st, ft);
448 static void filetime_add_ms(FILETIME *ft, ULONGLONG ms)
450 union u_ftll
452 FILETIME ft;
453 ULONGLONG ll;
454 } *ftll = (union u_ftll *)ft;
456 ftll->ll += ms * (ULONGLONG)10000;
459 static void filetime_add_hours(FILETIME *ft, ULONG hours)
461 filetime_add_ms(ft, (ULONGLONG)hours * 60 * 60 * 1000);
464 static void filetime_add_days(FILETIME *ft, ULONG days)
466 filetime_add_hours(ft, (ULONGLONG)days * 24);
469 static HRESULT WINAPI MSTASK_ITask_GetNextRunTime(ITask *iface, SYSTEMTIME *rt)
471 TaskImpl *This = impl_from_ITask(iface);
472 HRESULT hr = SCHED_S_TASK_NO_VALID_TRIGGERS;
473 SYSTEMTIME st, current_st;
474 FILETIME current_ft, begin_ft, end_ft, best_ft;
475 BOOL have_best_time = FALSE;
476 DWORD i;
478 TRACE("(%p, %p)\n", iface, rt);
480 if (This->flags & TASK_FLAG_DISABLED)
482 memset(rt, 0, sizeof(*rt));
483 return SCHED_S_TASK_DISABLED;
486 GetLocalTime(&current_st);
488 for (i = 0; i < This->trigger_count; i++)
490 if (!(This->trigger[i].rgFlags & TASK_TRIGGER_FLAG_DISABLED))
492 get_begin_time(&This->trigger[i], &begin_ft);
493 get_end_time(&This->trigger[i], &end_ft);
495 switch (This->trigger[i].TriggerType)
497 case TASK_EVENT_TRIGGER_ON_IDLE:
498 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
499 case TASK_EVENT_TRIGGER_AT_LOGON:
500 hr = SCHED_S_EVENT_TRIGGER;
501 break;
503 case TASK_TIME_TRIGGER_ONCE:
504 st = current_st;
505 st.wHour = This->trigger[i].wStartHour;
506 st.wMinute = This->trigger[i].wStartMinute;
507 st.wSecond = 0;
508 st.wMilliseconds = 0;
509 SystemTimeToFileTime(&st, &current_ft);
510 if (CompareFileTime(&begin_ft, &current_ft) <= 0 && CompareFileTime(&current_ft, &end_ft) < 0)
512 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
514 best_ft = current_ft;
515 have_best_time = TRUE;
518 break;
520 case TASK_TIME_TRIGGER_DAILY:
521 st = current_st;
522 st.wHour = This->trigger[i].wStartHour;
523 st.wMinute = This->trigger[i].wStartMinute;
524 st.wSecond = 0;
525 st.wMilliseconds = 0;
526 SystemTimeToFileTime(&st, &current_ft);
527 while (CompareFileTime(&current_ft, &end_ft) < 0)
529 if (CompareFileTime(&current_ft, &begin_ft) >= 0)
531 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
533 best_ft = current_ft;
534 have_best_time = TRUE;
536 break;
539 filetime_add_days(&current_ft, This->trigger[i].Type.Daily.DaysInterval);
541 break;
543 default:
544 FIXME("trigger type %u is not handled\n", This->trigger[i].TriggerType);
545 break;
550 if (have_best_time)
552 FileTimeToSystemTime(&best_ft, rt);
553 return S_OK;
556 memset(rt, 0, sizeof(*rt));
557 return hr;
560 static HRESULT WINAPI MSTASK_ITask_SetIdleWait(
561 ITask* iface,
562 WORD wIdleMinutes,
563 WORD wDeadlineMinutes)
565 FIXME("(%p, %d, %d): stub\n", iface, wIdleMinutes, wDeadlineMinutes);
566 return E_NOTIMPL;
569 static HRESULT WINAPI MSTASK_ITask_GetIdleWait(ITask *iface, WORD *idle_minutes, WORD *deadline_minutes)
571 TaskImpl *This = impl_from_ITask(iface);
573 TRACE("(%p, %p, %p): stub\n", iface, idle_minutes, deadline_minutes);
575 *idle_minutes = This->idle_minutes;
576 *deadline_minutes = This->deadline_minutes;
577 return S_OK;
580 static HRESULT WINAPI MSTASK_ITask_Run(ITask *iface)
582 TaskImpl *This = impl_from_ITask(iface);
584 TRACE("(%p)\n", iface);
586 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
587 return SCHED_E_TASK_NOT_READY;
589 This->flags |= 0x04000000;
590 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
593 static HRESULT WINAPI MSTASK_ITask_Terminate(ITask *iface)
595 TaskImpl *This = impl_from_ITask(iface);
597 TRACE("(%p)\n", iface);
599 if (!This->instance_count)
600 return SCHED_E_TASK_NOT_RUNNING;
602 This->flags |= 0x08000000;
603 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
606 static HRESULT WINAPI MSTASK_ITask_EditWorkItem(
607 ITask* iface,
608 HWND hParent,
609 DWORD dwReserved)
611 FIXME("(%p, %p, %d): stub\n", iface, hParent, dwReserved);
612 return E_NOTIMPL;
615 static HRESULT WINAPI MSTASK_ITask_GetMostRecentRunTime(ITask *iface, SYSTEMTIME *st)
617 FIXME("(%p, %p): stub\n", iface, st);
619 memset(st, 0, sizeof(*st));
620 return SCHED_S_TASK_HAS_NOT_RUN;
623 static HRESULT WINAPI MSTASK_ITask_GetStatus(ITask *iface, HRESULT *status)
625 TaskImpl *This = impl_from_ITask(iface);
627 TRACE("(%p, %p)\n", iface, status);
629 *status = This->instance_count ? SCHED_S_TASK_RUNNING : This->status;
630 return S_OK;
633 static HRESULT WINAPI MSTASK_ITask_GetExitCode(ITask *iface, DWORD *exit_code)
635 TaskImpl *This = impl_from_ITask(iface);
637 TRACE("(%p, %p)\n", iface, exit_code);
639 *exit_code = This->exit_code;
640 return SCHED_S_TASK_HAS_NOT_RUN; /* FIXME */
643 static HRESULT WINAPI MSTASK_ITask_SetComment(ITask *iface, LPCWSTR comment)
645 TaskImpl *This = impl_from_ITask(iface);
646 IRegistrationInfo *info;
647 HRESULT hr;
649 TRACE("(%p, %s)\n", iface, debugstr_w(comment));
651 if (!comment || !comment[0])
652 comment = NULL;
654 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
655 if (hr == S_OK)
657 hr = IRegistrationInfo_put_Description(info, (BSTR)comment);
658 IRegistrationInfo_Release(info);
659 This->is_dirty = TRUE;
661 return hr;
664 static HRESULT WINAPI MSTASK_ITask_GetComment(ITask *iface, LPWSTR *comment)
666 TaskImpl *This = impl_from_ITask(iface);
667 IRegistrationInfo *info;
668 HRESULT hr;
669 BSTR description;
670 DWORD len;
672 TRACE("(%p, %p)\n", iface, comment);
674 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
675 if (hr != S_OK) return hr;
677 hr = IRegistrationInfo_get_Description(info, &description);
678 if (hr == S_OK)
680 len = description ? lstrlenW(description) + 1 : 1;
681 *comment = CoTaskMemAlloc(len * sizeof(WCHAR));
682 if (*comment)
684 if (!description)
685 *comment[0] = 0;
686 else
687 lstrcpyW(*comment, description);
688 hr = S_OK;
690 else
691 hr = E_OUTOFMEMORY;
693 SysFreeString(description);
696 IRegistrationInfo_Release(info);
697 return hr;
700 static HRESULT WINAPI MSTASK_ITask_SetCreator(ITask *iface, LPCWSTR creator)
702 TaskImpl *This = impl_from_ITask(iface);
703 IRegistrationInfo *info;
704 HRESULT hr;
706 TRACE("(%p, %s)\n", iface, debugstr_w(creator));
708 if (!creator || !creator[0])
709 creator = NULL;
711 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
712 if (hr == S_OK)
714 hr = IRegistrationInfo_put_Author(info, (BSTR)creator);
715 IRegistrationInfo_Release(info);
716 This->is_dirty = TRUE;
718 return hr;
721 static HRESULT WINAPI MSTASK_ITask_GetCreator(ITask *iface, LPWSTR *creator)
723 TaskImpl *This = impl_from_ITask(iface);
724 IRegistrationInfo *info;
725 HRESULT hr;
726 BSTR author;
727 DWORD len;
729 TRACE("(%p, %p)\n", iface, creator);
731 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
732 if (hr != S_OK) return hr;
734 hr = IRegistrationInfo_get_Author(info, &author);
735 if (hr == S_OK)
737 len = author ? lstrlenW(author) + 1 : 1;
738 *creator = CoTaskMemAlloc(len * sizeof(WCHAR));
739 if (*creator)
741 if (!author)
742 *creator[0] = 0;
743 else
744 lstrcpyW(*creator, author);
745 hr = S_OK;
747 else
748 hr = E_OUTOFMEMORY;
750 SysFreeString(author);
753 IRegistrationInfo_Release(info);
754 return hr;
757 static HRESULT WINAPI MSTASK_ITask_SetWorkItemData(
758 ITask* iface,
759 WORD cBytes,
760 BYTE rgbData[])
762 FIXME("(%p, %d, %p): stub\n", iface, cBytes, rgbData);
763 return E_NOTIMPL;
766 static HRESULT WINAPI MSTASK_ITask_GetWorkItemData(
767 ITask* iface,
768 WORD *pcBytes,
769 BYTE **ppBytes)
771 FIXME("(%p, %p, %p): stub\n", iface, pcBytes, ppBytes);
772 return E_NOTIMPL;
775 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryCount(
776 ITask* iface,
777 WORD wRetryCount)
779 FIXME("(%p, %d): stub\n", iface, wRetryCount);
780 return E_NOTIMPL;
783 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryCount(ITask *iface, WORD *count)
785 TRACE("(%p, %p)\n", iface, count);
786 return E_NOTIMPL;
789 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryInterval(
790 ITask* iface,
791 WORD wRetryInterval)
793 FIXME("(%p, %d): stub\n", iface, wRetryInterval);
794 return E_NOTIMPL;
797 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryInterval(ITask *iface, WORD *interval)
799 TRACE("(%p, %p)\n", iface, interval);
800 return E_NOTIMPL;
803 static HRESULT WINAPI MSTASK_ITask_SetFlags(ITask *iface, DWORD flags)
805 TaskImpl *This = impl_from_ITask(iface);
807 TRACE("(%p, 0x%08x)\n", iface, flags);
808 This->flags &= 0xffff8000;
809 This->flags |= flags & 0x7fff;
810 This->is_dirty = TRUE;
811 return S_OK;
814 static HRESULT WINAPI MSTASK_ITask_GetFlags(ITask *iface, DWORD *flags)
816 TaskImpl *This = impl_from_ITask(iface);
818 TRACE("(%p, %p)\n", iface, flags);
819 *flags = LOWORD(This->flags);
820 return S_OK;
823 static HRESULT WINAPI MSTASK_ITask_SetAccountInformation(
824 ITask* iface,
825 LPCWSTR pwszAccountName,
826 LPCWSTR pwszPassword)
828 DWORD n;
829 TaskImpl *This = impl_from_ITask(iface);
830 LPWSTR tmp_account_name;
832 TRACE("(%p, %s, %s): partial stub\n", iface, debugstr_w(pwszAccountName),
833 debugstr_w(pwszPassword));
835 if (pwszPassword)
836 FIXME("Partial stub ignores passwords\n");
838 n = (lstrlenW(pwszAccountName) + 1);
839 tmp_account_name = heap_alloc(n * sizeof(WCHAR));
840 if (!tmp_account_name)
841 return E_OUTOFMEMORY;
842 lstrcpyW(tmp_account_name, pwszAccountName);
843 heap_free(This->accountName);
844 This->accountName = tmp_account_name;
845 This->is_dirty = TRUE;
846 return S_OK;
849 static HRESULT WINAPI MSTASK_ITask_GetAccountInformation(
850 ITask* iface,
851 LPWSTR *ppwszAccountName)
853 DWORD n;
854 TaskImpl *This = impl_from_ITask(iface);
856 TRACE("(%p, %p): partial stub\n", iface, ppwszAccountName);
858 /* This implements the WinXP behavior when accountName has not yet
859 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
860 if (!This->accountName)
861 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
863 n = (lstrlenW(This->accountName) + 1);
864 *ppwszAccountName = CoTaskMemAlloc(n * sizeof(WCHAR));
865 if (!*ppwszAccountName)
866 return E_OUTOFMEMORY;
867 lstrcpyW(*ppwszAccountName, This->accountName);
868 return S_OK;
871 static HRESULT WINAPI MSTASK_ITask_SetApplicationName(ITask *iface, LPCWSTR appname)
873 TaskImpl *This = impl_from_ITask(iface);
874 DWORD len;
875 HRESULT hr;
877 TRACE("(%p, %s)\n", iface, debugstr_w(appname));
879 /* Empty application name */
880 if (!appname || !appname[0])
881 return IExecAction_put_Path(This->action, NULL);
883 /* Attempt to set pwszApplicationName to a path resolved application name */
884 len = SearchPathW(NULL, appname, NULL, 0, NULL, NULL);
885 if (len)
887 LPWSTR tmp_name;
889 tmp_name = heap_alloc(len * sizeof(WCHAR));
890 if (!tmp_name)
891 return E_OUTOFMEMORY;
892 len = SearchPathW(NULL, appname, NULL, len, tmp_name, NULL);
893 if (len)
895 hr = IExecAction_put_Path(This->action, tmp_name);
896 if (hr == S_OK) This->is_dirty = TRUE;
898 else
899 hr = HRESULT_FROM_WIN32(GetLastError());
901 heap_free(tmp_name);
902 return hr;
905 /* If unable to path resolve name, simply set to appname */
906 hr = IExecAction_put_Path(This->action, (BSTR)appname);
907 if (hr == S_OK) This->is_dirty = TRUE;
908 return hr;
911 static HRESULT WINAPI MSTASK_ITask_GetApplicationName(ITask *iface, LPWSTR *appname)
913 TaskImpl *This = impl_from_ITask(iface);
914 HRESULT hr;
915 BSTR path;
916 DWORD len;
918 TRACE("(%p, %p)\n", iface, appname);
920 hr = IExecAction_get_Path(This->action, &path);
921 if (hr != S_OK) return hr;
923 len = path ? lstrlenW(path) + 1 : 1;
924 *appname = CoTaskMemAlloc(len * sizeof(WCHAR));
925 if (*appname)
927 if (!path)
928 *appname[0] = 0;
929 else
930 lstrcpyW(*appname, path);
931 hr = S_OK;
933 else
934 hr = E_OUTOFMEMORY;
936 SysFreeString(path);
937 return hr;
940 static HRESULT WINAPI MSTASK_ITask_SetParameters(ITask *iface, LPCWSTR params)
942 TaskImpl *This = impl_from_ITask(iface);
943 HRESULT hr;
945 TRACE("(%p, %s)\n", iface, debugstr_w(params));
947 /* Empty parameter list */
948 if (!params || !params[0])
949 params = NULL;
951 hr = IExecAction_put_Arguments(This->action, (BSTR)params);
952 if (hr == S_OK) This->is_dirty = TRUE;
953 return hr;
956 static HRESULT WINAPI MSTASK_ITask_GetParameters(ITask *iface, LPWSTR *params)
958 TaskImpl *This = impl_from_ITask(iface);
959 HRESULT hr;
960 BSTR args;
961 DWORD len;
963 TRACE("(%p, %p)\n", iface, params);
965 hr = IExecAction_get_Arguments(This->action, &args);
966 if (hr != S_OK) return hr;
968 len = args ? lstrlenW(args) + 1 : 1;
969 *params = CoTaskMemAlloc(len * sizeof(WCHAR));
970 if (*params)
972 if (!args)
973 *params[0] = 0;
974 else
975 lstrcpyW(*params, args);
976 hr = S_OK;
978 else
979 hr = E_OUTOFMEMORY;
981 SysFreeString(args);
982 return hr;
985 static HRESULT WINAPI MSTASK_ITask_SetWorkingDirectory(ITask * iface, LPCWSTR workdir)
987 TaskImpl *This = impl_from_ITask(iface);
988 HRESULT hr;
990 TRACE("(%p, %s)\n", iface, debugstr_w(workdir));
992 if (!workdir || !workdir[0])
993 workdir = NULL;
995 hr = IExecAction_put_WorkingDirectory(This->action, (BSTR)workdir);
996 if (hr == S_OK) This->is_dirty = TRUE;
997 return hr;
1000 static HRESULT WINAPI MSTASK_ITask_GetWorkingDirectory(ITask *iface, LPWSTR *workdir)
1002 TaskImpl *This = impl_from_ITask(iface);
1003 HRESULT hr;
1004 BSTR dir;
1005 DWORD len;
1007 TRACE("(%p, %p)\n", iface, workdir);
1009 hr = IExecAction_get_WorkingDirectory(This->action, &dir);
1010 if (hr != S_OK) return hr;
1012 len = dir ? lstrlenW(dir) + 1 : 1;
1013 *workdir = CoTaskMemAlloc(len * sizeof(WCHAR));
1014 if (*workdir)
1016 if (!dir)
1017 *workdir[0] = 0;
1018 else
1019 lstrcpyW(*workdir, dir);
1020 hr = S_OK;
1022 else
1023 hr = E_OUTOFMEMORY;
1025 SysFreeString(dir);
1026 return hr;
1029 static HRESULT WINAPI MSTASK_ITask_SetPriority(
1030 ITask* iface,
1031 DWORD dwPriority)
1033 FIXME("(%p, 0x%08x): stub\n", iface, dwPriority);
1034 return E_NOTIMPL;
1037 static HRESULT WINAPI MSTASK_ITask_GetPriority(ITask *iface, DWORD *priority)
1039 TaskImpl *This = impl_from_ITask(iface);
1041 TRACE("(%p, %p)\n", iface, priority);
1043 *priority = This->priority;
1044 return S_OK;
1047 static HRESULT WINAPI MSTASK_ITask_SetTaskFlags(
1048 ITask* iface,
1049 DWORD dwFlags)
1051 FIXME("(%p, 0x%08x): stub\n", iface, dwFlags);
1052 return E_NOTIMPL;
1055 static HRESULT WINAPI MSTASK_ITask_GetTaskFlags(ITask *iface, DWORD *flags)
1057 FIXME("(%p, %p): stub\n", iface, flags);
1058 *flags = 0;
1059 return S_OK;
1062 static HRESULT WINAPI MSTASK_ITask_SetMaxRunTime(
1063 ITask* iface,
1064 DWORD dwMaxRunTime)
1066 TaskImpl *This = impl_from_ITask(iface);
1068 TRACE("(%p, %d)\n", iface, dwMaxRunTime);
1070 This->maxRunTime = dwMaxRunTime;
1071 This->is_dirty = TRUE;
1072 return S_OK;
1075 static HRESULT WINAPI MSTASK_ITask_GetMaxRunTime(
1076 ITask* iface,
1077 DWORD *pdwMaxRunTime)
1079 TaskImpl *This = impl_from_ITask(iface);
1081 TRACE("(%p, %p)\n", iface, pdwMaxRunTime);
1083 *pdwMaxRunTime = This->maxRunTime;
1084 return S_OK;
1087 static HRESULT WINAPI MSTASK_IPersistFile_QueryInterface(
1088 IPersistFile* iface,
1089 REFIID riid,
1090 void **ppvObject)
1092 TaskImpl *This = impl_from_IPersistFile(iface);
1093 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
1094 return ITask_QueryInterface(&This->ITask_iface, riid, ppvObject);
1097 static ULONG WINAPI MSTASK_IPersistFile_AddRef(
1098 IPersistFile* iface)
1100 TaskImpl *This = impl_from_IPersistFile(iface);
1101 return ITask_AddRef(&This->ITask_iface);
1104 static ULONG WINAPI MSTASK_IPersistFile_Release(
1105 IPersistFile* iface)
1107 TaskImpl *This = impl_from_IPersistFile(iface);
1108 return ITask_Release(&This->ITask_iface);
1111 static HRESULT WINAPI MSTASK_IPersistFile_GetClassID(IPersistFile *iface, CLSID *clsid)
1113 TRACE("(%p, %p)\n", iface, clsid);
1115 *clsid = CLSID_CTask;
1116 return S_OK;
1119 static HRESULT WINAPI MSTASK_IPersistFile_IsDirty(IPersistFile *iface)
1121 TaskImpl *This = impl_from_IPersistFile(iface);
1122 TRACE("(%p)\n", iface);
1123 return This->is_dirty ? S_OK : S_FALSE;
1126 static DWORD load_unicode_strings(ITask *task, BYTE *data, DWORD limit)
1128 DWORD i, data_size = 0;
1129 USHORT len;
1131 for (i = 0; i < 5; i++)
1133 if (limit < sizeof(USHORT))
1135 TRACE("invalid string %u offset\n", i);
1136 break;
1139 len = *(USHORT *)data;
1140 data += sizeof(USHORT);
1141 data_size += sizeof(USHORT);
1142 limit -= sizeof(USHORT);
1143 if (limit < len * sizeof(WCHAR))
1145 TRACE("invalid string %u size\n", i);
1146 break;
1149 TRACE("string %u: %s\n", i, wine_dbgstr_wn((const WCHAR *)data, len));
1151 switch (i)
1153 case 0:
1154 ITask_SetApplicationName(task, (const WCHAR *)data);
1155 break;
1156 case 1:
1157 ITask_SetParameters(task, (const WCHAR *)data);
1158 break;
1159 case 2:
1160 ITask_SetWorkingDirectory(task, (const WCHAR *)data);
1161 break;
1162 case 3:
1163 ITask_SetCreator(task, (const WCHAR *)data);
1164 break;
1165 case 4:
1166 ITask_SetComment(task, (const WCHAR *)data);
1167 break;
1168 default:
1169 break;
1172 data += len * sizeof(WCHAR);
1173 data_size += len * sizeof(WCHAR);
1176 return data_size;
1179 static HRESULT load_job_data(TaskImpl *This, BYTE *data, DWORD size)
1181 ITask *task = &This->ITask_iface;
1182 HRESULT hr;
1183 const FIXDLEN_DATA *fixed;
1184 const SYSTEMTIME *st;
1185 DWORD unicode_strings_size, data_size, triggers_size;
1186 USHORT trigger_count, i;
1187 const USHORT *signature;
1188 TASK_TRIGGER *task_trigger;
1190 if (size < sizeof(*fixed))
1192 TRACE("no space for FIXDLEN_DATA\n");
1193 return SCHED_E_INVALID_TASK;
1196 fixed = (const FIXDLEN_DATA *)data;
1198 TRACE("product_version %04x\n", fixed->product_version);
1199 TRACE("file_version %04x\n", fixed->file_version);
1200 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed->uuid));
1202 if (fixed->file_version != 0x0001)
1203 return SCHED_E_INVALID_TASK;
1205 TRACE("name_size_offset %04x\n", fixed->name_size_offset);
1206 TRACE("trigger_offset %04x\n", fixed->trigger_offset);
1207 TRACE("error_retry_count %u\n", fixed->error_retry_count);
1208 TRACE("error_retry_interval %u\n", fixed->error_retry_interval);
1209 TRACE("idle_deadline %u\n", fixed->idle_deadline);
1210 This->deadline_minutes = fixed->idle_deadline;
1211 TRACE("idle_wait %u\n", fixed->idle_wait);
1212 This->idle_minutes = fixed->idle_wait;
1213 TRACE("priority %08x\n", fixed->priority);
1214 This->priority = fixed->priority;
1215 TRACE("maximum_runtime %u\n", fixed->maximum_runtime);
1216 This->maxRunTime = fixed->maximum_runtime;
1217 TRACE("exit_code %#x\n", fixed->exit_code);
1218 This->exit_code = fixed->exit_code;
1219 TRACE("status %08x\n", fixed->status);
1220 This->status = fixed->status;
1221 TRACE("flags %08x\n", fixed->flags);
1222 This->flags = fixed->flags;
1223 st = &fixed->last_runtime;
1224 TRACE("last_runtime %d/%d/%d wday %d %d:%d:%d.%03d\n",
1225 st->wDay, st->wMonth, st->wYear, st->wDayOfWeek,
1226 st->wHour, st->wMinute, st->wSecond, st->wMilliseconds);
1228 /* Instance Count */
1229 if (size < sizeof(*fixed) + sizeof(USHORT))
1231 TRACE("no space for instance count\n");
1232 return SCHED_E_INVALID_TASK;
1235 This->instance_count = *(const USHORT *)(data + sizeof(*fixed));
1236 TRACE("instance count %u\n", This->instance_count);
1238 if (fixed->name_size_offset + sizeof(USHORT) < size)
1239 unicode_strings_size = load_unicode_strings(task, data + fixed->name_size_offset, size - fixed->name_size_offset);
1240 else
1242 TRACE("invalid name_size_offset\n");
1243 return SCHED_E_INVALID_TASK;
1245 TRACE("unicode strings end at %#x\n", fixed->name_size_offset + unicode_strings_size);
1247 if (size < fixed->trigger_offset + sizeof(USHORT))
1249 TRACE("no space for triggers count\n");
1250 return SCHED_E_INVALID_TASK;
1252 trigger_count = *(const USHORT *)(data + fixed->trigger_offset);
1253 TRACE("trigger_count %u\n", trigger_count);
1254 triggers_size = size - fixed->trigger_offset - sizeof(USHORT);
1255 TRACE("triggers_size %u\n", triggers_size);
1256 task_trigger = (TASK_TRIGGER *)(data + fixed->trigger_offset + sizeof(USHORT));
1258 data += fixed->name_size_offset + unicode_strings_size;
1259 size -= fixed->name_size_offset + unicode_strings_size;
1261 /* User Data */
1262 if (size < sizeof(USHORT))
1264 TRACE("no space for user data size\n");
1265 return SCHED_E_INVALID_TASK;
1268 data_size = *(const USHORT *)data;
1269 if (size < sizeof(USHORT) + data_size)
1271 TRACE("no space for user data\n");
1272 return SCHED_E_INVALID_TASK;
1274 TRACE("User Data size %#x\n", data_size);
1275 ITask_SetWorkItemData(task, data_size, data + sizeof(USHORT));
1277 size -= sizeof(USHORT) + data_size;
1278 data += sizeof(USHORT) + data_size;
1280 /* Reserved Data */
1281 if (size < sizeof(USHORT))
1283 TRACE("no space for reserved data size\n");
1284 return SCHED_E_INVALID_TASK;
1287 data_size = *(const USHORT *)data;
1288 if (size < sizeof(USHORT) + data_size)
1290 TRACE("no space for reserved data\n");
1291 return SCHED_E_INVALID_TASK;
1293 TRACE("Reserved Data size %#x\n", data_size);
1295 size -= sizeof(USHORT) + data_size;
1296 data += sizeof(USHORT) + data_size;
1298 /* Trigger Data */
1299 TRACE("trigger_offset %04x, triggers end at %04x\n", fixed->trigger_offset,
1300 (DWORD)(fixed->trigger_offset + sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER)));
1302 task_trigger = (TASK_TRIGGER *)(data + sizeof(USHORT));
1304 if (trigger_count * sizeof(TASK_TRIGGER) > triggers_size)
1306 TRACE("no space for triggers data\n");
1307 return SCHED_E_INVALID_TASK;
1310 This->trigger_count = 0;
1312 for (i = 0; i < trigger_count; i++)
1314 ITaskTrigger *trigger;
1315 WORD idx;
1317 hr = ITask_CreateTrigger(task, &idx, &trigger);
1318 if (hr != S_OK) return hr;
1320 hr = ITaskTrigger_SetTrigger(trigger, &task_trigger[i]);
1321 ITaskTrigger_Release(trigger);
1322 if (hr != S_OK)
1324 ITask_DeleteTrigger(task, idx);
1325 return hr;
1329 size -= sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1330 data += sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1332 if (size < 2 * sizeof(USHORT) + 64)
1334 TRACE("no space for signature\n");
1335 return S_OK; /* signature is optional */
1338 signature = (const USHORT *)data;
1339 TRACE("signature version %04x, client version %04x\n", signature[0], signature[1]);
1341 return S_OK;
1344 static HRESULT WINAPI MSTASK_IPersistFile_Load(IPersistFile *iface, LPCOLESTR file_name, DWORD mode)
1346 TaskImpl *This = impl_from_IPersistFile(iface);
1347 HRESULT hr;
1348 HANDLE file, mapping;
1349 DWORD access, sharing, size;
1350 void *data;
1352 TRACE("(%p, %s, 0x%08x)\n", iface, debugstr_w(file_name), mode);
1354 switch (mode & 0x000f)
1356 default:
1357 case STGM_READ:
1358 access = GENERIC_READ;
1359 break;
1360 case STGM_WRITE:
1361 case STGM_READWRITE:
1362 access = GENERIC_READ | GENERIC_WRITE;
1363 break;
1366 switch (mode & 0x00f0)
1368 default:
1369 case STGM_SHARE_DENY_NONE:
1370 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1371 break;
1372 case STGM_SHARE_DENY_READ:
1373 sharing = FILE_SHARE_WRITE;
1374 break;
1375 case STGM_SHARE_DENY_WRITE:
1376 sharing = FILE_SHARE_READ;
1377 break;
1378 case STGM_SHARE_EXCLUSIVE:
1379 sharing = 0;
1380 break;
1383 file = CreateFileW(file_name, access, sharing, NULL, OPEN_EXISTING, 0, 0);
1384 if (file == INVALID_HANDLE_VALUE)
1386 TRACE("Failed to open %s, error %u\n", debugstr_w(file_name), GetLastError());
1387 return HRESULT_FROM_WIN32(GetLastError());
1390 size = GetFileSize(file, NULL);
1392 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, 0);
1393 if (!mapping)
1395 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name), GetLastError());
1396 CloseHandle(file);
1397 return HRESULT_FROM_WIN32(GetLastError());
1400 data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1401 if (data)
1403 hr = load_job_data(This, data, size);
1404 if (hr == S_OK) This->is_dirty = FALSE;
1405 UnmapViewOfFile(data);
1407 else
1408 hr = HRESULT_FROM_WIN32(GetLastError());
1410 CloseHandle(mapping);
1411 CloseHandle(file);
1413 return hr;
1416 static BOOL write_signature(HANDLE hfile)
1418 struct
1420 USHORT SignatureVersion;
1421 USHORT ClientVersion;
1422 BYTE md5[64];
1423 } signature;
1424 DWORD size;
1426 signature.SignatureVersion = 0x0001;
1427 signature.ClientVersion = 0x0001;
1428 memset(&signature.md5, 0, sizeof(signature.md5));
1430 return WriteFile(hfile, &signature, sizeof(signature), &size, NULL);
1433 static BOOL write_reserved_data(HANDLE hfile)
1435 static const struct
1437 USHORT size;
1438 BYTE data[8];
1439 } user = { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1440 DWORD size;
1442 return WriteFile(hfile, &user, sizeof(user), &size, NULL);
1445 static BOOL write_user_data(HANDLE hfile, BYTE *data, WORD data_size)
1447 DWORD size;
1449 if (!WriteFile(hfile, &data_size, sizeof(data_size), &size, NULL))
1450 return FALSE;
1452 if (!data_size) return TRUE;
1454 return WriteFile(hfile, data, data_size, &size, NULL);
1457 static HRESULT write_triggers(TaskImpl *This, HANDLE hfile)
1459 WORD count, i, idx = 0xffff;
1460 DWORD size;
1461 HRESULT hr = S_OK;
1462 ITaskTrigger *trigger;
1464 count = This->trigger_count;
1466 /* Windows saves a .job with at least 1 trigger */
1467 if (!count)
1469 hr = ITask_CreateTrigger(&This->ITask_iface, &idx, &trigger);
1470 if (hr != S_OK) return hr;
1471 ITaskTrigger_Release(trigger);
1473 count = 1;
1476 if (WriteFile(hfile, &count, sizeof(count), &size, NULL))
1478 for (i = 0; i < count; i++)
1480 if (!WriteFile(hfile, &This->trigger[i], sizeof(This->trigger[0]), &size, NULL))
1482 hr = HRESULT_FROM_WIN32(GetLastError());
1483 break;
1487 else
1488 hr = HRESULT_FROM_WIN32(GetLastError());
1490 if (idx != 0xffff)
1491 ITask_DeleteTrigger(&This->ITask_iface, idx);
1493 return hr;
1496 static BOOL write_unicode_string(HANDLE hfile, const WCHAR *str)
1498 USHORT count;
1499 DWORD size;
1501 count = str ? (lstrlenW(str) + 1) : 0;
1502 if (!WriteFile(hfile, &count, sizeof(count), &size, NULL))
1503 return FALSE;
1505 if (!str) return TRUE;
1507 count *= sizeof(WCHAR);
1508 return WriteFile(hfile, str, count, &size, NULL);
1511 static HRESULT WINAPI MSTASK_IPersistFile_Save(IPersistFile *iface, LPCOLESTR task_name, BOOL remember)
1513 static WCHAR authorW[] = { 'W','i','n','e',0 };
1514 static WCHAR commentW[] = { 'C','r','e','a','t','e','d',' ','b','y',' ','W','i','n','e',0 };
1515 FIXDLEN_DATA fixed;
1516 WORD word, user_data_size = 0;
1517 HANDLE hfile;
1518 DWORD size, ver, disposition, try;
1519 TaskImpl *This = impl_from_IPersistFile(iface);
1520 ITask *task = &This->ITask_iface;
1521 LPWSTR appname = NULL, params = NULL, workdir = NULL, creator = NULL, comment = NULL;
1522 BYTE *user_data = NULL;
1523 HRESULT hr;
1525 TRACE("(%p, %s, %d)\n", iface, debugstr_w(task_name), remember);
1527 disposition = task_name ? CREATE_NEW : OPEN_ALWAYS;
1529 if (!task_name)
1531 task_name = This->task_name;
1532 remember = FALSE;
1535 ITask_GetComment(task, &comment);
1536 if (!comment) comment = commentW;
1537 ITask_GetCreator(task, &creator);
1538 if (!creator) creator = authorW;
1539 ITask_GetApplicationName(task, &appname);
1540 ITask_GetParameters(task, &params);
1541 ITask_GetWorkingDirectory(task, &workdir);
1542 ITask_GetWorkItemData(task, &user_data_size, &user_data);
1544 ver = GetVersion();
1545 fixed.product_version = MAKEWORD(ver >> 8, ver);
1546 fixed.file_version = 0x0001;
1547 CoCreateGuid(&fixed.uuid);
1548 fixed.name_size_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1549 fixed.trigger_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1550 fixed.trigger_offset += sizeof(USHORT); /* Application Name */
1551 if (appname)
1552 fixed.trigger_offset += (lstrlenW(appname) + 1) * sizeof(WCHAR);
1553 fixed.trigger_offset += sizeof(USHORT); /* Parameters */
1554 if (params)
1555 fixed.trigger_offset += (lstrlenW(params) + 1) * sizeof(WCHAR);
1556 fixed.trigger_offset += sizeof(USHORT); /* Working Directory */
1557 if (workdir)
1558 fixed.trigger_offset += (lstrlenW(workdir) + 1) * sizeof(WCHAR);
1559 fixed.trigger_offset += sizeof(USHORT); /* Author */
1560 if (creator)
1561 fixed.trigger_offset += (lstrlenW(creator) + 1) * sizeof(WCHAR);
1562 fixed.trigger_offset += sizeof(USHORT); /* Comment */
1563 if (comment)
1564 fixed.trigger_offset += (lstrlenW(comment) + 1) * sizeof(WCHAR);
1565 fixed.trigger_offset += sizeof(USHORT) + user_data_size; /* User Data */
1566 fixed.trigger_offset += 10; /* Reserved Data */
1568 fixed.error_retry_count = 0;
1569 fixed.error_retry_interval = 0;
1570 fixed.idle_wait = This->idle_minutes;
1571 fixed.idle_deadline = This->deadline_minutes;
1572 fixed.priority = This->priority;
1573 fixed.maximum_runtime = This->maxRunTime;
1574 fixed.exit_code = This->exit_code;
1575 if (This->status == SCHED_S_TASK_NOT_SCHEDULED && This->trigger_count)
1576 This->status = SCHED_S_TASK_HAS_NOT_RUN;
1577 fixed.status = This->status;
1578 fixed.flags = This->flags;
1579 memset(&fixed.last_runtime, 0, sizeof(fixed.last_runtime));
1581 try = 1;
1582 for (;;)
1584 hfile = CreateFileW(task_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, disposition, 0, 0);
1585 if (hfile != INVALID_HANDLE_VALUE) break;
1587 if (try++ >= 3) return HRESULT_FROM_WIN32(GetLastError());
1588 Sleep(100);
1591 if (!WriteFile(hfile, &fixed, sizeof(fixed), &size, NULL))
1593 hr = HRESULT_FROM_WIN32(GetLastError());
1594 goto failed;
1597 /* Instance Count: don't touch it in the client */
1598 if (SetFilePointer(hfile, sizeof(word), NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
1600 hr = HRESULT_FROM_WIN32(GetLastError());
1601 goto failed;
1603 /* Application Name */
1604 if (!write_unicode_string(hfile, appname))
1606 hr = HRESULT_FROM_WIN32(GetLastError());
1607 goto failed;
1609 /* Parameters */
1610 if (!write_unicode_string(hfile, params))
1612 hr = HRESULT_FROM_WIN32(GetLastError());
1613 goto failed;
1615 /* Working Directory */
1616 if (!write_unicode_string(hfile, workdir))
1618 hr = HRESULT_FROM_WIN32(GetLastError());
1619 goto failed;
1621 /* Author */
1622 if (!write_unicode_string(hfile, creator))
1624 hr = HRESULT_FROM_WIN32(GetLastError());
1625 goto failed;
1627 /* Comment */
1628 if (!write_unicode_string(hfile, comment))
1630 hr = HRESULT_FROM_WIN32(GetLastError());
1631 goto failed;
1634 /* User Data */
1635 if (!write_user_data(hfile, user_data, user_data_size))
1637 hr = HRESULT_FROM_WIN32(GetLastError());
1638 goto failed;
1641 /* Reserved Data */
1642 if (!write_reserved_data(hfile))
1644 hr = HRESULT_FROM_WIN32(GetLastError());
1645 goto failed;
1648 /* Triggers */
1649 hr = write_triggers(This, hfile);
1650 if (hr != S_OK)
1651 goto failed;
1653 /* Signature */
1654 if (!write_signature(hfile))
1656 hr = HRESULT_FROM_WIN32(GetLastError());
1657 goto failed;
1660 hr = S_OK;
1661 This->is_dirty = FALSE;
1663 failed:
1664 CoTaskMemFree(appname);
1665 CoTaskMemFree(params);
1666 CoTaskMemFree(workdir);
1667 if (creator != authorW)
1668 CoTaskMemFree(creator);
1669 if (comment != commentW)
1670 CoTaskMemFree(comment);
1671 CoTaskMemFree(user_data);
1673 CloseHandle(hfile);
1674 if (hr != S_OK)
1675 DeleteFileW(task_name);
1676 else if (remember)
1678 heap_free(This->task_name);
1679 This->task_name = heap_strdupW(task_name);
1681 return hr;
1684 static HRESULT WINAPI MSTASK_IPersistFile_SaveCompleted(
1685 IPersistFile* iface,
1686 LPCOLESTR pszFileName)
1688 FIXME("(%p, %p): stub\n", iface, pszFileName);
1689 return E_NOTIMPL;
1692 static HRESULT WINAPI MSTASK_IPersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *file_name)
1694 TaskImpl *This = impl_from_IPersistFile(iface);
1696 TRACE("(%p, %p)\n", iface, file_name);
1698 *file_name = CoTaskMemAlloc((strlenW(This->task_name) + 1) * sizeof(WCHAR));
1699 if (!*file_name) return E_OUTOFMEMORY;
1701 strcpyW(*file_name, This->task_name);
1702 return S_OK;
1705 static const ITaskVtbl MSTASK_ITaskVtbl =
1707 MSTASK_ITask_QueryInterface,
1708 MSTASK_ITask_AddRef,
1709 MSTASK_ITask_Release,
1710 MSTASK_ITask_CreateTrigger,
1711 MSTASK_ITask_DeleteTrigger,
1712 MSTASK_ITask_GetTriggerCount,
1713 MSTASK_ITask_GetTrigger,
1714 MSTASK_ITask_GetTriggerString,
1715 MSTASK_ITask_GetRunTimes,
1716 MSTASK_ITask_GetNextRunTime,
1717 MSTASK_ITask_SetIdleWait,
1718 MSTASK_ITask_GetIdleWait,
1719 MSTASK_ITask_Run,
1720 MSTASK_ITask_Terminate,
1721 MSTASK_ITask_EditWorkItem,
1722 MSTASK_ITask_GetMostRecentRunTime,
1723 MSTASK_ITask_GetStatus,
1724 MSTASK_ITask_GetExitCode,
1725 MSTASK_ITask_SetComment,
1726 MSTASK_ITask_GetComment,
1727 MSTASK_ITask_SetCreator,
1728 MSTASK_ITask_GetCreator,
1729 MSTASK_ITask_SetWorkItemData,
1730 MSTASK_ITask_GetWorkItemData,
1731 MSTASK_ITask_SetErrorRetryCount,
1732 MSTASK_ITask_GetErrorRetryCount,
1733 MSTASK_ITask_SetErrorRetryInterval,
1734 MSTASK_ITask_GetErrorRetryInterval,
1735 MSTASK_ITask_SetFlags,
1736 MSTASK_ITask_GetFlags,
1737 MSTASK_ITask_SetAccountInformation,
1738 MSTASK_ITask_GetAccountInformation,
1739 MSTASK_ITask_SetApplicationName,
1740 MSTASK_ITask_GetApplicationName,
1741 MSTASK_ITask_SetParameters,
1742 MSTASK_ITask_GetParameters,
1743 MSTASK_ITask_SetWorkingDirectory,
1744 MSTASK_ITask_GetWorkingDirectory,
1745 MSTASK_ITask_SetPriority,
1746 MSTASK_ITask_GetPriority,
1747 MSTASK_ITask_SetTaskFlags,
1748 MSTASK_ITask_GetTaskFlags,
1749 MSTASK_ITask_SetMaxRunTime,
1750 MSTASK_ITask_GetMaxRunTime
1753 static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
1755 MSTASK_IPersistFile_QueryInterface,
1756 MSTASK_IPersistFile_AddRef,
1757 MSTASK_IPersistFile_Release,
1758 MSTASK_IPersistFile_GetClassID,
1759 MSTASK_IPersistFile_IsDirty,
1760 MSTASK_IPersistFile_Load,
1761 MSTASK_IPersistFile_Save,
1762 MSTASK_IPersistFile_SaveCompleted,
1763 MSTASK_IPersistFile_GetCurFile
1766 HRESULT TaskConstructor(ITaskService *service, const WCHAR *name, ITask **task)
1768 static const WCHAR tasksW[] = { '\\','T','a','s','k','s','\\',0 };
1769 static const WCHAR jobW[] = { '.','j','o','b',0 };
1770 TaskImpl *This;
1771 WCHAR task_name[MAX_PATH];
1772 ITaskDefinition *taskdef;
1773 IActionCollection *actions;
1774 HRESULT hr;
1776 TRACE("(%s, %p)\n", debugstr_w(name), task);
1778 if (strchrW(name, '.')) return E_INVALIDARG;
1780 GetWindowsDirectoryW(task_name, MAX_PATH);
1781 lstrcatW(task_name, tasksW);
1782 lstrcatW(task_name, name);
1783 lstrcatW(task_name, jobW);
1785 hr = ITaskService_NewTask(service, 0, &taskdef);
1786 if (hr != S_OK) return hr;
1788 This = heap_alloc(sizeof(*This));
1789 if (!This)
1791 ITaskDefinition_Release(taskdef);
1792 return E_OUTOFMEMORY;
1795 This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
1796 This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
1797 This->ref = 1;
1798 This->task = taskdef;
1799 This->task_name = heap_strdupW(task_name);
1800 This->flags = 0;
1801 This->status = SCHED_S_TASK_NOT_SCHEDULED;
1802 This->exit_code = 0;
1803 This->idle_minutes = 10;
1804 This->deadline_minutes = 60;
1805 This->priority = NORMAL_PRIORITY_CLASS;
1806 This->accountName = NULL;
1807 This->trigger_count = 0;
1808 This->trigger = NULL;
1809 This->is_dirty = FALSE;
1810 This->instance_count = 0;
1812 /* Default time is 3 days = 259200000 ms */
1813 This->maxRunTime = 259200000;
1815 hr = ITaskDefinition_get_Actions(This->task, &actions);
1816 if (hr == S_OK)
1818 hr = IActionCollection_Create(actions, TASK_ACTION_EXEC, (IAction **)&This->action);
1819 IActionCollection_Release(actions);
1820 if (hr == S_OK)
1822 *task = &This->ITask_iface;
1823 InterlockedIncrement(&dll_ref);
1824 return S_OK;
1828 ITaskDefinition_Release(This->task);
1829 ITask_Release(&This->ITask_iface);
1830 return hr;