mstask/tests: Add some tests for workitem data.
[wine.git] / dlls / mstask / task.c
blob490229ae71123222cc880c7ca64f8796d1c9b60e
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 BYTE *data;
62 WORD data_count;
63 UUID uuid;
64 LPWSTR task_name;
65 HRESULT status;
66 WORD idle_minutes, deadline_minutes;
67 DWORD flags, priority, maxRunTime, exit_code;
68 SYSTEMTIME last_runtime;
69 LPWSTR accountName;
70 DWORD trigger_count;
71 TASK_TRIGGER *trigger;
72 BOOL is_dirty;
73 USHORT instance_count;
74 } TaskImpl;
76 static inline TaskImpl *impl_from_ITask(ITask *iface)
78 return CONTAINING_RECORD(iface, TaskImpl, ITask_iface);
81 static inline TaskImpl *impl_from_IPersistFile( IPersistFile *iface )
83 return CONTAINING_RECORD(iface, TaskImpl, IPersistFile_iface);
86 static void TaskDestructor(TaskImpl *This)
88 TRACE("%p\n", This);
89 if (This->action)
90 IExecAction_Release(This->action);
91 ITaskDefinition_Release(This->task);
92 heap_free(This->data);
93 heap_free(This->task_name);
94 heap_free(This->accountName);
95 heap_free(This->trigger);
96 heap_free(This);
97 InterlockedDecrement(&dll_ref);
100 static HRESULT WINAPI MSTASK_ITask_QueryInterface(
101 ITask* iface,
102 REFIID riid,
103 void **ppvObject)
105 TaskImpl * This = impl_from_ITask(iface);
107 TRACE("IID: %s\n", debugstr_guid(riid));
108 if (ppvObject == NULL)
109 return E_POINTER;
111 if (IsEqualGUID(riid, &IID_IUnknown) ||
112 IsEqualGUID(riid, &IID_ITask))
114 *ppvObject = &This->ITask_iface;
115 ITask_AddRef(iface);
116 return S_OK;
118 else if (IsEqualGUID(riid, &IID_IPersistFile))
120 *ppvObject = &This->IPersistFile_iface;
121 ITask_AddRef(iface);
122 return S_OK;
125 WARN("Unknown interface: %s\n", debugstr_guid(riid));
126 *ppvObject = NULL;
127 return E_NOINTERFACE;
130 static ULONG WINAPI MSTASK_ITask_AddRef(
131 ITask* iface)
133 TaskImpl *This = impl_from_ITask(iface);
134 ULONG ref;
135 TRACE("\n");
136 ref = InterlockedIncrement(&This->ref);
137 return ref;
140 static ULONG WINAPI MSTASK_ITask_Release(
141 ITask* iface)
143 TaskImpl * This = impl_from_ITask(iface);
144 ULONG ref;
145 TRACE("\n");
146 ref = InterlockedDecrement(&This->ref);
147 if (ref == 0)
148 TaskDestructor(This);
149 return ref;
152 HRESULT task_set_trigger(ITask *task, WORD idx, const TASK_TRIGGER *src)
154 TaskImpl *This = impl_from_ITask(task);
155 TIME_FIELDS field_time;
156 LARGE_INTEGER sys_time;
157 TASK_TRIGGER dst;
159 TRACE("(%p, %u, %p)\n", task, idx, src);
161 if (idx >= This->trigger_count)
162 return E_FAIL;
164 /* Verify valid structure size */
165 if (src->cbTriggerSize != sizeof(*src))
166 return E_INVALIDARG;
167 dst.cbTriggerSize = src->cbTriggerSize;
169 /* Reserved field must be zero */
170 dst.Reserved1 = 0;
172 /* Verify and set valid start date and time */
173 memset(&field_time, 0, sizeof(field_time));
174 field_time.Year = src->wBeginYear;
175 field_time.Month = src->wBeginMonth;
176 field_time.Day = src->wBeginDay;
177 field_time.Hour = src->wStartHour;
178 field_time.Minute = src->wStartMinute;
179 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
180 return E_INVALIDARG;
181 dst.wBeginYear = src->wBeginYear;
182 dst.wBeginMonth = src->wBeginMonth;
183 dst.wBeginDay = src->wBeginDay;
184 dst.wStartHour = src->wStartHour;
185 dst.wStartMinute = src->wStartMinute;
187 /* Verify valid end date if TASK_TRIGGER_FLAG_HAS_END_DATE flag is set */
188 if (src->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE)
190 memset(&field_time, 0, sizeof(field_time));
191 field_time.Year = src->wEndYear;
192 field_time.Month = src->wEndMonth;
193 field_time.Day = src->wEndDay;
194 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
195 return E_INVALIDARG;
198 /* Set valid end date independent of TASK_TRIGGER_FLAG_HAS_END_DATE flag */
199 dst.wEndYear = src->wEndYear;
200 dst.wEndMonth = src->wEndMonth;
201 dst.wEndDay = src->wEndDay;
203 /* Verify duration and interval pair */
204 if (src->MinutesDuration <= src->MinutesInterval && src->MinutesInterval > 0)
205 return E_INVALIDARG;
206 dst.MinutesDuration = src->MinutesDuration;
207 dst.MinutesInterval = src->MinutesInterval;
209 /* Copy over flags */
210 dst.rgFlags = src->rgFlags;
212 /* Set TriggerType dependent fields of Type union */
213 dst.TriggerType = src->TriggerType;
214 switch (src->TriggerType)
216 case TASK_TIME_TRIGGER_DAILY:
217 dst.Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
218 break;
219 case TASK_TIME_TRIGGER_WEEKLY:
220 dst.Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
221 dst.Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
222 break;
223 case TASK_TIME_TRIGGER_MONTHLYDATE:
224 dst.Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
225 dst.Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
226 break;
227 case TASK_TIME_TRIGGER_MONTHLYDOW:
228 dst.Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
229 dst.Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
230 dst.Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
231 break;
232 case TASK_TIME_TRIGGER_ONCE:
233 case TASK_EVENT_TRIGGER_ON_IDLE:
234 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
235 case TASK_EVENT_TRIGGER_AT_LOGON:
236 default:
237 dst.Type = src->Type;
238 break;
241 /* Reserved field must be zero */
242 dst.Reserved2 = 0;
244 /* wRandomMinutesInterval not currently used and is initialized to zero */
245 dst.wRandomMinutesInterval = 0;
247 This->trigger[idx] = dst;
249 return S_OK;
252 HRESULT task_get_trigger(ITask *task, WORD idx, TASK_TRIGGER *dst)
254 TaskImpl *This = impl_from_ITask(task);
255 TASK_TRIGGER *src;
257 TRACE("(%p, %u, %p)\n", task, idx, dst);
259 if (idx >= This->trigger_count)
260 return SCHED_E_TRIGGER_NOT_FOUND;
262 src = &This->trigger[idx];
264 /* Native implementation doesn't verify equivalent cbTriggerSize fields */
266 /* Copy relevant fields of the structure */
267 dst->cbTriggerSize = src->cbTriggerSize;
268 dst->Reserved1 = 0;
269 dst->wBeginYear = src->wBeginYear;
270 dst->wBeginMonth = src->wBeginMonth;
271 dst->wBeginDay = src->wBeginDay;
272 dst->wEndYear = src->wEndYear;
273 dst->wEndMonth = src->wEndMonth;
274 dst->wEndDay = src->wEndDay;
275 dst->wStartHour = src->wStartHour;
276 dst->wStartMinute = src->wStartMinute;
277 dst->MinutesDuration = src->MinutesDuration;
278 dst->MinutesInterval = src->MinutesInterval;
279 dst->rgFlags = src->rgFlags;
280 dst->TriggerType = src->TriggerType;
281 switch (src->TriggerType)
283 case TASK_TIME_TRIGGER_DAILY:
284 dst->Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
285 break;
286 case TASK_TIME_TRIGGER_WEEKLY:
287 dst->Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
288 dst->Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
289 break;
290 case TASK_TIME_TRIGGER_MONTHLYDATE:
291 dst->Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
292 dst->Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
293 break;
294 case TASK_TIME_TRIGGER_MONTHLYDOW:
295 dst->Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
296 dst->Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
297 dst->Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
298 break;
299 case TASK_TIME_TRIGGER_ONCE:
300 case TASK_EVENT_TRIGGER_ON_IDLE:
301 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
302 case TASK_EVENT_TRIGGER_AT_LOGON:
303 default:
304 break;
306 dst->Reserved2 = 0;
307 dst->wRandomMinutesInterval = 0;
309 return S_OK;
312 static HRESULT WINAPI MSTASK_ITask_CreateTrigger(ITask *iface, WORD *idx, ITaskTrigger **task_trigger)
314 TaskImpl *This = impl_from_ITask(iface);
315 TASK_TRIGGER *new_trigger;
316 SYSTEMTIME time;
317 HRESULT hr;
319 TRACE("(%p, %p, %p)\n", iface, idx, task_trigger);
321 hr = TaskTriggerConstructor(iface, This->trigger_count, task_trigger);
322 if (hr != S_OK) return hr;
324 if (This->trigger)
325 new_trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * (This->trigger_count + 1));
326 else
327 new_trigger = heap_alloc(sizeof(This->trigger[0]));
328 if (!new_trigger)
330 ITaskTrigger_Release(*task_trigger);
331 return E_OUTOFMEMORY;
334 This->trigger = new_trigger;
336 new_trigger = &This->trigger[This->trigger_count];
338 /* Most fields default to zero. Initialize other fields to default values. */
339 memset(new_trigger, 0, sizeof(*new_trigger));
340 GetLocalTime(&time);
341 new_trigger->cbTriggerSize = sizeof(*new_trigger);
342 new_trigger->wBeginYear = time.wYear;
343 new_trigger->wBeginMonth = time.wMonth;
344 new_trigger->wBeginDay = time.wDay;
345 new_trigger->wStartHour = time.wHour;
346 new_trigger->wStartMinute = time.wMinute;
347 new_trigger->rgFlags = TASK_TRIGGER_FLAG_DISABLED;
348 new_trigger->TriggerType = TASK_TIME_TRIGGER_DAILY,
349 new_trigger->Type.Daily.DaysInterval = 1;
351 *idx = This->trigger_count++;
353 return hr;
356 static HRESULT WINAPI MSTASK_ITask_DeleteTrigger(ITask *iface, WORD idx)
358 TaskImpl *This = impl_from_ITask(iface);
360 TRACE("(%p, %u)\n", iface, idx);
362 if (idx >= This->trigger_count)
363 return SCHED_E_TRIGGER_NOT_FOUND;
365 This->trigger_count--;
366 memmove(&This->trigger[idx], &This->trigger[idx + 1], (This->trigger_count - idx) * sizeof(This->trigger[0]));
367 /* this shouldn't fail in practice since we're shrinking the memory block */
368 This->trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * This->trigger_count);
370 return S_OK;
373 static HRESULT WINAPI MSTASK_ITask_GetTriggerCount(ITask *iface, WORD *count)
375 TaskImpl *This = impl_from_ITask(iface);
377 TRACE("(%p, %p)\n", iface, count);
379 *count = This->trigger_count;
380 return S_OK;
383 static HRESULT WINAPI MSTASK_ITask_GetTrigger(ITask *iface, WORD idx, ITaskTrigger **trigger)
385 TaskImpl *This = impl_from_ITask(iface);
387 TRACE("(%p, %u, %p)\n", iface, idx, trigger);
389 if (idx >= This->trigger_count)
390 return SCHED_E_TRIGGER_NOT_FOUND;
392 return TaskTriggerConstructor(iface, idx, trigger);
395 static HRESULT WINAPI MSTASK_ITask_GetTriggerString(
396 ITask* iface,
397 WORD iTrigger,
398 LPWSTR *ppwszTrigger)
400 FIXME("(%p, %d, %p): stub\n", iface, iTrigger, ppwszTrigger);
401 return E_NOTIMPL;
404 static HRESULT WINAPI MSTASK_ITask_GetRunTimes(
405 ITask* iface,
406 const LPSYSTEMTIME pstBegin,
407 const LPSYSTEMTIME pstEnd,
408 WORD *pCount,
409 LPSYSTEMTIME *rgstTaskTimes)
411 FIXME("(%p, %p, %p, %p, %p): stub\n", iface, pstBegin, pstEnd, pCount,
412 rgstTaskTimes);
413 return E_NOTIMPL;
416 static void get_begin_time(const TASK_TRIGGER *trigger, FILETIME *ft)
418 SYSTEMTIME st;
420 st.wYear = trigger->wBeginYear;
421 st.wMonth = trigger->wBeginMonth;
422 st.wDay = trigger->wBeginDay;
423 st.wDayOfWeek = 0;
424 st.wHour = 0;
425 st.wMinute = 0;
426 st.wSecond = 0;
427 st.wMilliseconds = 0;
428 SystemTimeToFileTime(&st, ft);
431 static void get_end_time(const TASK_TRIGGER *trigger, FILETIME *ft)
433 SYSTEMTIME st;
435 if (!(trigger->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE))
437 ft->dwHighDateTime = ~0u;
438 ft->dwLowDateTime = ~0u;
439 return;
442 st.wYear = trigger->wEndYear;
443 st.wMonth = trigger->wEndMonth;
444 st.wDay = trigger->wEndDay;
445 st.wDayOfWeek = 0;
446 st.wHour = 0;
447 st.wMinute = 0;
448 st.wSecond = 0;
449 st.wMilliseconds = 0;
450 SystemTimeToFileTime(&st, ft);
453 static void filetime_add_ms(FILETIME *ft, ULONGLONG ms)
455 union u_ftll
457 FILETIME ft;
458 ULONGLONG ll;
459 } *ftll = (union u_ftll *)ft;
461 ftll->ll += ms * (ULONGLONG)10000;
464 static void filetime_add_hours(FILETIME *ft, ULONG hours)
466 filetime_add_ms(ft, (ULONGLONG)hours * 60 * 60 * 1000);
469 static void filetime_add_days(FILETIME *ft, ULONG days)
471 filetime_add_hours(ft, (ULONGLONG)days * 24);
474 static void filetime_add_weeks(FILETIME *ft, ULONG weeks)
476 filetime_add_days(ft, (ULONGLONG)weeks * 7);
479 static HRESULT WINAPI MSTASK_ITask_GetNextRunTime(ITask *iface, SYSTEMTIME *rt)
481 TaskImpl *This = impl_from_ITask(iface);
482 HRESULT hr = SCHED_S_TASK_NO_VALID_TRIGGERS;
483 SYSTEMTIME st, current_st;
484 FILETIME current_ft, begin_ft, end_ft, best_ft;
485 BOOL have_best_time = FALSE;
486 DWORD i;
488 TRACE("(%p, %p)\n", iface, rt);
490 if (This->flags & TASK_FLAG_DISABLED)
492 memset(rt, 0, sizeof(*rt));
493 return SCHED_S_TASK_DISABLED;
496 GetLocalTime(&current_st);
498 for (i = 0; i < This->trigger_count; i++)
500 if (!(This->trigger[i].rgFlags & TASK_TRIGGER_FLAG_DISABLED))
502 get_begin_time(&This->trigger[i], &begin_ft);
503 get_end_time(&This->trigger[i], &end_ft);
505 switch (This->trigger[i].TriggerType)
507 case TASK_EVENT_TRIGGER_ON_IDLE:
508 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
509 case TASK_EVENT_TRIGGER_AT_LOGON:
510 hr = SCHED_S_EVENT_TRIGGER;
511 break;
513 case TASK_TIME_TRIGGER_ONCE:
514 st = current_st;
515 st.wHour = This->trigger[i].wStartHour;
516 st.wMinute = This->trigger[i].wStartMinute;
517 st.wSecond = 0;
518 st.wMilliseconds = 0;
519 SystemTimeToFileTime(&st, &current_ft);
520 if (CompareFileTime(&begin_ft, &current_ft) <= 0 && CompareFileTime(&current_ft, &end_ft) < 0)
522 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
524 best_ft = current_ft;
525 have_best_time = TRUE;
528 break;
530 case TASK_TIME_TRIGGER_DAILY:
531 st = current_st;
532 st.wHour = This->trigger[i].wStartHour;
533 st.wMinute = This->trigger[i].wStartMinute;
534 st.wSecond = 0;
535 st.wMilliseconds = 0;
536 SystemTimeToFileTime(&st, &current_ft);
537 while (CompareFileTime(&current_ft, &end_ft) < 0)
539 if (CompareFileTime(&current_ft, &begin_ft) >= 0)
541 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
543 best_ft = current_ft;
544 have_best_time = TRUE;
546 break;
549 filetime_add_days(&current_ft, This->trigger[i].Type.Daily.DaysInterval);
551 break;
553 case TASK_TIME_TRIGGER_WEEKLY:
554 if (!This->trigger[i].Type.Weekly.rgfDaysOfTheWeek)
555 break; /* avoid infinite loop */
557 st = current_st;
558 st.wHour = This->trigger[i].wStartHour;
559 st.wMinute = This->trigger[i].wStartMinute;
560 st.wSecond = 0;
561 st.wMilliseconds = 0;
562 SystemTimeToFileTime(&st, &current_ft);
563 while (CompareFileTime(&current_ft, &end_ft) < 0)
565 FileTimeToSystemTime(&current_ft, &st);
567 if (CompareFileTime(&current_ft, &begin_ft) >= 0)
569 if (This->trigger[i].Type.Weekly.rgfDaysOfTheWeek & (1 << st.wDayOfWeek))
571 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
573 best_ft = current_ft;
574 have_best_time = TRUE;
576 break;
580 if (st.wDayOfWeek == 0 && This->trigger[i].Type.Weekly.WeeksInterval > 1) /* Sunday, goto next week */
581 filetime_add_weeks(&current_ft, This->trigger[i].Type.Weekly.WeeksInterval - 1);
582 else /* check next weekday */
583 filetime_add_days(&current_ft, 1);
585 break;
587 default:
588 FIXME("trigger type %u is not handled\n", This->trigger[i].TriggerType);
589 break;
594 if (have_best_time)
596 FileTimeToSystemTime(&best_ft, rt);
597 return S_OK;
600 memset(rt, 0, sizeof(*rt));
601 return hr;
604 static HRESULT WINAPI MSTASK_ITask_SetIdleWait(
605 ITask* iface,
606 WORD wIdleMinutes,
607 WORD wDeadlineMinutes)
609 FIXME("(%p, %d, %d): stub\n", iface, wIdleMinutes, wDeadlineMinutes);
610 return E_NOTIMPL;
613 static HRESULT WINAPI MSTASK_ITask_GetIdleWait(ITask *iface, WORD *idle_minutes, WORD *deadline_minutes)
615 TaskImpl *This = impl_from_ITask(iface);
617 TRACE("(%p, %p, %p): stub\n", iface, idle_minutes, deadline_minutes);
619 *idle_minutes = This->idle_minutes;
620 *deadline_minutes = This->deadline_minutes;
621 return S_OK;
624 static HRESULT WINAPI MSTASK_ITask_Run(ITask *iface)
626 TaskImpl *This = impl_from_ITask(iface);
628 TRACE("(%p)\n", iface);
630 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
631 return SCHED_E_TASK_NOT_READY;
633 This->flags |= 0x04000000;
634 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
637 static HRESULT WINAPI MSTASK_ITask_Terminate(ITask *iface)
639 TaskImpl *This = impl_from_ITask(iface);
641 TRACE("(%p)\n", iface);
643 if (!This->instance_count)
644 return SCHED_E_TASK_NOT_RUNNING;
646 This->flags |= 0x08000000;
647 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
650 static HRESULT WINAPI MSTASK_ITask_EditWorkItem(
651 ITask* iface,
652 HWND hParent,
653 DWORD dwReserved)
655 FIXME("(%p, %p, %d): stub\n", iface, hParent, dwReserved);
656 return E_NOTIMPL;
659 static HRESULT WINAPI MSTASK_ITask_GetMostRecentRunTime(ITask *iface, SYSTEMTIME *st)
661 TaskImpl *This = impl_from_ITask(iface);
663 TRACE("(%p, %p)\n", iface, st);
665 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
667 memset(st, 0, sizeof(*st));
668 return SCHED_S_TASK_HAS_NOT_RUN;
671 *st = This->last_runtime;
672 return S_OK;
675 static HRESULT WINAPI MSTASK_ITask_GetStatus(ITask *iface, HRESULT *status)
677 TaskImpl *This = impl_from_ITask(iface);
679 TRACE("(%p, %p)\n", iface, status);
681 *status = This->instance_count ? SCHED_S_TASK_RUNNING : This->status;
682 return S_OK;
685 static HRESULT WINAPI MSTASK_ITask_GetExitCode(ITask *iface, DWORD *exit_code)
687 TaskImpl *This = impl_from_ITask(iface);
689 TRACE("(%p, %p)\n", iface, exit_code);
691 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
693 *exit_code = 0;
694 return SCHED_S_TASK_HAS_NOT_RUN;
697 *exit_code = This->exit_code;
698 return S_OK;
701 static HRESULT WINAPI MSTASK_ITask_SetComment(ITask *iface, LPCWSTR comment)
703 TaskImpl *This = impl_from_ITask(iface);
704 IRegistrationInfo *info;
705 HRESULT hr;
707 TRACE("(%p, %s)\n", iface, debugstr_w(comment));
709 if (!comment || !comment[0])
710 comment = NULL;
712 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
713 if (hr == S_OK)
715 hr = IRegistrationInfo_put_Description(info, (BSTR)comment);
716 IRegistrationInfo_Release(info);
717 This->is_dirty = TRUE;
719 return hr;
722 static HRESULT WINAPI MSTASK_ITask_GetComment(ITask *iface, LPWSTR *comment)
724 TaskImpl *This = impl_from_ITask(iface);
725 IRegistrationInfo *info;
726 HRESULT hr;
727 BSTR description;
728 DWORD len;
730 TRACE("(%p, %p)\n", iface, comment);
732 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
733 if (hr != S_OK) return hr;
735 hr = IRegistrationInfo_get_Description(info, &description);
736 if (hr == S_OK)
738 len = description ? lstrlenW(description) + 1 : 1;
739 *comment = CoTaskMemAlloc(len * sizeof(WCHAR));
740 if (*comment)
742 if (!description)
743 *comment[0] = 0;
744 else
745 lstrcpyW(*comment, description);
746 hr = S_OK;
748 else
749 hr = E_OUTOFMEMORY;
751 SysFreeString(description);
754 IRegistrationInfo_Release(info);
755 return hr;
758 static HRESULT WINAPI MSTASK_ITask_SetCreator(ITask *iface, LPCWSTR creator)
760 TaskImpl *This = impl_from_ITask(iface);
761 IRegistrationInfo *info;
762 HRESULT hr;
764 TRACE("(%p, %s)\n", iface, debugstr_w(creator));
766 if (!creator || !creator[0])
767 creator = NULL;
769 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
770 if (hr == S_OK)
772 hr = IRegistrationInfo_put_Author(info, (BSTR)creator);
773 IRegistrationInfo_Release(info);
774 This->is_dirty = TRUE;
776 return hr;
779 static HRESULT WINAPI MSTASK_ITask_GetCreator(ITask *iface, LPWSTR *creator)
781 TaskImpl *This = impl_from_ITask(iface);
782 IRegistrationInfo *info;
783 HRESULT hr;
784 BSTR author;
785 DWORD len;
787 TRACE("(%p, %p)\n", iface, creator);
789 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
790 if (hr != S_OK) return hr;
792 hr = IRegistrationInfo_get_Author(info, &author);
793 if (hr == S_OK)
795 len = author ? lstrlenW(author) + 1 : 1;
796 *creator = CoTaskMemAlloc(len * sizeof(WCHAR));
797 if (*creator)
799 if (!author)
800 *creator[0] = 0;
801 else
802 lstrcpyW(*creator, author);
803 hr = S_OK;
805 else
806 hr = E_OUTOFMEMORY;
808 SysFreeString(author);
811 IRegistrationInfo_Release(info);
812 return hr;
815 static HRESULT WINAPI MSTASK_ITask_SetWorkItemData(ITask *iface, WORD count, BYTE data[])
817 TaskImpl *This = impl_from_ITask(iface);
819 TRACE("(%p, %u, %p)\n", iface, count, data);
821 if (count)
823 if (!data) return E_INVALIDARG;
825 heap_free(This->data);
826 This->data = heap_alloc(count);
827 if (!This->data) return E_OUTOFMEMORY;
828 memcpy(This->data, data, count);
829 This->data_count = count;
831 else
833 if (data) return E_INVALIDARG;
835 heap_free(This->data);
836 This->data = NULL;
837 This->data_count = 0;
840 return S_OK;
843 static HRESULT WINAPI MSTASK_ITask_GetWorkItemData(ITask *iface, WORD *count, BYTE **data)
845 TaskImpl *This = impl_from_ITask(iface);
847 TRACE("(%p, %p, %p)\n", iface, count, data);
849 if (!This->data)
851 *count = 0;
852 *data = NULL;
854 else
856 *data = CoTaskMemAlloc(This->data_count);
857 if (!*data) return E_OUTOFMEMORY;
858 memcpy(*data, This->data, This->data_count);
859 *count = This->data_count;
862 return S_OK;
865 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryCount(
866 ITask* iface,
867 WORD wRetryCount)
869 FIXME("(%p, %d): stub\n", iface, wRetryCount);
870 return E_NOTIMPL;
873 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryCount(ITask *iface, WORD *count)
875 TRACE("(%p, %p)\n", iface, count);
876 return E_NOTIMPL;
879 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryInterval(
880 ITask* iface,
881 WORD wRetryInterval)
883 FIXME("(%p, %d): stub\n", iface, wRetryInterval);
884 return E_NOTIMPL;
887 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryInterval(ITask *iface, WORD *interval)
889 TRACE("(%p, %p)\n", iface, interval);
890 return E_NOTIMPL;
893 static HRESULT WINAPI MSTASK_ITask_SetFlags(ITask *iface, DWORD flags)
895 TaskImpl *This = impl_from_ITask(iface);
897 TRACE("(%p, 0x%08x)\n", iface, flags);
898 This->flags &= 0xffff8000;
899 This->flags |= flags & 0x7fff;
900 This->is_dirty = TRUE;
901 return S_OK;
904 static HRESULT WINAPI MSTASK_ITask_GetFlags(ITask *iface, DWORD *flags)
906 TaskImpl *This = impl_from_ITask(iface);
908 TRACE("(%p, %p)\n", iface, flags);
909 *flags = LOWORD(This->flags);
910 return S_OK;
913 static HRESULT WINAPI MSTASK_ITask_SetAccountInformation(
914 ITask* iface,
915 LPCWSTR pwszAccountName,
916 LPCWSTR pwszPassword)
918 DWORD n;
919 TaskImpl *This = impl_from_ITask(iface);
920 LPWSTR tmp_account_name;
922 TRACE("(%p, %s, %s): partial stub\n", iface, debugstr_w(pwszAccountName),
923 debugstr_w(pwszPassword));
925 if (pwszPassword)
926 FIXME("Partial stub ignores passwords\n");
928 n = (lstrlenW(pwszAccountName) + 1);
929 tmp_account_name = heap_alloc(n * sizeof(WCHAR));
930 if (!tmp_account_name)
931 return E_OUTOFMEMORY;
932 lstrcpyW(tmp_account_name, pwszAccountName);
933 heap_free(This->accountName);
934 This->accountName = tmp_account_name;
935 This->is_dirty = TRUE;
936 return S_OK;
939 static HRESULT WINAPI MSTASK_ITask_GetAccountInformation(
940 ITask* iface,
941 LPWSTR *ppwszAccountName)
943 DWORD n;
944 TaskImpl *This = impl_from_ITask(iface);
946 TRACE("(%p, %p): partial stub\n", iface, ppwszAccountName);
948 /* This implements the WinXP behavior when accountName has not yet
949 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
950 if (!This->accountName)
951 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
953 n = (lstrlenW(This->accountName) + 1);
954 *ppwszAccountName = CoTaskMemAlloc(n * sizeof(WCHAR));
955 if (!*ppwszAccountName)
956 return E_OUTOFMEMORY;
957 lstrcpyW(*ppwszAccountName, This->accountName);
958 return S_OK;
961 static HRESULT WINAPI MSTASK_ITask_SetApplicationName(ITask *iface, LPCWSTR appname)
963 TaskImpl *This = impl_from_ITask(iface);
964 DWORD len;
965 HRESULT hr;
967 TRACE("(%p, %s)\n", iface, debugstr_w(appname));
969 /* Empty application name */
970 if (!appname || !appname[0])
971 return IExecAction_put_Path(This->action, NULL);
973 /* Attempt to set pwszApplicationName to a path resolved application name */
974 len = SearchPathW(NULL, appname, NULL, 0, NULL, NULL);
975 if (len)
977 LPWSTR tmp_name;
979 tmp_name = heap_alloc(len * sizeof(WCHAR));
980 if (!tmp_name)
981 return E_OUTOFMEMORY;
982 len = SearchPathW(NULL, appname, NULL, len, tmp_name, NULL);
983 if (len)
985 hr = IExecAction_put_Path(This->action, tmp_name);
986 if (hr == S_OK) This->is_dirty = TRUE;
988 else
989 hr = HRESULT_FROM_WIN32(GetLastError());
991 heap_free(tmp_name);
992 return hr;
995 /* If unable to path resolve name, simply set to appname */
996 hr = IExecAction_put_Path(This->action, (BSTR)appname);
997 if (hr == S_OK) This->is_dirty = TRUE;
998 return hr;
1001 static HRESULT WINAPI MSTASK_ITask_GetApplicationName(ITask *iface, LPWSTR *appname)
1003 TaskImpl *This = impl_from_ITask(iface);
1004 HRESULT hr;
1005 BSTR path;
1006 DWORD len;
1008 TRACE("(%p, %p)\n", iface, appname);
1010 hr = IExecAction_get_Path(This->action, &path);
1011 if (hr != S_OK) return hr;
1013 len = path ? lstrlenW(path) + 1 : 1;
1014 *appname = CoTaskMemAlloc(len * sizeof(WCHAR));
1015 if (*appname)
1017 if (!path)
1018 *appname[0] = 0;
1019 else
1020 lstrcpyW(*appname, path);
1021 hr = S_OK;
1023 else
1024 hr = E_OUTOFMEMORY;
1026 SysFreeString(path);
1027 return hr;
1030 static HRESULT WINAPI MSTASK_ITask_SetParameters(ITask *iface, LPCWSTR params)
1032 TaskImpl *This = impl_from_ITask(iface);
1033 HRESULT hr;
1035 TRACE("(%p, %s)\n", iface, debugstr_w(params));
1037 /* Empty parameter list */
1038 if (!params || !params[0])
1039 params = NULL;
1041 hr = IExecAction_put_Arguments(This->action, (BSTR)params);
1042 if (hr == S_OK) This->is_dirty = TRUE;
1043 return hr;
1046 static HRESULT WINAPI MSTASK_ITask_GetParameters(ITask *iface, LPWSTR *params)
1048 TaskImpl *This = impl_from_ITask(iface);
1049 HRESULT hr;
1050 BSTR args;
1051 DWORD len;
1053 TRACE("(%p, %p)\n", iface, params);
1055 hr = IExecAction_get_Arguments(This->action, &args);
1056 if (hr != S_OK) return hr;
1058 len = args ? lstrlenW(args) + 1 : 1;
1059 *params = CoTaskMemAlloc(len * sizeof(WCHAR));
1060 if (*params)
1062 if (!args)
1063 *params[0] = 0;
1064 else
1065 lstrcpyW(*params, args);
1066 hr = S_OK;
1068 else
1069 hr = E_OUTOFMEMORY;
1071 SysFreeString(args);
1072 return hr;
1075 static HRESULT WINAPI MSTASK_ITask_SetWorkingDirectory(ITask * iface, LPCWSTR workdir)
1077 TaskImpl *This = impl_from_ITask(iface);
1078 HRESULT hr;
1080 TRACE("(%p, %s)\n", iface, debugstr_w(workdir));
1082 if (!workdir || !workdir[0])
1083 workdir = NULL;
1085 hr = IExecAction_put_WorkingDirectory(This->action, (BSTR)workdir);
1086 if (hr == S_OK) This->is_dirty = TRUE;
1087 return hr;
1090 static HRESULT WINAPI MSTASK_ITask_GetWorkingDirectory(ITask *iface, LPWSTR *workdir)
1092 TaskImpl *This = impl_from_ITask(iface);
1093 HRESULT hr;
1094 BSTR dir;
1095 DWORD len;
1097 TRACE("(%p, %p)\n", iface, workdir);
1099 hr = IExecAction_get_WorkingDirectory(This->action, &dir);
1100 if (hr != S_OK) return hr;
1102 len = dir ? lstrlenW(dir) + 1 : 1;
1103 *workdir = CoTaskMemAlloc(len * sizeof(WCHAR));
1104 if (*workdir)
1106 if (!dir)
1107 *workdir[0] = 0;
1108 else
1109 lstrcpyW(*workdir, dir);
1110 hr = S_OK;
1112 else
1113 hr = E_OUTOFMEMORY;
1115 SysFreeString(dir);
1116 return hr;
1119 static HRESULT WINAPI MSTASK_ITask_SetPriority(
1120 ITask* iface,
1121 DWORD dwPriority)
1123 FIXME("(%p, 0x%08x): stub\n", iface, dwPriority);
1124 return E_NOTIMPL;
1127 static HRESULT WINAPI MSTASK_ITask_GetPriority(ITask *iface, DWORD *priority)
1129 TaskImpl *This = impl_from_ITask(iface);
1131 TRACE("(%p, %p)\n", iface, priority);
1133 *priority = This->priority;
1134 return S_OK;
1137 static HRESULT WINAPI MSTASK_ITask_SetTaskFlags(
1138 ITask* iface,
1139 DWORD dwFlags)
1141 FIXME("(%p, 0x%08x): stub\n", iface, dwFlags);
1142 return E_NOTIMPL;
1145 static HRESULT WINAPI MSTASK_ITask_GetTaskFlags(ITask *iface, DWORD *flags)
1147 FIXME("(%p, %p): stub\n", iface, flags);
1148 *flags = 0;
1149 return S_OK;
1152 static HRESULT WINAPI MSTASK_ITask_SetMaxRunTime(
1153 ITask* iface,
1154 DWORD dwMaxRunTime)
1156 TaskImpl *This = impl_from_ITask(iface);
1158 TRACE("(%p, %d)\n", iface, dwMaxRunTime);
1160 This->maxRunTime = dwMaxRunTime;
1161 This->is_dirty = TRUE;
1162 return S_OK;
1165 static HRESULT WINAPI MSTASK_ITask_GetMaxRunTime(
1166 ITask* iface,
1167 DWORD *pdwMaxRunTime)
1169 TaskImpl *This = impl_from_ITask(iface);
1171 TRACE("(%p, %p)\n", iface, pdwMaxRunTime);
1173 *pdwMaxRunTime = This->maxRunTime;
1174 return S_OK;
1177 static HRESULT WINAPI MSTASK_IPersistFile_QueryInterface(
1178 IPersistFile* iface,
1179 REFIID riid,
1180 void **ppvObject)
1182 TaskImpl *This = impl_from_IPersistFile(iface);
1183 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
1184 return ITask_QueryInterface(&This->ITask_iface, riid, ppvObject);
1187 static ULONG WINAPI MSTASK_IPersistFile_AddRef(
1188 IPersistFile* iface)
1190 TaskImpl *This = impl_from_IPersistFile(iface);
1191 return ITask_AddRef(&This->ITask_iface);
1194 static ULONG WINAPI MSTASK_IPersistFile_Release(
1195 IPersistFile* iface)
1197 TaskImpl *This = impl_from_IPersistFile(iface);
1198 return ITask_Release(&This->ITask_iface);
1201 static HRESULT WINAPI MSTASK_IPersistFile_GetClassID(IPersistFile *iface, CLSID *clsid)
1203 TRACE("(%p, %p)\n", iface, clsid);
1205 *clsid = CLSID_CTask;
1206 return S_OK;
1209 static HRESULT WINAPI MSTASK_IPersistFile_IsDirty(IPersistFile *iface)
1211 TaskImpl *This = impl_from_IPersistFile(iface);
1212 TRACE("(%p)\n", iface);
1213 return This->is_dirty ? S_OK : S_FALSE;
1216 static DWORD load_unicode_strings(ITask *task, BYTE *data, DWORD limit)
1218 DWORD i, data_size = 0;
1219 USHORT len;
1221 for (i = 0; i < 5; i++)
1223 if (limit < sizeof(USHORT))
1225 TRACE("invalid string %u offset\n", i);
1226 break;
1229 len = *(USHORT *)data;
1230 data += sizeof(USHORT);
1231 data_size += sizeof(USHORT);
1232 limit -= sizeof(USHORT);
1233 if (limit < len * sizeof(WCHAR))
1235 TRACE("invalid string %u size\n", i);
1236 break;
1239 TRACE("string %u: %s\n", i, wine_dbgstr_wn((const WCHAR *)data, len));
1241 switch (i)
1243 case 0:
1244 ITask_SetApplicationName(task, (const WCHAR *)data);
1245 break;
1246 case 1:
1247 ITask_SetParameters(task, (const WCHAR *)data);
1248 break;
1249 case 2:
1250 ITask_SetWorkingDirectory(task, (const WCHAR *)data);
1251 break;
1252 case 3:
1253 ITask_SetCreator(task, (const WCHAR *)data);
1254 break;
1255 case 4:
1256 ITask_SetComment(task, (const WCHAR *)data);
1257 break;
1258 default:
1259 break;
1262 data += len * sizeof(WCHAR);
1263 data_size += len * sizeof(WCHAR);
1266 return data_size;
1269 static HRESULT load_job_data(TaskImpl *This, BYTE *data, DWORD size)
1271 ITask *task = &This->ITask_iface;
1272 HRESULT hr;
1273 const FIXDLEN_DATA *fixed;
1274 const SYSTEMTIME *st;
1275 DWORD unicode_strings_size, data_size, triggers_size;
1276 USHORT trigger_count, i;
1277 const USHORT *signature;
1278 TASK_TRIGGER *task_trigger;
1280 if (size < sizeof(*fixed))
1282 TRACE("no space for FIXDLEN_DATA\n");
1283 return SCHED_E_INVALID_TASK;
1286 fixed = (const FIXDLEN_DATA *)data;
1288 TRACE("product_version %04x\n", fixed->product_version);
1289 TRACE("file_version %04x\n", fixed->file_version);
1290 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed->uuid));
1292 if (fixed->file_version != 0x0001)
1293 return SCHED_E_INVALID_TASK;
1295 This->uuid = fixed->uuid;
1297 TRACE("name_size_offset %04x\n", fixed->name_size_offset);
1298 TRACE("trigger_offset %04x\n", fixed->trigger_offset);
1299 TRACE("error_retry_count %u\n", fixed->error_retry_count);
1300 TRACE("error_retry_interval %u\n", fixed->error_retry_interval);
1301 TRACE("idle_deadline %u\n", fixed->idle_deadline);
1302 This->deadline_minutes = fixed->idle_deadline;
1303 TRACE("idle_wait %u\n", fixed->idle_wait);
1304 This->idle_minutes = fixed->idle_wait;
1305 TRACE("priority %08x\n", fixed->priority);
1306 This->priority = fixed->priority;
1307 TRACE("maximum_runtime %u\n", fixed->maximum_runtime);
1308 This->maxRunTime = fixed->maximum_runtime;
1309 TRACE("exit_code %#x\n", fixed->exit_code);
1310 This->exit_code = fixed->exit_code;
1311 TRACE("status %08x\n", fixed->status);
1312 This->status = fixed->status;
1313 TRACE("flags %08x\n", fixed->flags);
1314 This->flags = fixed->flags;
1315 This->last_runtime = fixed->last_runtime;
1316 st = &fixed->last_runtime;
1317 TRACE("last_runtime %u/%u/%u wday %u %u:%02u:%02u.%03u\n",
1318 st->wDay, st->wMonth, st->wYear, st->wDayOfWeek,
1319 st->wHour, st->wMinute, st->wSecond, st->wMilliseconds);
1321 /* Instance Count */
1322 if (size < sizeof(*fixed) + sizeof(USHORT))
1324 TRACE("no space for instance count\n");
1325 return SCHED_E_INVALID_TASK;
1328 This->instance_count = *(const USHORT *)(data + sizeof(*fixed));
1329 TRACE("instance count %u\n", This->instance_count);
1331 if (fixed->name_size_offset + sizeof(USHORT) < size)
1332 unicode_strings_size = load_unicode_strings(task, data + fixed->name_size_offset, size - fixed->name_size_offset);
1333 else
1335 TRACE("invalid name_size_offset\n");
1336 return SCHED_E_INVALID_TASK;
1338 TRACE("unicode strings end at %#x\n", fixed->name_size_offset + unicode_strings_size);
1340 if (size < fixed->trigger_offset + sizeof(USHORT))
1342 TRACE("no space for triggers count\n");
1343 return SCHED_E_INVALID_TASK;
1345 trigger_count = *(const USHORT *)(data + fixed->trigger_offset);
1346 TRACE("trigger_count %u\n", trigger_count);
1347 triggers_size = size - fixed->trigger_offset - sizeof(USHORT);
1348 TRACE("triggers_size %u\n", triggers_size);
1349 task_trigger = (TASK_TRIGGER *)(data + fixed->trigger_offset + sizeof(USHORT));
1351 data += fixed->name_size_offset + unicode_strings_size;
1352 size -= fixed->name_size_offset + unicode_strings_size;
1354 /* User Data */
1355 if (size < sizeof(USHORT))
1357 TRACE("no space for user data size\n");
1358 return SCHED_E_INVALID_TASK;
1361 data_size = *(const USHORT *)data;
1362 if (size < sizeof(USHORT) + data_size)
1364 TRACE("no space for user data\n");
1365 return SCHED_E_INVALID_TASK;
1367 TRACE("User Data size %#x\n", data_size);
1368 ITask_SetWorkItemData(task, data_size, data + sizeof(USHORT));
1370 size -= sizeof(USHORT) + data_size;
1371 data += sizeof(USHORT) + data_size;
1373 /* Reserved Data */
1374 if (size < sizeof(USHORT))
1376 TRACE("no space for reserved data size\n");
1377 return SCHED_E_INVALID_TASK;
1380 data_size = *(const USHORT *)data;
1381 if (size < sizeof(USHORT) + data_size)
1383 TRACE("no space for reserved data\n");
1384 return SCHED_E_INVALID_TASK;
1386 TRACE("Reserved Data size %#x\n", data_size);
1388 size -= sizeof(USHORT) + data_size;
1389 data += sizeof(USHORT) + data_size;
1391 /* Trigger Data */
1392 TRACE("trigger_offset %04x, triggers end at %04x\n", fixed->trigger_offset,
1393 (DWORD)(fixed->trigger_offset + sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER)));
1395 task_trigger = (TASK_TRIGGER *)(data + sizeof(USHORT));
1397 if (trigger_count * sizeof(TASK_TRIGGER) > triggers_size)
1399 TRACE("no space for triggers data\n");
1400 return SCHED_E_INVALID_TASK;
1403 This->trigger_count = 0;
1405 for (i = 0; i < trigger_count; i++)
1407 ITaskTrigger *trigger;
1408 WORD idx;
1410 hr = ITask_CreateTrigger(task, &idx, &trigger);
1411 if (hr != S_OK) return hr;
1413 hr = ITaskTrigger_SetTrigger(trigger, &task_trigger[i]);
1414 ITaskTrigger_Release(trigger);
1415 if (hr != S_OK)
1417 ITask_DeleteTrigger(task, idx);
1418 return hr;
1422 size -= sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1423 data += sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1425 if (size < 2 * sizeof(USHORT) + 64)
1427 TRACE("no space for signature\n");
1428 return S_OK; /* signature is optional */
1431 signature = (const USHORT *)data;
1432 TRACE("signature version %04x, client version %04x\n", signature[0], signature[1]);
1434 return S_OK;
1437 static HRESULT WINAPI MSTASK_IPersistFile_Load(IPersistFile *iface, LPCOLESTR file_name, DWORD mode)
1439 TaskImpl *This = impl_from_IPersistFile(iface);
1440 HRESULT hr;
1441 HANDLE file, mapping;
1442 DWORD access, sharing, size, try;
1443 void *data;
1445 TRACE("(%p, %s, 0x%08x)\n", iface, debugstr_w(file_name), mode);
1447 switch (mode & 0x000f)
1449 default:
1450 case STGM_READ:
1451 access = GENERIC_READ;
1452 break;
1453 case STGM_WRITE:
1454 case STGM_READWRITE:
1455 access = GENERIC_READ | GENERIC_WRITE;
1456 break;
1459 switch (mode & 0x00f0)
1461 default:
1462 case STGM_SHARE_DENY_NONE:
1463 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1464 break;
1465 case STGM_SHARE_DENY_READ:
1466 sharing = FILE_SHARE_WRITE;
1467 break;
1468 case STGM_SHARE_DENY_WRITE:
1469 sharing = FILE_SHARE_READ;
1470 break;
1471 case STGM_SHARE_EXCLUSIVE:
1472 sharing = 0;
1473 break;
1476 try = 1;
1477 for (;;)
1479 file = CreateFileW(file_name, access, sharing, NULL, OPEN_EXISTING, 0, 0);
1480 if (file != INVALID_HANDLE_VALUE) break;
1482 if (GetLastError() != ERROR_SHARING_VIOLATION || try++ >= 3)
1484 TRACE("Failed to open %s, error %u\n", debugstr_w(file_name), GetLastError());
1485 return HRESULT_FROM_WIN32(GetLastError());
1487 Sleep(100);
1490 size = GetFileSize(file, NULL);
1492 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, 0);
1493 if (!mapping)
1495 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name), GetLastError());
1496 CloseHandle(file);
1497 return HRESULT_FROM_WIN32(GetLastError());
1500 data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1501 if (data)
1503 hr = load_job_data(This, data, size);
1504 if (hr == S_OK) This->is_dirty = FALSE;
1505 UnmapViewOfFile(data);
1507 else
1508 hr = HRESULT_FROM_WIN32(GetLastError());
1510 CloseHandle(mapping);
1511 CloseHandle(file);
1513 return hr;
1516 static BOOL write_signature(HANDLE hfile)
1518 struct
1520 USHORT SignatureVersion;
1521 USHORT ClientVersion;
1522 BYTE md5[64];
1523 } signature;
1524 DWORD size;
1526 signature.SignatureVersion = 0x0001;
1527 signature.ClientVersion = 0x0001;
1528 memset(&signature.md5, 0, sizeof(signature.md5));
1530 return WriteFile(hfile, &signature, sizeof(signature), &size, NULL);
1533 static BOOL write_reserved_data(HANDLE hfile)
1535 static const struct
1537 USHORT size;
1538 BYTE data[8];
1539 } user = { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1540 DWORD size;
1542 return WriteFile(hfile, &user, sizeof(user), &size, NULL);
1545 static BOOL write_user_data(HANDLE hfile, BYTE *data, WORD data_size)
1547 DWORD size;
1549 if (!WriteFile(hfile, &data_size, sizeof(data_size), &size, NULL))
1550 return FALSE;
1552 if (!data_size) return TRUE;
1554 return WriteFile(hfile, data, data_size, &size, NULL);
1557 static HRESULT write_triggers(TaskImpl *This, HANDLE hfile)
1559 WORD count, i, idx = 0xffff;
1560 DWORD size;
1561 HRESULT hr = S_OK;
1562 ITaskTrigger *trigger;
1564 count = This->trigger_count;
1566 /* Windows saves a .job with at least 1 trigger */
1567 if (!count)
1569 hr = ITask_CreateTrigger(&This->ITask_iface, &idx, &trigger);
1570 if (hr != S_OK) return hr;
1571 ITaskTrigger_Release(trigger);
1573 count = 1;
1576 if (WriteFile(hfile, &count, sizeof(count), &size, NULL))
1578 for (i = 0; i < count; i++)
1580 if (!WriteFile(hfile, &This->trigger[i], sizeof(This->trigger[0]), &size, NULL))
1582 hr = HRESULT_FROM_WIN32(GetLastError());
1583 break;
1587 else
1588 hr = HRESULT_FROM_WIN32(GetLastError());
1590 if (idx != 0xffff)
1591 ITask_DeleteTrigger(&This->ITask_iface, idx);
1593 return hr;
1596 static BOOL write_unicode_string(HANDLE hfile, const WCHAR *str)
1598 USHORT count;
1599 DWORD size;
1601 count = str ? (lstrlenW(str) + 1) : 0;
1602 if (!WriteFile(hfile, &count, sizeof(count), &size, NULL))
1603 return FALSE;
1605 if (!str) return TRUE;
1607 count *= sizeof(WCHAR);
1608 return WriteFile(hfile, str, count, &size, NULL);
1611 static HRESULT WINAPI MSTASK_IPersistFile_Save(IPersistFile *iface, LPCOLESTR task_name, BOOL remember)
1613 static WCHAR authorW[] = { 'W','i','n','e',0 };
1614 static WCHAR commentW[] = { 'C','r','e','a','t','e','d',' ','b','y',' ','W','i','n','e',0 };
1615 FIXDLEN_DATA fixed;
1616 WORD word, user_data_size = 0;
1617 HANDLE hfile;
1618 DWORD size, ver, disposition, try;
1619 TaskImpl *This = impl_from_IPersistFile(iface);
1620 ITask *task = &This->ITask_iface;
1621 LPWSTR appname = NULL, params = NULL, workdir = NULL, creator = NULL, comment = NULL;
1622 BYTE *user_data = NULL;
1623 HRESULT hr;
1625 TRACE("(%p, %s, %d)\n", iface, debugstr_w(task_name), remember);
1627 disposition = task_name ? CREATE_NEW : OPEN_ALWAYS;
1629 if (!task_name)
1631 task_name = This->task_name;
1632 remember = FALSE;
1635 ITask_GetComment(task, &comment);
1636 if (!comment) comment = commentW;
1637 ITask_GetCreator(task, &creator);
1638 if (!creator) creator = authorW;
1639 ITask_GetApplicationName(task, &appname);
1640 ITask_GetParameters(task, &params);
1641 ITask_GetWorkingDirectory(task, &workdir);
1642 ITask_GetWorkItemData(task, &user_data_size, &user_data);
1644 ver = GetVersion();
1645 fixed.product_version = MAKEWORD(ver >> 8, ver);
1646 fixed.file_version = 0x0001;
1647 fixed.name_size_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1648 fixed.trigger_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1649 fixed.trigger_offset += sizeof(USHORT); /* Application Name */
1650 if (appname)
1651 fixed.trigger_offset += (lstrlenW(appname) + 1) * sizeof(WCHAR);
1652 fixed.trigger_offset += sizeof(USHORT); /* Parameters */
1653 if (params)
1654 fixed.trigger_offset += (lstrlenW(params) + 1) * sizeof(WCHAR);
1655 fixed.trigger_offset += sizeof(USHORT); /* Working Directory */
1656 if (workdir)
1657 fixed.trigger_offset += (lstrlenW(workdir) + 1) * sizeof(WCHAR);
1658 fixed.trigger_offset += sizeof(USHORT); /* Author */
1659 if (creator)
1660 fixed.trigger_offset += (lstrlenW(creator) + 1) * sizeof(WCHAR);
1661 fixed.trigger_offset += sizeof(USHORT); /* Comment */
1662 if (comment)
1663 fixed.trigger_offset += (lstrlenW(comment) + 1) * sizeof(WCHAR);
1664 fixed.trigger_offset += sizeof(USHORT) + user_data_size; /* User Data */
1665 fixed.trigger_offset += 10; /* Reserved Data */
1667 fixed.error_retry_count = 0;
1668 fixed.error_retry_interval = 0;
1669 fixed.idle_wait = This->idle_minutes;
1670 fixed.idle_deadline = This->deadline_minutes;
1671 fixed.priority = This->priority;
1672 fixed.maximum_runtime = This->maxRunTime;
1673 fixed.exit_code = This->exit_code;
1674 if (This->status == SCHED_S_TASK_NOT_SCHEDULED && This->trigger_count)
1675 This->status = SCHED_S_TASK_HAS_NOT_RUN;
1676 fixed.status = This->status;
1677 fixed.flags = This->flags;
1678 fixed.last_runtime = This->last_runtime;
1680 try = 1;
1681 for (;;)
1683 hfile = CreateFileW(task_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, disposition, 0, 0);
1684 if (hfile != INVALID_HANDLE_VALUE) break;
1686 if (try++ >= 3) return HRESULT_FROM_WIN32(GetLastError());
1687 Sleep(100);
1690 if (GetLastError() == ERROR_ALREADY_EXISTS)
1691 fixed.uuid = This->uuid;
1692 else
1693 CoCreateGuid(&fixed.uuid);
1695 if (!WriteFile(hfile, &fixed, sizeof(fixed), &size, NULL))
1697 hr = HRESULT_FROM_WIN32(GetLastError());
1698 goto failed;
1701 /* Instance Count: don't touch it in the client */
1702 if (SetFilePointer(hfile, sizeof(word), NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
1704 hr = HRESULT_FROM_WIN32(GetLastError());
1705 goto failed;
1707 /* Application Name */
1708 if (!write_unicode_string(hfile, appname))
1710 hr = HRESULT_FROM_WIN32(GetLastError());
1711 goto failed;
1713 /* Parameters */
1714 if (!write_unicode_string(hfile, params))
1716 hr = HRESULT_FROM_WIN32(GetLastError());
1717 goto failed;
1719 /* Working Directory */
1720 if (!write_unicode_string(hfile, workdir))
1722 hr = HRESULT_FROM_WIN32(GetLastError());
1723 goto failed;
1725 /* Author */
1726 if (!write_unicode_string(hfile, creator))
1728 hr = HRESULT_FROM_WIN32(GetLastError());
1729 goto failed;
1731 /* Comment */
1732 if (!write_unicode_string(hfile, comment))
1734 hr = HRESULT_FROM_WIN32(GetLastError());
1735 goto failed;
1738 /* User Data */
1739 if (!write_user_data(hfile, user_data, user_data_size))
1741 hr = HRESULT_FROM_WIN32(GetLastError());
1742 goto failed;
1745 /* Reserved Data */
1746 if (!write_reserved_data(hfile))
1748 hr = HRESULT_FROM_WIN32(GetLastError());
1749 goto failed;
1752 /* Triggers */
1753 hr = write_triggers(This, hfile);
1754 if (hr != S_OK)
1755 goto failed;
1757 /* Signature */
1758 if (!write_signature(hfile))
1760 hr = HRESULT_FROM_WIN32(GetLastError());
1761 goto failed;
1764 hr = S_OK;
1765 This->is_dirty = FALSE;
1767 failed:
1768 CoTaskMemFree(appname);
1769 CoTaskMemFree(params);
1770 CoTaskMemFree(workdir);
1771 if (creator != authorW)
1772 CoTaskMemFree(creator);
1773 if (comment != commentW)
1774 CoTaskMemFree(comment);
1775 CoTaskMemFree(user_data);
1777 CloseHandle(hfile);
1778 if (hr != S_OK)
1779 DeleteFileW(task_name);
1780 else if (remember)
1782 heap_free(This->task_name);
1783 This->task_name = heap_strdupW(task_name);
1785 return hr;
1788 static HRESULT WINAPI MSTASK_IPersistFile_SaveCompleted(
1789 IPersistFile* iface,
1790 LPCOLESTR pszFileName)
1792 FIXME("(%p, %p): stub\n", iface, pszFileName);
1793 return E_NOTIMPL;
1796 static HRESULT WINAPI MSTASK_IPersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *file_name)
1798 TaskImpl *This = impl_from_IPersistFile(iface);
1800 TRACE("(%p, %p)\n", iface, file_name);
1802 *file_name = CoTaskMemAlloc((strlenW(This->task_name) + 1) * sizeof(WCHAR));
1803 if (!*file_name) return E_OUTOFMEMORY;
1805 strcpyW(*file_name, This->task_name);
1806 return S_OK;
1809 static const ITaskVtbl MSTASK_ITaskVtbl =
1811 MSTASK_ITask_QueryInterface,
1812 MSTASK_ITask_AddRef,
1813 MSTASK_ITask_Release,
1814 MSTASK_ITask_CreateTrigger,
1815 MSTASK_ITask_DeleteTrigger,
1816 MSTASK_ITask_GetTriggerCount,
1817 MSTASK_ITask_GetTrigger,
1818 MSTASK_ITask_GetTriggerString,
1819 MSTASK_ITask_GetRunTimes,
1820 MSTASK_ITask_GetNextRunTime,
1821 MSTASK_ITask_SetIdleWait,
1822 MSTASK_ITask_GetIdleWait,
1823 MSTASK_ITask_Run,
1824 MSTASK_ITask_Terminate,
1825 MSTASK_ITask_EditWorkItem,
1826 MSTASK_ITask_GetMostRecentRunTime,
1827 MSTASK_ITask_GetStatus,
1828 MSTASK_ITask_GetExitCode,
1829 MSTASK_ITask_SetComment,
1830 MSTASK_ITask_GetComment,
1831 MSTASK_ITask_SetCreator,
1832 MSTASK_ITask_GetCreator,
1833 MSTASK_ITask_SetWorkItemData,
1834 MSTASK_ITask_GetWorkItemData,
1835 MSTASK_ITask_SetErrorRetryCount,
1836 MSTASK_ITask_GetErrorRetryCount,
1837 MSTASK_ITask_SetErrorRetryInterval,
1838 MSTASK_ITask_GetErrorRetryInterval,
1839 MSTASK_ITask_SetFlags,
1840 MSTASK_ITask_GetFlags,
1841 MSTASK_ITask_SetAccountInformation,
1842 MSTASK_ITask_GetAccountInformation,
1843 MSTASK_ITask_SetApplicationName,
1844 MSTASK_ITask_GetApplicationName,
1845 MSTASK_ITask_SetParameters,
1846 MSTASK_ITask_GetParameters,
1847 MSTASK_ITask_SetWorkingDirectory,
1848 MSTASK_ITask_GetWorkingDirectory,
1849 MSTASK_ITask_SetPriority,
1850 MSTASK_ITask_GetPriority,
1851 MSTASK_ITask_SetTaskFlags,
1852 MSTASK_ITask_GetTaskFlags,
1853 MSTASK_ITask_SetMaxRunTime,
1854 MSTASK_ITask_GetMaxRunTime
1857 static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
1859 MSTASK_IPersistFile_QueryInterface,
1860 MSTASK_IPersistFile_AddRef,
1861 MSTASK_IPersistFile_Release,
1862 MSTASK_IPersistFile_GetClassID,
1863 MSTASK_IPersistFile_IsDirty,
1864 MSTASK_IPersistFile_Load,
1865 MSTASK_IPersistFile_Save,
1866 MSTASK_IPersistFile_SaveCompleted,
1867 MSTASK_IPersistFile_GetCurFile
1870 HRESULT TaskConstructor(ITaskService *service, const WCHAR *name, ITask **task)
1872 static const WCHAR tasksW[] = { '\\','T','a','s','k','s','\\',0 };
1873 static const WCHAR jobW[] = { '.','j','o','b',0 };
1874 TaskImpl *This;
1875 WCHAR task_name[MAX_PATH];
1876 ITaskDefinition *taskdef;
1877 IActionCollection *actions;
1878 HRESULT hr;
1880 TRACE("(%s, %p)\n", debugstr_w(name), task);
1882 if (strchrW(name, '.')) return E_INVALIDARG;
1884 GetWindowsDirectoryW(task_name, MAX_PATH);
1885 lstrcatW(task_name, tasksW);
1886 lstrcatW(task_name, name);
1887 lstrcatW(task_name, jobW);
1889 hr = ITaskService_NewTask(service, 0, &taskdef);
1890 if (hr != S_OK) return hr;
1892 This = heap_alloc(sizeof(*This));
1893 if (!This)
1895 ITaskDefinition_Release(taskdef);
1896 return E_OUTOFMEMORY;
1899 This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
1900 This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
1901 This->ref = 1;
1902 This->task = taskdef;
1903 This->data = NULL;
1904 This->data_count = 0;
1905 This->task_name = heap_strdupW(task_name);
1906 This->flags = 0;
1907 This->status = SCHED_S_TASK_NOT_SCHEDULED;
1908 This->exit_code = 0;
1909 This->idle_minutes = 10;
1910 This->deadline_minutes = 60;
1911 This->priority = NORMAL_PRIORITY_CLASS;
1912 This->accountName = NULL;
1913 This->trigger_count = 0;
1914 This->trigger = NULL;
1915 This->is_dirty = FALSE;
1916 This->instance_count = 0;
1918 memset(&This->last_runtime, 0, sizeof(This->last_runtime));
1919 CoCreateGuid(&This->uuid);
1921 /* Default time is 3 days = 259200000 ms */
1922 This->maxRunTime = 259200000;
1924 hr = ITaskDefinition_get_Actions(This->task, &actions);
1925 if (hr == S_OK)
1927 hr = IActionCollection_Create(actions, TASK_ACTION_EXEC, (IAction **)&This->action);
1928 IActionCollection_Release(actions);
1929 if (hr == S_OK)
1931 *task = &This->ITask_iface;
1932 InterlockedIncrement(&dll_ref);
1933 return S_OK;
1937 ITaskDefinition_Release(This->task);
1938 ITask_Release(&This->ITask_iface);
1939 return hr;