mstask: Handle disabled tasks in ITask::GetNextRunTime().
[wine.git] / dlls / mstask / task.c
bloba201e44a1f44a9070a38ed8a5c9025fcf71867c5
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 SYSTEMTIME st, current_st;
473 FILETIME current_ft, begin_ft, end_ft, best_ft;
474 BOOL have_best_time = FALSE;
475 DWORD i;
477 TRACE("(%p, %p)\n", iface, rt);
479 if (This->flags & TASK_FLAG_DISABLED)
481 memset(rt, 0, sizeof(*rt));
482 return SCHED_S_TASK_DISABLED;
485 GetLocalTime(&current_st);
487 for (i = 0; i < This->trigger_count; i++)
489 if (!(This->trigger[i].rgFlags & TASK_TRIGGER_FLAG_DISABLED))
491 get_begin_time(&This->trigger[i], &begin_ft);
492 get_end_time(&This->trigger[i], &end_ft);
494 switch (This->trigger[i].TriggerType)
496 case TASK_TIME_TRIGGER_ONCE:
497 st = current_st;
498 st.wHour = This->trigger[i].wStartHour;
499 st.wMinute = This->trigger[i].wStartMinute;
500 st.wSecond = 0;
501 st.wMilliseconds = 0;
502 SystemTimeToFileTime(&st, &current_ft);
503 if (CompareFileTime(&begin_ft, &current_ft) <= 0 && CompareFileTime(&current_ft, &end_ft) < 0)
505 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
507 best_ft = current_ft;
508 have_best_time = TRUE;
511 break;
513 case TASK_TIME_TRIGGER_DAILY:
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 while (CompareFileTime(&current_ft, &end_ft) < 0)
522 if (CompareFileTime(&current_ft, &begin_ft) >= 0)
524 if (!have_best_time || CompareFileTime(&current_ft, &best_ft) < 0)
526 best_ft = current_ft;
527 have_best_time = TRUE;
529 break;
532 filetime_add_days(&current_ft, This->trigger[i].Type.Daily.DaysInterval);
534 break;
536 default:
537 FIXME("trigger type %u is not handled\n", This->trigger[i].TriggerType);
538 break;
543 if (have_best_time)
545 FileTimeToSystemTime(&best_ft, rt);
546 return S_OK;
549 memset(rt, 0, sizeof(*rt));
550 return SCHED_S_TASK_NO_VALID_TRIGGERS;
553 static HRESULT WINAPI MSTASK_ITask_SetIdleWait(
554 ITask* iface,
555 WORD wIdleMinutes,
556 WORD wDeadlineMinutes)
558 FIXME("(%p, %d, %d): stub\n", iface, wIdleMinutes, wDeadlineMinutes);
559 return E_NOTIMPL;
562 static HRESULT WINAPI MSTASK_ITask_GetIdleWait(ITask *iface, WORD *idle_minutes, WORD *deadline_minutes)
564 TaskImpl *This = impl_from_ITask(iface);
566 TRACE("(%p, %p, %p): stub\n", iface, idle_minutes, deadline_minutes);
568 *idle_minutes = This->idle_minutes;
569 *deadline_minutes = This->deadline_minutes;
570 return S_OK;
573 static HRESULT WINAPI MSTASK_ITask_Run(ITask *iface)
575 TaskImpl *This = impl_from_ITask(iface);
577 TRACE("(%p)\n", iface);
579 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
580 return SCHED_E_TASK_NOT_READY;
582 This->flags |= 0x04000000;
583 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
586 static HRESULT WINAPI MSTASK_ITask_Terminate(ITask *iface)
588 TaskImpl *This = impl_from_ITask(iface);
590 TRACE("(%p)\n", iface);
592 if (!This->instance_count)
593 return SCHED_E_TASK_NOT_RUNNING;
595 This->flags |= 0x08000000;
596 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
599 static HRESULT WINAPI MSTASK_ITask_EditWorkItem(
600 ITask* iface,
601 HWND hParent,
602 DWORD dwReserved)
604 FIXME("(%p, %p, %d): stub\n", iface, hParent, dwReserved);
605 return E_NOTIMPL;
608 static HRESULT WINAPI MSTASK_ITask_GetMostRecentRunTime(ITask *iface, SYSTEMTIME *st)
610 FIXME("(%p, %p): stub\n", iface, st);
612 memset(st, 0, sizeof(*st));
613 return SCHED_S_TASK_HAS_NOT_RUN;
616 static HRESULT WINAPI MSTASK_ITask_GetStatus(ITask *iface, HRESULT *status)
618 TaskImpl *This = impl_from_ITask(iface);
620 TRACE("(%p, %p)\n", iface, status);
622 *status = This->instance_count ? SCHED_S_TASK_RUNNING : This->status;
623 return S_OK;
626 static HRESULT WINAPI MSTASK_ITask_GetExitCode(ITask *iface, DWORD *exit_code)
628 TaskImpl *This = impl_from_ITask(iface);
630 TRACE("(%p, %p)\n", iface, exit_code);
632 *exit_code = This->exit_code;
633 return SCHED_S_TASK_HAS_NOT_RUN; /* FIXME */
636 static HRESULT WINAPI MSTASK_ITask_SetComment(ITask *iface, LPCWSTR comment)
638 TaskImpl *This = impl_from_ITask(iface);
639 IRegistrationInfo *info;
640 HRESULT hr;
642 TRACE("(%p, %s)\n", iface, debugstr_w(comment));
644 if (!comment || !comment[0])
645 comment = NULL;
647 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
648 if (hr == S_OK)
650 hr = IRegistrationInfo_put_Description(info, (BSTR)comment);
651 IRegistrationInfo_Release(info);
652 This->is_dirty = TRUE;
654 return hr;
657 static HRESULT WINAPI MSTASK_ITask_GetComment(ITask *iface, LPWSTR *comment)
659 TaskImpl *This = impl_from_ITask(iface);
660 IRegistrationInfo *info;
661 HRESULT hr;
662 BSTR description;
663 DWORD len;
665 TRACE("(%p, %p)\n", iface, comment);
667 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
668 if (hr != S_OK) return hr;
670 hr = IRegistrationInfo_get_Description(info, &description);
671 if (hr == S_OK)
673 len = description ? lstrlenW(description) + 1 : 1;
674 *comment = CoTaskMemAlloc(len * sizeof(WCHAR));
675 if (*comment)
677 if (!description)
678 *comment[0] = 0;
679 else
680 lstrcpyW(*comment, description);
681 hr = S_OK;
683 else
684 hr = E_OUTOFMEMORY;
686 SysFreeString(description);
689 IRegistrationInfo_Release(info);
690 return hr;
693 static HRESULT WINAPI MSTASK_ITask_SetCreator(ITask *iface, LPCWSTR creator)
695 TaskImpl *This = impl_from_ITask(iface);
696 IRegistrationInfo *info;
697 HRESULT hr;
699 TRACE("(%p, %s)\n", iface, debugstr_w(creator));
701 if (!creator || !creator[0])
702 creator = NULL;
704 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
705 if (hr == S_OK)
707 hr = IRegistrationInfo_put_Author(info, (BSTR)creator);
708 IRegistrationInfo_Release(info);
709 This->is_dirty = TRUE;
711 return hr;
714 static HRESULT WINAPI MSTASK_ITask_GetCreator(ITask *iface, LPWSTR *creator)
716 TaskImpl *This = impl_from_ITask(iface);
717 IRegistrationInfo *info;
718 HRESULT hr;
719 BSTR author;
720 DWORD len;
722 TRACE("(%p, %p)\n", iface, creator);
724 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
725 if (hr != S_OK) return hr;
727 hr = IRegistrationInfo_get_Author(info, &author);
728 if (hr == S_OK)
730 len = author ? lstrlenW(author) + 1 : 1;
731 *creator = CoTaskMemAlloc(len * sizeof(WCHAR));
732 if (*creator)
734 if (!author)
735 *creator[0] = 0;
736 else
737 lstrcpyW(*creator, author);
738 hr = S_OK;
740 else
741 hr = E_OUTOFMEMORY;
743 SysFreeString(author);
746 IRegistrationInfo_Release(info);
747 return hr;
750 static HRESULT WINAPI MSTASK_ITask_SetWorkItemData(
751 ITask* iface,
752 WORD cBytes,
753 BYTE rgbData[])
755 FIXME("(%p, %d, %p): stub\n", iface, cBytes, rgbData);
756 return E_NOTIMPL;
759 static HRESULT WINAPI MSTASK_ITask_GetWorkItemData(
760 ITask* iface,
761 WORD *pcBytes,
762 BYTE **ppBytes)
764 FIXME("(%p, %p, %p): stub\n", iface, pcBytes, ppBytes);
765 return E_NOTIMPL;
768 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryCount(
769 ITask* iface,
770 WORD wRetryCount)
772 FIXME("(%p, %d): stub\n", iface, wRetryCount);
773 return E_NOTIMPL;
776 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryCount(ITask *iface, WORD *count)
778 TRACE("(%p, %p)\n", iface, count);
779 return E_NOTIMPL;
782 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryInterval(
783 ITask* iface,
784 WORD wRetryInterval)
786 FIXME("(%p, %d): stub\n", iface, wRetryInterval);
787 return E_NOTIMPL;
790 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryInterval(ITask *iface, WORD *interval)
792 TRACE("(%p, %p)\n", iface, interval);
793 return E_NOTIMPL;
796 static HRESULT WINAPI MSTASK_ITask_SetFlags(ITask *iface, DWORD flags)
798 TaskImpl *This = impl_from_ITask(iface);
800 TRACE("(%p, 0x%08x)\n", iface, flags);
801 This->flags &= 0xffff8000;
802 This->flags |= flags & 0x7fff;
803 This->is_dirty = TRUE;
804 return S_OK;
807 static HRESULT WINAPI MSTASK_ITask_GetFlags(ITask *iface, DWORD *flags)
809 TaskImpl *This = impl_from_ITask(iface);
811 TRACE("(%p, %p)\n", iface, flags);
812 *flags = LOWORD(This->flags);
813 return S_OK;
816 static HRESULT WINAPI MSTASK_ITask_SetAccountInformation(
817 ITask* iface,
818 LPCWSTR pwszAccountName,
819 LPCWSTR pwszPassword)
821 DWORD n;
822 TaskImpl *This = impl_from_ITask(iface);
823 LPWSTR tmp_account_name;
825 TRACE("(%p, %s, %s): partial stub\n", iface, debugstr_w(pwszAccountName),
826 debugstr_w(pwszPassword));
828 if (pwszPassword)
829 FIXME("Partial stub ignores passwords\n");
831 n = (lstrlenW(pwszAccountName) + 1);
832 tmp_account_name = heap_alloc(n * sizeof(WCHAR));
833 if (!tmp_account_name)
834 return E_OUTOFMEMORY;
835 lstrcpyW(tmp_account_name, pwszAccountName);
836 heap_free(This->accountName);
837 This->accountName = tmp_account_name;
838 This->is_dirty = TRUE;
839 return S_OK;
842 static HRESULT WINAPI MSTASK_ITask_GetAccountInformation(
843 ITask* iface,
844 LPWSTR *ppwszAccountName)
846 DWORD n;
847 TaskImpl *This = impl_from_ITask(iface);
849 TRACE("(%p, %p): partial stub\n", iface, ppwszAccountName);
851 /* This implements the WinXP behavior when accountName has not yet
852 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
853 if (!This->accountName)
854 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
856 n = (lstrlenW(This->accountName) + 1);
857 *ppwszAccountName = CoTaskMemAlloc(n * sizeof(WCHAR));
858 if (!*ppwszAccountName)
859 return E_OUTOFMEMORY;
860 lstrcpyW(*ppwszAccountName, This->accountName);
861 return S_OK;
864 static HRESULT WINAPI MSTASK_ITask_SetApplicationName(ITask *iface, LPCWSTR appname)
866 TaskImpl *This = impl_from_ITask(iface);
867 DWORD len;
868 HRESULT hr;
870 TRACE("(%p, %s)\n", iface, debugstr_w(appname));
872 /* Empty application name */
873 if (!appname || !appname[0])
874 return IExecAction_put_Path(This->action, NULL);
876 /* Attempt to set pwszApplicationName to a path resolved application name */
877 len = SearchPathW(NULL, appname, NULL, 0, NULL, NULL);
878 if (len)
880 LPWSTR tmp_name;
882 tmp_name = heap_alloc(len * sizeof(WCHAR));
883 if (!tmp_name)
884 return E_OUTOFMEMORY;
885 len = SearchPathW(NULL, appname, NULL, len, tmp_name, NULL);
886 if (len)
888 hr = IExecAction_put_Path(This->action, tmp_name);
889 if (hr == S_OK) This->is_dirty = TRUE;
891 else
892 hr = HRESULT_FROM_WIN32(GetLastError());
894 heap_free(tmp_name);
895 return hr;
898 /* If unable to path resolve name, simply set to appname */
899 hr = IExecAction_put_Path(This->action, (BSTR)appname);
900 if (hr == S_OK) This->is_dirty = TRUE;
901 return hr;
904 static HRESULT WINAPI MSTASK_ITask_GetApplicationName(ITask *iface, LPWSTR *appname)
906 TaskImpl *This = impl_from_ITask(iface);
907 HRESULT hr;
908 BSTR path;
909 DWORD len;
911 TRACE("(%p, %p)\n", iface, appname);
913 hr = IExecAction_get_Path(This->action, &path);
914 if (hr != S_OK) return hr;
916 len = path ? lstrlenW(path) + 1 : 1;
917 *appname = CoTaskMemAlloc(len * sizeof(WCHAR));
918 if (*appname)
920 if (!path)
921 *appname[0] = 0;
922 else
923 lstrcpyW(*appname, path);
924 hr = S_OK;
926 else
927 hr = E_OUTOFMEMORY;
929 SysFreeString(path);
930 return hr;
933 static HRESULT WINAPI MSTASK_ITask_SetParameters(ITask *iface, LPCWSTR params)
935 TaskImpl *This = impl_from_ITask(iface);
936 HRESULT hr;
938 TRACE("(%p, %s)\n", iface, debugstr_w(params));
940 /* Empty parameter list */
941 if (!params || !params[0])
942 params = NULL;
944 hr = IExecAction_put_Arguments(This->action, (BSTR)params);
945 if (hr == S_OK) This->is_dirty = TRUE;
946 return hr;
949 static HRESULT WINAPI MSTASK_ITask_GetParameters(ITask *iface, LPWSTR *params)
951 TaskImpl *This = impl_from_ITask(iface);
952 HRESULT hr;
953 BSTR args;
954 DWORD len;
956 TRACE("(%p, %p)\n", iface, params);
958 hr = IExecAction_get_Arguments(This->action, &args);
959 if (hr != S_OK) return hr;
961 len = args ? lstrlenW(args) + 1 : 1;
962 *params = CoTaskMemAlloc(len * sizeof(WCHAR));
963 if (*params)
965 if (!args)
966 *params[0] = 0;
967 else
968 lstrcpyW(*params, args);
969 hr = S_OK;
971 else
972 hr = E_OUTOFMEMORY;
974 SysFreeString(args);
975 return hr;
978 static HRESULT WINAPI MSTASK_ITask_SetWorkingDirectory(ITask * iface, LPCWSTR workdir)
980 TaskImpl *This = impl_from_ITask(iface);
981 HRESULT hr;
983 TRACE("(%p, %s)\n", iface, debugstr_w(workdir));
985 if (!workdir || !workdir[0])
986 workdir = NULL;
988 hr = IExecAction_put_WorkingDirectory(This->action, (BSTR)workdir);
989 if (hr == S_OK) This->is_dirty = TRUE;
990 return hr;
993 static HRESULT WINAPI MSTASK_ITask_GetWorkingDirectory(ITask *iface, LPWSTR *workdir)
995 TaskImpl *This = impl_from_ITask(iface);
996 HRESULT hr;
997 BSTR dir;
998 DWORD len;
1000 TRACE("(%p, %p)\n", iface, workdir);
1002 hr = IExecAction_get_WorkingDirectory(This->action, &dir);
1003 if (hr != S_OK) return hr;
1005 len = dir ? lstrlenW(dir) + 1 : 1;
1006 *workdir = CoTaskMemAlloc(len * sizeof(WCHAR));
1007 if (*workdir)
1009 if (!dir)
1010 *workdir[0] = 0;
1011 else
1012 lstrcpyW(*workdir, dir);
1013 hr = S_OK;
1015 else
1016 hr = E_OUTOFMEMORY;
1018 SysFreeString(dir);
1019 return hr;
1022 static HRESULT WINAPI MSTASK_ITask_SetPriority(
1023 ITask* iface,
1024 DWORD dwPriority)
1026 FIXME("(%p, 0x%08x): stub\n", iface, dwPriority);
1027 return E_NOTIMPL;
1030 static HRESULT WINAPI MSTASK_ITask_GetPriority(ITask *iface, DWORD *priority)
1032 TaskImpl *This = impl_from_ITask(iface);
1034 TRACE("(%p, %p)\n", iface, priority);
1036 *priority = This->priority;
1037 return S_OK;
1040 static HRESULT WINAPI MSTASK_ITask_SetTaskFlags(
1041 ITask* iface,
1042 DWORD dwFlags)
1044 FIXME("(%p, 0x%08x): stub\n", iface, dwFlags);
1045 return E_NOTIMPL;
1048 static HRESULT WINAPI MSTASK_ITask_GetTaskFlags(ITask *iface, DWORD *flags)
1050 FIXME("(%p, %p): stub\n", iface, flags);
1051 *flags = 0;
1052 return S_OK;
1055 static HRESULT WINAPI MSTASK_ITask_SetMaxRunTime(
1056 ITask* iface,
1057 DWORD dwMaxRunTime)
1059 TaskImpl *This = impl_from_ITask(iface);
1061 TRACE("(%p, %d)\n", iface, dwMaxRunTime);
1063 This->maxRunTime = dwMaxRunTime;
1064 This->is_dirty = TRUE;
1065 return S_OK;
1068 static HRESULT WINAPI MSTASK_ITask_GetMaxRunTime(
1069 ITask* iface,
1070 DWORD *pdwMaxRunTime)
1072 TaskImpl *This = impl_from_ITask(iface);
1074 TRACE("(%p, %p)\n", iface, pdwMaxRunTime);
1076 *pdwMaxRunTime = This->maxRunTime;
1077 return S_OK;
1080 static HRESULT WINAPI MSTASK_IPersistFile_QueryInterface(
1081 IPersistFile* iface,
1082 REFIID riid,
1083 void **ppvObject)
1085 TaskImpl *This = impl_from_IPersistFile(iface);
1086 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
1087 return ITask_QueryInterface(&This->ITask_iface, riid, ppvObject);
1090 static ULONG WINAPI MSTASK_IPersistFile_AddRef(
1091 IPersistFile* iface)
1093 TaskImpl *This = impl_from_IPersistFile(iface);
1094 return ITask_AddRef(&This->ITask_iface);
1097 static ULONG WINAPI MSTASK_IPersistFile_Release(
1098 IPersistFile* iface)
1100 TaskImpl *This = impl_from_IPersistFile(iface);
1101 return ITask_Release(&This->ITask_iface);
1104 static HRESULT WINAPI MSTASK_IPersistFile_GetClassID(IPersistFile *iface, CLSID *clsid)
1106 TRACE("(%p, %p)\n", iface, clsid);
1108 *clsid = CLSID_CTask;
1109 return S_OK;
1112 static HRESULT WINAPI MSTASK_IPersistFile_IsDirty(IPersistFile *iface)
1114 TaskImpl *This = impl_from_IPersistFile(iface);
1115 TRACE("(%p)\n", iface);
1116 return This->is_dirty ? S_OK : S_FALSE;
1119 static DWORD load_unicode_strings(ITask *task, BYTE *data, DWORD limit)
1121 DWORD i, data_size = 0;
1122 USHORT len;
1124 for (i = 0; i < 5; i++)
1126 if (limit < sizeof(USHORT))
1128 TRACE("invalid string %u offset\n", i);
1129 break;
1132 len = *(USHORT *)data;
1133 data += sizeof(USHORT);
1134 data_size += sizeof(USHORT);
1135 limit -= sizeof(USHORT);
1136 if (limit < len * sizeof(WCHAR))
1138 TRACE("invalid string %u size\n", i);
1139 break;
1142 TRACE("string %u: %s\n", i, wine_dbgstr_wn((const WCHAR *)data, len));
1144 switch (i)
1146 case 0:
1147 ITask_SetApplicationName(task, (const WCHAR *)data);
1148 break;
1149 case 1:
1150 ITask_SetParameters(task, (const WCHAR *)data);
1151 break;
1152 case 2:
1153 ITask_SetWorkingDirectory(task, (const WCHAR *)data);
1154 break;
1155 case 3:
1156 ITask_SetCreator(task, (const WCHAR *)data);
1157 break;
1158 case 4:
1159 ITask_SetComment(task, (const WCHAR *)data);
1160 break;
1161 default:
1162 break;
1165 data += len * sizeof(WCHAR);
1166 data_size += len * sizeof(WCHAR);
1169 return data_size;
1172 static HRESULT load_job_data(TaskImpl *This, BYTE *data, DWORD size)
1174 ITask *task = &This->ITask_iface;
1175 HRESULT hr;
1176 const FIXDLEN_DATA *fixed;
1177 const SYSTEMTIME *st;
1178 DWORD unicode_strings_size, data_size, triggers_size;
1179 USHORT trigger_count, i;
1180 const USHORT *signature;
1181 TASK_TRIGGER *task_trigger;
1183 if (size < sizeof(*fixed))
1185 TRACE("no space for FIXDLEN_DATA\n");
1186 return SCHED_E_INVALID_TASK;
1189 fixed = (const FIXDLEN_DATA *)data;
1191 TRACE("product_version %04x\n", fixed->product_version);
1192 TRACE("file_version %04x\n", fixed->file_version);
1193 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed->uuid));
1195 if (fixed->file_version != 0x0001)
1196 return SCHED_E_INVALID_TASK;
1198 TRACE("name_size_offset %04x\n", fixed->name_size_offset);
1199 TRACE("trigger_offset %04x\n", fixed->trigger_offset);
1200 TRACE("error_retry_count %u\n", fixed->error_retry_count);
1201 TRACE("error_retry_interval %u\n", fixed->error_retry_interval);
1202 TRACE("idle_deadline %u\n", fixed->idle_deadline);
1203 This->deadline_minutes = fixed->idle_deadline;
1204 TRACE("idle_wait %u\n", fixed->idle_wait);
1205 This->idle_minutes = fixed->idle_wait;
1206 TRACE("priority %08x\n", fixed->priority);
1207 This->priority = fixed->priority;
1208 TRACE("maximum_runtime %u\n", fixed->maximum_runtime);
1209 This->maxRunTime = fixed->maximum_runtime;
1210 TRACE("exit_code %#x\n", fixed->exit_code);
1211 This->exit_code = fixed->exit_code;
1212 TRACE("status %08x\n", fixed->status);
1213 This->status = fixed->status;
1214 TRACE("flags %08x\n", fixed->flags);
1215 This->flags = fixed->flags;
1216 st = &fixed->last_runtime;
1217 TRACE("last_runtime %d/%d/%d wday %d %d:%d:%d.%03d\n",
1218 st->wDay, st->wMonth, st->wYear, st->wDayOfWeek,
1219 st->wHour, st->wMinute, st->wSecond, st->wMilliseconds);
1221 /* Instance Count */
1222 if (size < sizeof(*fixed) + sizeof(USHORT))
1224 TRACE("no space for instance count\n");
1225 return SCHED_E_INVALID_TASK;
1228 This->instance_count = *(const USHORT *)(data + sizeof(*fixed));
1229 TRACE("instance count %u\n", This->instance_count);
1231 if (fixed->name_size_offset + sizeof(USHORT) < size)
1232 unicode_strings_size = load_unicode_strings(task, data + fixed->name_size_offset, size - fixed->name_size_offset);
1233 else
1235 TRACE("invalid name_size_offset\n");
1236 return SCHED_E_INVALID_TASK;
1238 TRACE("unicode strings end at %#x\n", fixed->name_size_offset + unicode_strings_size);
1240 if (size < fixed->trigger_offset + sizeof(USHORT))
1242 TRACE("no space for triggers count\n");
1243 return SCHED_E_INVALID_TASK;
1245 trigger_count = *(const USHORT *)(data + fixed->trigger_offset);
1246 TRACE("trigger_count %u\n", trigger_count);
1247 triggers_size = size - fixed->trigger_offset - sizeof(USHORT);
1248 TRACE("triggers_size %u\n", triggers_size);
1249 task_trigger = (TASK_TRIGGER *)(data + fixed->trigger_offset + sizeof(USHORT));
1251 data += fixed->name_size_offset + unicode_strings_size;
1252 size -= fixed->name_size_offset + unicode_strings_size;
1254 /* User Data */
1255 if (size < sizeof(USHORT))
1257 TRACE("no space for user data size\n");
1258 return SCHED_E_INVALID_TASK;
1261 data_size = *(const USHORT *)data;
1262 if (size < sizeof(USHORT) + data_size)
1264 TRACE("no space for user data\n");
1265 return SCHED_E_INVALID_TASK;
1267 TRACE("User Data size %#x\n", data_size);
1268 ITask_SetWorkItemData(task, data_size, data + sizeof(USHORT));
1270 size -= sizeof(USHORT) + data_size;
1271 data += sizeof(USHORT) + data_size;
1273 /* Reserved Data */
1274 if (size < sizeof(USHORT))
1276 TRACE("no space for reserved data size\n");
1277 return SCHED_E_INVALID_TASK;
1280 data_size = *(const USHORT *)data;
1281 if (size < sizeof(USHORT) + data_size)
1283 TRACE("no space for reserved data\n");
1284 return SCHED_E_INVALID_TASK;
1286 TRACE("Reserved Data size %#x\n", data_size);
1288 size -= sizeof(USHORT) + data_size;
1289 data += sizeof(USHORT) + data_size;
1291 /* Trigger Data */
1292 TRACE("trigger_offset %04x, triggers end at %04x\n", fixed->trigger_offset,
1293 (DWORD)(fixed->trigger_offset + sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER)));
1295 task_trigger = (TASK_TRIGGER *)(data + sizeof(USHORT));
1297 if (trigger_count * sizeof(TASK_TRIGGER) > triggers_size)
1299 TRACE("no space for triggers data\n");
1300 return SCHED_E_INVALID_TASK;
1303 This->trigger_count = 0;
1305 for (i = 0; i < trigger_count; i++)
1307 ITaskTrigger *trigger;
1308 WORD idx;
1310 hr = ITask_CreateTrigger(task, &idx, &trigger);
1311 if (hr != S_OK) return hr;
1313 hr = ITaskTrigger_SetTrigger(trigger, &task_trigger[i]);
1314 ITaskTrigger_Release(trigger);
1315 if (hr != S_OK)
1317 ITask_DeleteTrigger(task, idx);
1318 return hr;
1322 size -= sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1323 data += sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1325 if (size < 2 * sizeof(USHORT) + 64)
1327 TRACE("no space for signature\n");
1328 return S_OK; /* signature is optional */
1331 signature = (const USHORT *)data;
1332 TRACE("signature version %04x, client version %04x\n", signature[0], signature[1]);
1334 return S_OK;
1337 static HRESULT WINAPI MSTASK_IPersistFile_Load(IPersistFile *iface, LPCOLESTR file_name, DWORD mode)
1339 TaskImpl *This = impl_from_IPersistFile(iface);
1340 HRESULT hr;
1341 HANDLE file, mapping;
1342 DWORD access, sharing, size;
1343 void *data;
1345 TRACE("(%p, %s, 0x%08x)\n", iface, debugstr_w(file_name), mode);
1347 switch (mode & 0x000f)
1349 default:
1350 case STGM_READ:
1351 access = GENERIC_READ;
1352 break;
1353 case STGM_WRITE:
1354 case STGM_READWRITE:
1355 access = GENERIC_READ | GENERIC_WRITE;
1356 break;
1359 switch (mode & 0x00f0)
1361 default:
1362 case STGM_SHARE_DENY_NONE:
1363 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1364 break;
1365 case STGM_SHARE_DENY_READ:
1366 sharing = FILE_SHARE_WRITE;
1367 break;
1368 case STGM_SHARE_DENY_WRITE:
1369 sharing = FILE_SHARE_READ;
1370 break;
1371 case STGM_SHARE_EXCLUSIVE:
1372 sharing = 0;
1373 break;
1376 file = CreateFileW(file_name, access, sharing, NULL, OPEN_EXISTING, 0, 0);
1377 if (file == INVALID_HANDLE_VALUE)
1379 TRACE("Failed to open %s, error %u\n", debugstr_w(file_name), GetLastError());
1380 return HRESULT_FROM_WIN32(GetLastError());
1383 size = GetFileSize(file, NULL);
1385 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, 0);
1386 if (!mapping)
1388 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name), GetLastError());
1389 CloseHandle(file);
1390 return HRESULT_FROM_WIN32(GetLastError());
1393 data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1394 if (data)
1396 hr = load_job_data(This, data, size);
1397 if (hr == S_OK) This->is_dirty = FALSE;
1398 UnmapViewOfFile(data);
1400 else
1401 hr = HRESULT_FROM_WIN32(GetLastError());
1403 CloseHandle(mapping);
1404 CloseHandle(file);
1406 return hr;
1409 static BOOL write_signature(HANDLE hfile)
1411 struct
1413 USHORT SignatureVersion;
1414 USHORT ClientVersion;
1415 BYTE md5[64];
1416 } signature;
1417 DWORD size;
1419 signature.SignatureVersion = 0x0001;
1420 signature.ClientVersion = 0x0001;
1421 memset(&signature.md5, 0, sizeof(signature.md5));
1423 return WriteFile(hfile, &signature, sizeof(signature), &size, NULL);
1426 static BOOL write_reserved_data(HANDLE hfile)
1428 static const struct
1430 USHORT size;
1431 BYTE data[8];
1432 } user = { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1433 DWORD size;
1435 return WriteFile(hfile, &user, sizeof(user), &size, NULL);
1438 static BOOL write_user_data(HANDLE hfile, BYTE *data, WORD data_size)
1440 DWORD size;
1442 if (!WriteFile(hfile, &data_size, sizeof(data_size), &size, NULL))
1443 return FALSE;
1445 if (!data_size) return TRUE;
1447 return WriteFile(hfile, data, data_size, &size, NULL);
1450 static HRESULT write_triggers(TaskImpl *This, HANDLE hfile)
1452 WORD count, i, idx = 0xffff;
1453 DWORD size;
1454 HRESULT hr = S_OK;
1455 ITaskTrigger *trigger;
1457 count = This->trigger_count;
1459 /* Windows saves a .job with at least 1 trigger */
1460 if (!count)
1462 hr = ITask_CreateTrigger(&This->ITask_iface, &idx, &trigger);
1463 if (hr != S_OK) return hr;
1464 ITaskTrigger_Release(trigger);
1466 count = 1;
1469 if (WriteFile(hfile, &count, sizeof(count), &size, NULL))
1471 for (i = 0; i < count; i++)
1473 if (!WriteFile(hfile, &This->trigger[i], sizeof(This->trigger[0]), &size, NULL))
1475 hr = HRESULT_FROM_WIN32(GetLastError());
1476 break;
1480 else
1481 hr = HRESULT_FROM_WIN32(GetLastError());
1483 if (idx != 0xffff)
1484 ITask_DeleteTrigger(&This->ITask_iface, idx);
1486 return hr;
1489 static BOOL write_unicode_string(HANDLE hfile, const WCHAR *str)
1491 USHORT count;
1492 DWORD size;
1494 count = str ? (lstrlenW(str) + 1) : 0;
1495 if (!WriteFile(hfile, &count, sizeof(count), &size, NULL))
1496 return FALSE;
1498 if (!str) return TRUE;
1500 count *= sizeof(WCHAR);
1501 return WriteFile(hfile, str, count, &size, NULL);
1504 static HRESULT WINAPI MSTASK_IPersistFile_Save(IPersistFile *iface, LPCOLESTR task_name, BOOL remember)
1506 static WCHAR authorW[] = { 'W','i','n','e',0 };
1507 static WCHAR commentW[] = { 'C','r','e','a','t','e','d',' ','b','y',' ','W','i','n','e',0 };
1508 FIXDLEN_DATA fixed;
1509 WORD word, user_data_size = 0;
1510 HANDLE hfile;
1511 DWORD size, ver, disposition, try;
1512 TaskImpl *This = impl_from_IPersistFile(iface);
1513 ITask *task = &This->ITask_iface;
1514 LPWSTR appname = NULL, params = NULL, workdir = NULL, creator = NULL, comment = NULL;
1515 BYTE *user_data = NULL;
1516 HRESULT hr;
1518 TRACE("(%p, %s, %d)\n", iface, debugstr_w(task_name), remember);
1520 disposition = task_name ? CREATE_NEW : OPEN_ALWAYS;
1522 if (!task_name)
1524 task_name = This->task_name;
1525 remember = FALSE;
1528 ITask_GetComment(task, &comment);
1529 if (!comment) comment = commentW;
1530 ITask_GetCreator(task, &creator);
1531 if (!creator) creator = authorW;
1532 ITask_GetApplicationName(task, &appname);
1533 ITask_GetParameters(task, &params);
1534 ITask_GetWorkingDirectory(task, &workdir);
1535 ITask_GetWorkItemData(task, &user_data_size, &user_data);
1537 ver = GetVersion();
1538 fixed.product_version = MAKEWORD(ver >> 8, ver);
1539 fixed.file_version = 0x0001;
1540 CoCreateGuid(&fixed.uuid);
1541 fixed.name_size_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1542 fixed.trigger_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1543 fixed.trigger_offset += sizeof(USHORT); /* Application Name */
1544 if (appname)
1545 fixed.trigger_offset += (lstrlenW(appname) + 1) * sizeof(WCHAR);
1546 fixed.trigger_offset += sizeof(USHORT); /* Parameters */
1547 if (params)
1548 fixed.trigger_offset += (lstrlenW(params) + 1) * sizeof(WCHAR);
1549 fixed.trigger_offset += sizeof(USHORT); /* Working Directory */
1550 if (workdir)
1551 fixed.trigger_offset += (lstrlenW(workdir) + 1) * sizeof(WCHAR);
1552 fixed.trigger_offset += sizeof(USHORT); /* Author */
1553 if (creator)
1554 fixed.trigger_offset += (lstrlenW(creator) + 1) * sizeof(WCHAR);
1555 fixed.trigger_offset += sizeof(USHORT); /* Comment */
1556 if (comment)
1557 fixed.trigger_offset += (lstrlenW(comment) + 1) * sizeof(WCHAR);
1558 fixed.trigger_offset += sizeof(USHORT) + user_data_size; /* User Data */
1559 fixed.trigger_offset += 10; /* Reserved Data */
1561 fixed.error_retry_count = 0;
1562 fixed.error_retry_interval = 0;
1563 fixed.idle_wait = This->idle_minutes;
1564 fixed.idle_deadline = This->deadline_minutes;
1565 fixed.priority = This->priority;
1566 fixed.maximum_runtime = This->maxRunTime;
1567 fixed.exit_code = This->exit_code;
1568 if (This->status == SCHED_S_TASK_NOT_SCHEDULED && This->trigger_count)
1569 This->status = SCHED_S_TASK_HAS_NOT_RUN;
1570 fixed.status = This->status;
1571 fixed.flags = This->flags;
1572 memset(&fixed.last_runtime, 0, sizeof(fixed.last_runtime));
1574 try = 1;
1575 for (;;)
1577 hfile = CreateFileW(task_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, disposition, 0, 0);
1578 if (hfile != INVALID_HANDLE_VALUE) break;
1580 if (try++ >= 3) return HRESULT_FROM_WIN32(GetLastError());
1581 Sleep(100);
1584 if (!WriteFile(hfile, &fixed, sizeof(fixed), &size, NULL))
1586 hr = HRESULT_FROM_WIN32(GetLastError());
1587 goto failed;
1590 /* Instance Count: don't touch it in the client */
1591 if (SetFilePointer(hfile, sizeof(word), NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
1593 hr = HRESULT_FROM_WIN32(GetLastError());
1594 goto failed;
1596 /* Application Name */
1597 if (!write_unicode_string(hfile, appname))
1599 hr = HRESULT_FROM_WIN32(GetLastError());
1600 goto failed;
1602 /* Parameters */
1603 if (!write_unicode_string(hfile, params))
1605 hr = HRESULT_FROM_WIN32(GetLastError());
1606 goto failed;
1608 /* Working Directory */
1609 if (!write_unicode_string(hfile, workdir))
1611 hr = HRESULT_FROM_WIN32(GetLastError());
1612 goto failed;
1614 /* Author */
1615 if (!write_unicode_string(hfile, creator))
1617 hr = HRESULT_FROM_WIN32(GetLastError());
1618 goto failed;
1620 /* Comment */
1621 if (!write_unicode_string(hfile, comment))
1623 hr = HRESULT_FROM_WIN32(GetLastError());
1624 goto failed;
1627 /* User Data */
1628 if (!write_user_data(hfile, user_data, user_data_size))
1630 hr = HRESULT_FROM_WIN32(GetLastError());
1631 goto failed;
1634 /* Reserved Data */
1635 if (!write_reserved_data(hfile))
1637 hr = HRESULT_FROM_WIN32(GetLastError());
1638 goto failed;
1641 /* Triggers */
1642 hr = write_triggers(This, hfile);
1643 if (hr != S_OK)
1644 goto failed;
1646 /* Signature */
1647 if (!write_signature(hfile))
1649 hr = HRESULT_FROM_WIN32(GetLastError());
1650 goto failed;
1653 hr = S_OK;
1654 This->is_dirty = FALSE;
1656 failed:
1657 CoTaskMemFree(appname);
1658 CoTaskMemFree(params);
1659 CoTaskMemFree(workdir);
1660 if (creator != authorW)
1661 CoTaskMemFree(creator);
1662 if (comment != commentW)
1663 CoTaskMemFree(comment);
1664 CoTaskMemFree(user_data);
1666 CloseHandle(hfile);
1667 if (hr != S_OK)
1668 DeleteFileW(task_name);
1669 else if (remember)
1671 heap_free(This->task_name);
1672 This->task_name = heap_strdupW(task_name);
1674 return hr;
1677 static HRESULT WINAPI MSTASK_IPersistFile_SaveCompleted(
1678 IPersistFile* iface,
1679 LPCOLESTR pszFileName)
1681 FIXME("(%p, %p): stub\n", iface, pszFileName);
1682 return E_NOTIMPL;
1685 static HRESULT WINAPI MSTASK_IPersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *file_name)
1687 TaskImpl *This = impl_from_IPersistFile(iface);
1689 TRACE("(%p, %p)\n", iface, file_name);
1691 *file_name = CoTaskMemAlloc((strlenW(This->task_name) + 1) * sizeof(WCHAR));
1692 if (!*file_name) return E_OUTOFMEMORY;
1694 strcpyW(*file_name, This->task_name);
1695 return S_OK;
1698 static const ITaskVtbl MSTASK_ITaskVtbl =
1700 MSTASK_ITask_QueryInterface,
1701 MSTASK_ITask_AddRef,
1702 MSTASK_ITask_Release,
1703 MSTASK_ITask_CreateTrigger,
1704 MSTASK_ITask_DeleteTrigger,
1705 MSTASK_ITask_GetTriggerCount,
1706 MSTASK_ITask_GetTrigger,
1707 MSTASK_ITask_GetTriggerString,
1708 MSTASK_ITask_GetRunTimes,
1709 MSTASK_ITask_GetNextRunTime,
1710 MSTASK_ITask_SetIdleWait,
1711 MSTASK_ITask_GetIdleWait,
1712 MSTASK_ITask_Run,
1713 MSTASK_ITask_Terminate,
1714 MSTASK_ITask_EditWorkItem,
1715 MSTASK_ITask_GetMostRecentRunTime,
1716 MSTASK_ITask_GetStatus,
1717 MSTASK_ITask_GetExitCode,
1718 MSTASK_ITask_SetComment,
1719 MSTASK_ITask_GetComment,
1720 MSTASK_ITask_SetCreator,
1721 MSTASK_ITask_GetCreator,
1722 MSTASK_ITask_SetWorkItemData,
1723 MSTASK_ITask_GetWorkItemData,
1724 MSTASK_ITask_SetErrorRetryCount,
1725 MSTASK_ITask_GetErrorRetryCount,
1726 MSTASK_ITask_SetErrorRetryInterval,
1727 MSTASK_ITask_GetErrorRetryInterval,
1728 MSTASK_ITask_SetFlags,
1729 MSTASK_ITask_GetFlags,
1730 MSTASK_ITask_SetAccountInformation,
1731 MSTASK_ITask_GetAccountInformation,
1732 MSTASK_ITask_SetApplicationName,
1733 MSTASK_ITask_GetApplicationName,
1734 MSTASK_ITask_SetParameters,
1735 MSTASK_ITask_GetParameters,
1736 MSTASK_ITask_SetWorkingDirectory,
1737 MSTASK_ITask_GetWorkingDirectory,
1738 MSTASK_ITask_SetPriority,
1739 MSTASK_ITask_GetPriority,
1740 MSTASK_ITask_SetTaskFlags,
1741 MSTASK_ITask_GetTaskFlags,
1742 MSTASK_ITask_SetMaxRunTime,
1743 MSTASK_ITask_GetMaxRunTime
1746 static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
1748 MSTASK_IPersistFile_QueryInterface,
1749 MSTASK_IPersistFile_AddRef,
1750 MSTASK_IPersistFile_Release,
1751 MSTASK_IPersistFile_GetClassID,
1752 MSTASK_IPersistFile_IsDirty,
1753 MSTASK_IPersistFile_Load,
1754 MSTASK_IPersistFile_Save,
1755 MSTASK_IPersistFile_SaveCompleted,
1756 MSTASK_IPersistFile_GetCurFile
1759 HRESULT TaskConstructor(ITaskService *service, const WCHAR *name, ITask **task)
1761 static const WCHAR tasksW[] = { '\\','T','a','s','k','s','\\',0 };
1762 static const WCHAR jobW[] = { '.','j','o','b',0 };
1763 TaskImpl *This;
1764 WCHAR task_name[MAX_PATH];
1765 ITaskDefinition *taskdef;
1766 IActionCollection *actions;
1767 HRESULT hr;
1769 TRACE("(%s, %p)\n", debugstr_w(name), task);
1771 if (strchrW(name, '.')) return E_INVALIDARG;
1773 GetWindowsDirectoryW(task_name, MAX_PATH);
1774 lstrcatW(task_name, tasksW);
1775 lstrcatW(task_name, name);
1776 lstrcatW(task_name, jobW);
1778 hr = ITaskService_NewTask(service, 0, &taskdef);
1779 if (hr != S_OK) return hr;
1781 This = heap_alloc(sizeof(*This));
1782 if (!This)
1784 ITaskDefinition_Release(taskdef);
1785 return E_OUTOFMEMORY;
1788 This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
1789 This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
1790 This->ref = 1;
1791 This->task = taskdef;
1792 This->task_name = heap_strdupW(task_name);
1793 This->flags = 0;
1794 This->status = SCHED_S_TASK_NOT_SCHEDULED;
1795 This->exit_code = 0;
1796 This->idle_minutes = 10;
1797 This->deadline_minutes = 60;
1798 This->priority = NORMAL_PRIORITY_CLASS;
1799 This->accountName = NULL;
1800 This->trigger_count = 0;
1801 This->trigger = NULL;
1802 This->is_dirty = FALSE;
1803 This->instance_count = 0;
1805 /* Default time is 3 days = 259200000 ms */
1806 This->maxRunTime = 259200000;
1808 hr = ITaskDefinition_get_Actions(This->task, &actions);
1809 if (hr == S_OK)
1811 hr = IActionCollection_Create(actions, TASK_ACTION_EXEC, (IAction **)&This->action);
1812 IActionCollection_Release(actions);
1813 if (hr == S_OK)
1815 *task = &This->ITask_iface;
1816 InterlockedIncrement(&dll_ref);
1817 return S_OK;
1821 ITaskDefinition_Release(This->task);
1822 ITask_Release(&This->ITask_iface);
1823 return hr;