mstask: When writing triggers access the trigger data directly to avoid noise in...
[wine.git] / dlls / mstask / task.c
blob4bf916660e914d27293c547237314e025f31b4cd
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 UINT flags;
63 HRESULT status;
64 WORD idle_minutes, deadline_minutes;
65 DWORD priority, maxRunTime;
66 LPWSTR accountName;
67 DWORD trigger_count;
68 TASK_TRIGGER *trigger;
69 BOOL is_dirty;
70 USHORT instance_count;
71 } TaskImpl;
73 static inline TaskImpl *impl_from_ITask(ITask *iface)
75 return CONTAINING_RECORD(iface, TaskImpl, ITask_iface);
78 static inline TaskImpl *impl_from_IPersistFile( IPersistFile *iface )
80 return CONTAINING_RECORD(iface, TaskImpl, IPersistFile_iface);
83 static void TaskDestructor(TaskImpl *This)
85 TRACE("%p\n", This);
86 if (This->action)
87 IExecAction_Release(This->action);
88 ITaskDefinition_Release(This->task);
89 heap_free(This->task_name);
90 heap_free(This->accountName);
91 heap_free(This->trigger);
92 heap_free(This);
93 InterlockedDecrement(&dll_ref);
96 static HRESULT WINAPI MSTASK_ITask_QueryInterface(
97 ITask* iface,
98 REFIID riid,
99 void **ppvObject)
101 TaskImpl * This = impl_from_ITask(iface);
103 TRACE("IID: %s\n", debugstr_guid(riid));
104 if (ppvObject == NULL)
105 return E_POINTER;
107 if (IsEqualGUID(riid, &IID_IUnknown) ||
108 IsEqualGUID(riid, &IID_ITask))
110 *ppvObject = &This->ITask_iface;
111 ITask_AddRef(iface);
112 return S_OK;
114 else if (IsEqualGUID(riid, &IID_IPersistFile))
116 *ppvObject = &This->IPersistFile_iface;
117 ITask_AddRef(iface);
118 return S_OK;
121 WARN("Unknown interface: %s\n", debugstr_guid(riid));
122 *ppvObject = NULL;
123 return E_NOINTERFACE;
126 static ULONG WINAPI MSTASK_ITask_AddRef(
127 ITask* iface)
129 TaskImpl *This = impl_from_ITask(iface);
130 ULONG ref;
131 TRACE("\n");
132 ref = InterlockedIncrement(&This->ref);
133 return ref;
136 static ULONG WINAPI MSTASK_ITask_Release(
137 ITask* iface)
139 TaskImpl * This = impl_from_ITask(iface);
140 ULONG ref;
141 TRACE("\n");
142 ref = InterlockedDecrement(&This->ref);
143 if (ref == 0)
144 TaskDestructor(This);
145 return ref;
148 HRESULT task_set_trigger(ITask *task, WORD idx, const TASK_TRIGGER *src)
150 TaskImpl *This = impl_from_ITask(task);
151 TIME_FIELDS field_time;
152 LARGE_INTEGER sys_time;
153 TASK_TRIGGER dst;
155 TRACE("(%p, %u, %p)\n", task, idx, src);
157 if (idx >= This->trigger_count)
158 return E_FAIL;
160 /* Verify valid structure size */
161 if (src->cbTriggerSize != sizeof(*src))
162 return E_INVALIDARG;
163 dst.cbTriggerSize = src->cbTriggerSize;
165 /* Reserved field must be zero */
166 dst.Reserved1 = 0;
168 /* Verify and set valid start date and time */
169 memset(&field_time, 0, sizeof(field_time));
170 field_time.Year = src->wBeginYear;
171 field_time.Month = src->wBeginMonth;
172 field_time.Day = src->wBeginDay;
173 field_time.Hour = src->wStartHour;
174 field_time.Minute = src->wStartMinute;
175 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
176 return E_INVALIDARG;
177 dst.wBeginYear = src->wBeginYear;
178 dst.wBeginMonth = src->wBeginMonth;
179 dst.wBeginDay = src->wBeginDay;
180 dst.wStartHour = src->wStartHour;
181 dst.wStartMinute = src->wStartMinute;
183 /* Verify valid end date if TASK_TRIGGER_FLAG_HAS_END_DATE flag is set */
184 if (src->rgFlags & TASK_TRIGGER_FLAG_HAS_END_DATE)
186 memset(&field_time, 0, sizeof(field_time));
187 field_time.Year = src->wEndYear;
188 field_time.Month = src->wEndMonth;
189 field_time.Day = src->wEndDay;
190 if (!RtlTimeFieldsToTime(&field_time, &sys_time))
191 return E_INVALIDARG;
194 /* Set valid end date independent of TASK_TRIGGER_FLAG_HAS_END_DATE flag */
195 dst.wEndYear = src->wEndYear;
196 dst.wEndMonth = src->wEndMonth;
197 dst.wEndDay = src->wEndDay;
199 /* Verify duration and interval pair */
200 if (src->MinutesDuration <= src->MinutesInterval && src->MinutesInterval > 0)
201 return E_INVALIDARG;
202 dst.MinutesDuration = src->MinutesDuration;
203 dst.MinutesInterval = src->MinutesInterval;
205 /* Copy over flags */
206 dst.rgFlags = src->rgFlags;
208 /* Set TriggerType dependent fields of Type union */
209 dst.TriggerType = src->TriggerType;
210 switch (src->TriggerType)
212 case TASK_TIME_TRIGGER_DAILY:
213 dst.Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
214 break;
215 case TASK_TIME_TRIGGER_WEEKLY:
216 dst.Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
217 dst.Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
218 break;
219 case TASK_TIME_TRIGGER_MONTHLYDATE:
220 dst.Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
221 dst.Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
222 break;
223 case TASK_TIME_TRIGGER_MONTHLYDOW:
224 dst.Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
225 dst.Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
226 dst.Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
227 break;
228 case TASK_TIME_TRIGGER_ONCE:
229 case TASK_EVENT_TRIGGER_ON_IDLE:
230 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
231 case TASK_EVENT_TRIGGER_AT_LOGON:
232 default:
233 dst.Type = src->Type;
234 break;
237 /* Reserved field must be zero */
238 dst.Reserved2 = 0;
240 /* wRandomMinutesInterval not currently used and is initialized to zero */
241 dst.wRandomMinutesInterval = 0;
243 This->trigger[idx] = dst;
245 return S_OK;
248 HRESULT task_get_trigger(ITask *task, WORD idx, TASK_TRIGGER *dst)
250 TaskImpl *This = impl_from_ITask(task);
251 TASK_TRIGGER *src;
253 TRACE("(%p, %u, %p)\n", task, idx, dst);
255 if (idx >= This->trigger_count)
256 return SCHED_E_TRIGGER_NOT_FOUND;
258 src = &This->trigger[idx];
260 /* Native implementation doesn't verify equivalent cbTriggerSize fields */
262 /* Copy relevant fields of the structure */
263 dst->cbTriggerSize = src->cbTriggerSize;
264 dst->Reserved1 = 0;
265 dst->wBeginYear = src->wBeginYear;
266 dst->wBeginMonth = src->wBeginMonth;
267 dst->wBeginDay = src->wBeginDay;
268 dst->wEndYear = src->wEndYear;
269 dst->wEndMonth = src->wEndMonth;
270 dst->wEndDay = src->wEndDay;
271 dst->wStartHour = src->wStartHour;
272 dst->wStartMinute = src->wStartMinute;
273 dst->MinutesDuration = src->MinutesDuration;
274 dst->MinutesInterval = src->MinutesInterval;
275 dst->rgFlags = src->rgFlags;
276 dst->TriggerType = src->TriggerType;
277 switch (src->TriggerType)
279 case TASK_TIME_TRIGGER_DAILY:
280 dst->Type.Daily.DaysInterval = src->Type.Daily.DaysInterval;
281 break;
282 case TASK_TIME_TRIGGER_WEEKLY:
283 dst->Type.Weekly.WeeksInterval = src->Type.Weekly.WeeksInterval;
284 dst->Type.Weekly.rgfDaysOfTheWeek = src->Type.Weekly.rgfDaysOfTheWeek;
285 break;
286 case TASK_TIME_TRIGGER_MONTHLYDATE:
287 dst->Type.MonthlyDate.rgfDays = src->Type.MonthlyDate.rgfDays;
288 dst->Type.MonthlyDate.rgfMonths = src->Type.MonthlyDate.rgfMonths;
289 break;
290 case TASK_TIME_TRIGGER_MONTHLYDOW:
291 dst->Type.MonthlyDOW.wWhichWeek = src->Type.MonthlyDOW.wWhichWeek;
292 dst->Type.MonthlyDOW.rgfDaysOfTheWeek = src->Type.MonthlyDOW.rgfDaysOfTheWeek;
293 dst->Type.MonthlyDOW.rgfMonths = src->Type.MonthlyDOW.rgfMonths;
294 break;
295 case TASK_TIME_TRIGGER_ONCE:
296 case TASK_EVENT_TRIGGER_ON_IDLE:
297 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART:
298 case TASK_EVENT_TRIGGER_AT_LOGON:
299 default:
300 break;
302 dst->Reserved2 = 0;
303 dst->wRandomMinutesInterval = 0;
305 return S_OK;
308 static HRESULT WINAPI MSTASK_ITask_CreateTrigger(ITask *iface, WORD *idx, ITaskTrigger **task_trigger)
310 TaskImpl *This = impl_from_ITask(iface);
311 TASK_TRIGGER *new_trigger;
312 SYSTEMTIME time;
313 HRESULT hr;
315 TRACE("(%p, %p, %p)\n", iface, idx, task_trigger);
317 hr = TaskTriggerConstructor(iface, This->trigger_count, task_trigger);
318 if (hr != S_OK) return hr;
320 if (This->trigger)
321 new_trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * (This->trigger_count + 1));
322 else
323 new_trigger = heap_alloc(sizeof(This->trigger[0]));
324 if (!new_trigger)
326 ITaskTrigger_Release(*task_trigger);
327 return E_OUTOFMEMORY;
330 This->trigger = new_trigger;
332 new_trigger = &This->trigger[This->trigger_count];
334 /* Most fields default to zero. Initialize other fields to default values. */
335 memset(new_trigger, 0, sizeof(*new_trigger));
336 GetLocalTime(&time);
337 new_trigger->cbTriggerSize = sizeof(*new_trigger);
338 new_trigger->wBeginYear = time.wYear;
339 new_trigger->wBeginMonth = time.wMonth;
340 new_trigger->wBeginDay = time.wDay;
341 new_trigger->wStartHour = time.wHour;
342 new_trigger->wStartMinute = time.wMinute;
343 new_trigger->rgFlags = TASK_TRIGGER_FLAG_DISABLED;
344 new_trigger->TriggerType = TASK_TIME_TRIGGER_DAILY,
345 new_trigger->Type.Daily.DaysInterval = 1;
347 *idx = This->trigger_count++;
349 return hr;
352 static HRESULT WINAPI MSTASK_ITask_DeleteTrigger(ITask *iface, WORD idx)
354 TaskImpl *This = impl_from_ITask(iface);
356 TRACE("(%p, %u)\n", iface, idx);
358 if (idx >= This->trigger_count)
359 return SCHED_E_TRIGGER_NOT_FOUND;
361 This->trigger_count--;
362 memmove(&This->trigger[idx], &This->trigger[idx + 1], (This->trigger_count - idx) * sizeof(This->trigger[0]));
363 /* this shouldn't fail in practice since we're shrinking the memory block */
364 This->trigger = heap_realloc(This->trigger, sizeof(This->trigger[0]) * This->trigger_count);
366 return S_OK;
369 static HRESULT WINAPI MSTASK_ITask_GetTriggerCount(ITask *iface, WORD *count)
371 TaskImpl *This = impl_from_ITask(iface);
373 TRACE("(%p, %p)\n", iface, count);
375 *count = This->trigger_count;
376 return S_OK;
379 static HRESULT WINAPI MSTASK_ITask_GetTrigger(ITask *iface, WORD idx, ITaskTrigger **trigger)
381 TaskImpl *This = impl_from_ITask(iface);
383 TRACE("(%p, %u, %p)\n", iface, idx, trigger);
385 if (idx >= This->trigger_count)
386 return SCHED_E_TRIGGER_NOT_FOUND;
388 return TaskTriggerConstructor(iface, idx, trigger);
391 static HRESULT WINAPI MSTASK_ITask_GetTriggerString(
392 ITask* iface,
393 WORD iTrigger,
394 LPWSTR *ppwszTrigger)
396 FIXME("(%p, %d, %p): stub\n", iface, iTrigger, ppwszTrigger);
397 return E_NOTIMPL;
400 static HRESULT WINAPI MSTASK_ITask_GetRunTimes(
401 ITask* iface,
402 const LPSYSTEMTIME pstBegin,
403 const LPSYSTEMTIME pstEnd,
404 WORD *pCount,
405 LPSYSTEMTIME *rgstTaskTimes)
407 FIXME("(%p, %p, %p, %p, %p): stub\n", iface, pstBegin, pstEnd, pCount,
408 rgstTaskTimes);
409 return E_NOTIMPL;
412 static HRESULT WINAPI MSTASK_ITask_GetNextRunTime(ITask *iface, SYSTEMTIME *st)
414 FIXME("(%p, %p): stub\n", iface, st);
416 memset(st, 0, sizeof(*st));
417 return SCHED_S_TASK_NO_VALID_TRIGGERS;
420 static HRESULT WINAPI MSTASK_ITask_SetIdleWait(
421 ITask* iface,
422 WORD wIdleMinutes,
423 WORD wDeadlineMinutes)
425 FIXME("(%p, %d, %d): stub\n", iface, wIdleMinutes, wDeadlineMinutes);
426 return E_NOTIMPL;
429 static HRESULT WINAPI MSTASK_ITask_GetIdleWait(ITask *iface, WORD *idle_minutes, WORD *deadline_minutes)
431 TaskImpl *This = impl_from_ITask(iface);
433 TRACE("(%p, %p, %p): stub\n", iface, idle_minutes, deadline_minutes);
435 *idle_minutes = This->idle_minutes;
436 *deadline_minutes = This->deadline_minutes;
437 return S_OK;
440 static HRESULT WINAPI MSTASK_ITask_Run(ITask *iface)
442 TaskImpl *This = impl_from_ITask(iface);
444 TRACE("(%p)\n", iface);
446 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
447 return SCHED_E_TASK_NOT_READY;
449 This->flags |= 0x04000000;
450 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
453 static HRESULT WINAPI MSTASK_ITask_Terminate(ITask *iface)
455 TaskImpl *This = impl_from_ITask(iface);
457 TRACE("(%p)\n", iface);
459 if (!This->instance_count)
460 return SCHED_E_TASK_NOT_RUNNING;
462 This->flags |= 0x08000000;
463 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
466 static HRESULT WINAPI MSTASK_ITask_EditWorkItem(
467 ITask* iface,
468 HWND hParent,
469 DWORD dwReserved)
471 FIXME("(%p, %p, %d): stub\n", iface, hParent, dwReserved);
472 return E_NOTIMPL;
475 static HRESULT WINAPI MSTASK_ITask_GetMostRecentRunTime(ITask *iface, SYSTEMTIME *st)
477 FIXME("(%p, %p): stub\n", iface, st);
479 memset(st, 0, sizeof(*st));
480 return SCHED_S_TASK_HAS_NOT_RUN;
483 static HRESULT WINAPI MSTASK_ITask_GetStatus(ITask *iface, HRESULT *status)
485 TaskImpl *This = impl_from_ITask(iface);
487 TRACE("(%p, %p)\n", iface, status);
489 *status = This->instance_count ? SCHED_S_TASK_RUNNING : This->status;
490 return S_OK;
493 static HRESULT WINAPI MSTASK_ITask_GetExitCode(ITask *iface, DWORD *exit_code)
495 FIXME("(%p, %p): stub\n", iface, exit_code);
497 *exit_code = 0;
498 return SCHED_S_TASK_HAS_NOT_RUN;
501 static HRESULT WINAPI MSTASK_ITask_SetComment(ITask *iface, LPCWSTR comment)
503 TaskImpl *This = impl_from_ITask(iface);
504 IRegistrationInfo *info;
505 HRESULT hr;
507 TRACE("(%p, %s)\n", iface, debugstr_w(comment));
509 if (!comment || !comment[0])
510 comment = NULL;
512 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
513 if (hr == S_OK)
515 hr = IRegistrationInfo_put_Description(info, (BSTR)comment);
516 IRegistrationInfo_Release(info);
517 This->is_dirty = TRUE;
519 return hr;
522 static HRESULT WINAPI MSTASK_ITask_GetComment(ITask *iface, LPWSTR *comment)
524 TaskImpl *This = impl_from_ITask(iface);
525 IRegistrationInfo *info;
526 HRESULT hr;
527 BSTR description;
528 DWORD len;
530 TRACE("(%p, %p)\n", iface, comment);
532 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
533 if (hr != S_OK) return hr;
535 hr = IRegistrationInfo_get_Description(info, &description);
536 if (hr == S_OK)
538 len = description ? lstrlenW(description) + 1 : 1;
539 *comment = CoTaskMemAlloc(len * sizeof(WCHAR));
540 if (*comment)
542 if (!description)
543 *comment[0] = 0;
544 else
545 lstrcpyW(*comment, description);
546 hr = S_OK;
548 else
549 hr = E_OUTOFMEMORY;
551 SysFreeString(description);
554 IRegistrationInfo_Release(info);
555 return hr;
558 static HRESULT WINAPI MSTASK_ITask_SetCreator(ITask *iface, LPCWSTR creator)
560 TaskImpl *This = impl_from_ITask(iface);
561 IRegistrationInfo *info;
562 HRESULT hr;
564 TRACE("(%p, %s)\n", iface, debugstr_w(creator));
566 if (!creator || !creator[0])
567 creator = NULL;
569 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
570 if (hr == S_OK)
572 hr = IRegistrationInfo_put_Author(info, (BSTR)creator);
573 IRegistrationInfo_Release(info);
574 This->is_dirty = TRUE;
576 return hr;
579 static HRESULT WINAPI MSTASK_ITask_GetCreator(ITask *iface, LPWSTR *creator)
581 TaskImpl *This = impl_from_ITask(iface);
582 IRegistrationInfo *info;
583 HRESULT hr;
584 BSTR author;
585 DWORD len;
587 TRACE("(%p, %p)\n", iface, creator);
589 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
590 if (hr != S_OK) return hr;
592 hr = IRegistrationInfo_get_Author(info, &author);
593 if (hr == S_OK)
595 len = author ? lstrlenW(author) + 1 : 1;
596 *creator = CoTaskMemAlloc(len * sizeof(WCHAR));
597 if (*creator)
599 if (!author)
600 *creator[0] = 0;
601 else
602 lstrcpyW(*creator, author);
603 hr = S_OK;
605 else
606 hr = E_OUTOFMEMORY;
608 SysFreeString(author);
611 IRegistrationInfo_Release(info);
612 return hr;
615 static HRESULT WINAPI MSTASK_ITask_SetWorkItemData(
616 ITask* iface,
617 WORD cBytes,
618 BYTE rgbData[])
620 FIXME("(%p, %d, %p): stub\n", iface, cBytes, rgbData);
621 return E_NOTIMPL;
624 static HRESULT WINAPI MSTASK_ITask_GetWorkItemData(
625 ITask* iface,
626 WORD *pcBytes,
627 BYTE **ppBytes)
629 FIXME("(%p, %p, %p): stub\n", iface, pcBytes, ppBytes);
630 return E_NOTIMPL;
633 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryCount(
634 ITask* iface,
635 WORD wRetryCount)
637 FIXME("(%p, %d): stub\n", iface, wRetryCount);
638 return E_NOTIMPL;
641 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryCount(ITask *iface, WORD *count)
643 TRACE("(%p, %p)\n", iface, count);
644 return E_NOTIMPL;
647 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryInterval(
648 ITask* iface,
649 WORD wRetryInterval)
651 FIXME("(%p, %d): stub\n", iface, wRetryInterval);
652 return E_NOTIMPL;
655 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryInterval(ITask *iface, WORD *interval)
657 TRACE("(%p, %p)\n", iface, interval);
658 return E_NOTIMPL;
661 static HRESULT WINAPI MSTASK_ITask_SetFlags(ITask *iface, DWORD flags)
663 TaskImpl *This = impl_from_ITask(iface);
665 TRACE("(%p, 0x%08x)\n", iface, flags);
666 This->flags &= 0xffff8000;
667 This->flags |= flags & 0x7fff;
668 This->is_dirty = TRUE;
669 return S_OK;
672 static HRESULT WINAPI MSTASK_ITask_GetFlags(ITask *iface, DWORD *flags)
674 TaskImpl *This = impl_from_ITask(iface);
676 TRACE("(%p, %p)\n", iface, flags);
677 *flags = LOWORD(This->flags);
678 return S_OK;
681 static HRESULT WINAPI MSTASK_ITask_SetAccountInformation(
682 ITask* iface,
683 LPCWSTR pwszAccountName,
684 LPCWSTR pwszPassword)
686 DWORD n;
687 TaskImpl *This = impl_from_ITask(iface);
688 LPWSTR tmp_account_name;
690 TRACE("(%p, %s, %s): partial stub\n", iface, debugstr_w(pwszAccountName),
691 debugstr_w(pwszPassword));
693 if (pwszPassword)
694 FIXME("Partial stub ignores passwords\n");
696 n = (lstrlenW(pwszAccountName) + 1);
697 tmp_account_name = heap_alloc(n * sizeof(WCHAR));
698 if (!tmp_account_name)
699 return E_OUTOFMEMORY;
700 lstrcpyW(tmp_account_name, pwszAccountName);
701 heap_free(This->accountName);
702 This->accountName = tmp_account_name;
703 This->is_dirty = TRUE;
704 return S_OK;
707 static HRESULT WINAPI MSTASK_ITask_GetAccountInformation(
708 ITask* iface,
709 LPWSTR *ppwszAccountName)
711 DWORD n;
712 TaskImpl *This = impl_from_ITask(iface);
714 TRACE("(%p, %p): partial stub\n", iface, ppwszAccountName);
716 /* This implements the WinXP behavior when accountName has not yet
717 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
718 if (!This->accountName)
719 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
721 n = (lstrlenW(This->accountName) + 1);
722 *ppwszAccountName = CoTaskMemAlloc(n * sizeof(WCHAR));
723 if (!*ppwszAccountName)
724 return E_OUTOFMEMORY;
725 lstrcpyW(*ppwszAccountName, This->accountName);
726 return S_OK;
729 static HRESULT WINAPI MSTASK_ITask_SetApplicationName(ITask *iface, LPCWSTR appname)
731 TaskImpl *This = impl_from_ITask(iface);
732 DWORD len;
733 HRESULT hr;
735 TRACE("(%p, %s)\n", iface, debugstr_w(appname));
737 /* Empty application name */
738 if (!appname || !appname[0])
739 return IExecAction_put_Path(This->action, NULL);
741 /* Attempt to set pwszApplicationName to a path resolved application name */
742 len = SearchPathW(NULL, appname, NULL, 0, NULL, NULL);
743 if (len)
745 LPWSTR tmp_name;
747 tmp_name = heap_alloc(len * sizeof(WCHAR));
748 if (!tmp_name)
749 return E_OUTOFMEMORY;
750 len = SearchPathW(NULL, appname, NULL, len, tmp_name, NULL);
751 if (len)
753 hr = IExecAction_put_Path(This->action, tmp_name);
754 if (hr == S_OK) This->is_dirty = TRUE;
756 else
757 hr = HRESULT_FROM_WIN32(GetLastError());
759 heap_free(tmp_name);
760 return hr;
763 /* If unable to path resolve name, simply set to appname */
764 hr = IExecAction_put_Path(This->action, (BSTR)appname);
765 if (hr == S_OK) This->is_dirty = TRUE;
766 return hr;
769 static HRESULT WINAPI MSTASK_ITask_GetApplicationName(ITask *iface, LPWSTR *appname)
771 TaskImpl *This = impl_from_ITask(iface);
772 HRESULT hr;
773 BSTR path;
774 DWORD len;
776 TRACE("(%p, %p)\n", iface, appname);
778 hr = IExecAction_get_Path(This->action, &path);
779 if (hr != S_OK) return hr;
781 len = path ? lstrlenW(path) + 1 : 1;
782 *appname = CoTaskMemAlloc(len * sizeof(WCHAR));
783 if (*appname)
785 if (!path)
786 *appname[0] = 0;
787 else
788 lstrcpyW(*appname, path);
789 hr = S_OK;
791 else
792 hr = E_OUTOFMEMORY;
794 SysFreeString(path);
795 return hr;
798 static HRESULT WINAPI MSTASK_ITask_SetParameters(ITask *iface, LPCWSTR params)
800 TaskImpl *This = impl_from_ITask(iface);
801 HRESULT hr;
803 TRACE("(%p, %s)\n", iface, debugstr_w(params));
805 /* Empty parameter list */
806 if (!params || !params[0])
807 params = NULL;
809 hr = IExecAction_put_Arguments(This->action, (BSTR)params);
810 if (hr == S_OK) This->is_dirty = TRUE;
811 return hr;
814 static HRESULT WINAPI MSTASK_ITask_GetParameters(ITask *iface, LPWSTR *params)
816 TaskImpl *This = impl_from_ITask(iface);
817 HRESULT hr;
818 BSTR args;
819 DWORD len;
821 TRACE("(%p, %p)\n", iface, params);
823 hr = IExecAction_get_Arguments(This->action, &args);
824 if (hr != S_OK) return hr;
826 len = args ? lstrlenW(args) + 1 : 1;
827 *params = CoTaskMemAlloc(len * sizeof(WCHAR));
828 if (*params)
830 if (!args)
831 *params[0] = 0;
832 else
833 lstrcpyW(*params, args);
834 hr = S_OK;
836 else
837 hr = E_OUTOFMEMORY;
839 SysFreeString(args);
840 return hr;
843 static HRESULT WINAPI MSTASK_ITask_SetWorkingDirectory(ITask * iface, LPCWSTR workdir)
845 TaskImpl *This = impl_from_ITask(iface);
846 HRESULT hr;
848 TRACE("(%p, %s)\n", iface, debugstr_w(workdir));
850 if (!workdir || !workdir[0])
851 workdir = NULL;
853 hr = IExecAction_put_WorkingDirectory(This->action, (BSTR)workdir);
854 if (hr == S_OK) This->is_dirty = TRUE;
855 return hr;
858 static HRESULT WINAPI MSTASK_ITask_GetWorkingDirectory(ITask *iface, LPWSTR *workdir)
860 TaskImpl *This = impl_from_ITask(iface);
861 HRESULT hr;
862 BSTR dir;
863 DWORD len;
865 TRACE("(%p, %p)\n", iface, workdir);
867 hr = IExecAction_get_WorkingDirectory(This->action, &dir);
868 if (hr != S_OK) return hr;
870 len = dir ? lstrlenW(dir) + 1 : 1;
871 *workdir = CoTaskMemAlloc(len * sizeof(WCHAR));
872 if (*workdir)
874 if (!dir)
875 *workdir[0] = 0;
876 else
877 lstrcpyW(*workdir, dir);
878 hr = S_OK;
880 else
881 hr = E_OUTOFMEMORY;
883 SysFreeString(dir);
884 return hr;
887 static HRESULT WINAPI MSTASK_ITask_SetPriority(
888 ITask* iface,
889 DWORD dwPriority)
891 FIXME("(%p, 0x%08x): stub\n", iface, dwPriority);
892 return E_NOTIMPL;
895 static HRESULT WINAPI MSTASK_ITask_GetPriority(ITask *iface, DWORD *priority)
897 TaskImpl *This = impl_from_ITask(iface);
899 TRACE("(%p, %p)\n", iface, priority);
901 *priority = This->priority;
902 return S_OK;
905 static HRESULT WINAPI MSTASK_ITask_SetTaskFlags(
906 ITask* iface,
907 DWORD dwFlags)
909 FIXME("(%p, 0x%08x): stub\n", iface, dwFlags);
910 return E_NOTIMPL;
913 static HRESULT WINAPI MSTASK_ITask_GetTaskFlags(ITask *iface, DWORD *flags)
915 FIXME("(%p, %p): stub\n", iface, flags);
916 *flags = 0;
917 return S_OK;
920 static HRESULT WINAPI MSTASK_ITask_SetMaxRunTime(
921 ITask* iface,
922 DWORD dwMaxRunTime)
924 TaskImpl *This = impl_from_ITask(iface);
926 TRACE("(%p, %d)\n", iface, dwMaxRunTime);
928 This->maxRunTime = dwMaxRunTime;
929 This->is_dirty = TRUE;
930 return S_OK;
933 static HRESULT WINAPI MSTASK_ITask_GetMaxRunTime(
934 ITask* iface,
935 DWORD *pdwMaxRunTime)
937 TaskImpl *This = impl_from_ITask(iface);
939 TRACE("(%p, %p)\n", iface, pdwMaxRunTime);
941 *pdwMaxRunTime = This->maxRunTime;
942 return S_OK;
945 static HRESULT WINAPI MSTASK_IPersistFile_QueryInterface(
946 IPersistFile* iface,
947 REFIID riid,
948 void **ppvObject)
950 TaskImpl *This = impl_from_IPersistFile(iface);
951 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
952 return ITask_QueryInterface(&This->ITask_iface, riid, ppvObject);
955 static ULONG WINAPI MSTASK_IPersistFile_AddRef(
956 IPersistFile* iface)
958 TaskImpl *This = impl_from_IPersistFile(iface);
959 return ITask_AddRef(&This->ITask_iface);
962 static ULONG WINAPI MSTASK_IPersistFile_Release(
963 IPersistFile* iface)
965 TaskImpl *This = impl_from_IPersistFile(iface);
966 return ITask_Release(&This->ITask_iface);
969 static HRESULT WINAPI MSTASK_IPersistFile_GetClassID(IPersistFile *iface, CLSID *clsid)
971 TRACE("(%p, %p)\n", iface, clsid);
973 *clsid = CLSID_CTask;
974 return S_OK;
977 static HRESULT WINAPI MSTASK_IPersistFile_IsDirty(IPersistFile *iface)
979 TaskImpl *This = impl_from_IPersistFile(iface);
980 TRACE("(%p)\n", iface);
981 return This->is_dirty ? S_OK : S_FALSE;
984 static DWORD load_unicode_strings(ITask *task, BYTE *data, DWORD limit)
986 DWORD i, data_size = 0;
987 USHORT len;
989 for (i = 0; i < 5; i++)
991 if (limit < sizeof(USHORT))
993 TRACE("invalid string %u offset\n", i);
994 break;
997 len = *(USHORT *)data;
998 data += sizeof(USHORT);
999 data_size += sizeof(USHORT);
1000 limit -= sizeof(USHORT);
1001 if (limit < len * sizeof(WCHAR))
1003 TRACE("invalid string %u size\n", i);
1004 break;
1007 TRACE("string %u: %s\n", i, wine_dbgstr_wn((const WCHAR *)data, len));
1009 switch (i)
1011 case 0:
1012 ITask_SetApplicationName(task, (const WCHAR *)data);
1013 break;
1014 case 1:
1015 ITask_SetParameters(task, (const WCHAR *)data);
1016 break;
1017 case 2:
1018 ITask_SetWorkingDirectory(task, (const WCHAR *)data);
1019 break;
1020 case 3:
1021 ITask_SetCreator(task, (const WCHAR *)data);
1022 break;
1023 case 4:
1024 ITask_SetComment(task, (const WCHAR *)data);
1025 break;
1026 default:
1027 break;
1030 data += len * sizeof(WCHAR);
1031 data_size += len * sizeof(WCHAR);
1034 return data_size;
1037 static HRESULT load_job_data(TaskImpl *This, BYTE *data, DWORD size)
1039 ITask *task = &This->ITask_iface;
1040 HRESULT hr;
1041 const FIXDLEN_DATA *fixed;
1042 const SYSTEMTIME *st;
1043 DWORD unicode_strings_size, data_size, triggers_size;
1044 USHORT trigger_count, i;
1045 const USHORT *signature;
1046 TASK_TRIGGER *task_trigger;
1048 if (size < sizeof(*fixed))
1050 TRACE("no space for FIXDLEN_DATA\n");
1051 return SCHED_E_INVALID_TASK;
1054 fixed = (const FIXDLEN_DATA *)data;
1056 TRACE("product_version %04x\n", fixed->product_version);
1057 TRACE("file_version %04x\n", fixed->file_version);
1058 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed->uuid));
1060 if (fixed->file_version != 0x0001)
1061 return SCHED_E_INVALID_TASK;
1063 TRACE("name_size_offset %04x\n", fixed->name_size_offset);
1064 TRACE("trigger_offset %04x\n", fixed->trigger_offset);
1065 TRACE("error_retry_count %u\n", fixed->error_retry_count);
1066 TRACE("error_retry_interval %u\n", fixed->error_retry_interval);
1067 TRACE("idle_deadline %u\n", fixed->idle_deadline);
1068 This->deadline_minutes = fixed->idle_deadline;
1069 TRACE("idle_wait %u\n", fixed->idle_wait);
1070 This->idle_minutes = fixed->idle_wait;
1071 TRACE("priority %08x\n", fixed->priority);
1072 This->priority = fixed->priority;
1073 TRACE("maximum_runtime %u\n", fixed->maximum_runtime);
1074 This->maxRunTime = fixed->maximum_runtime;
1075 TRACE("exit_code %#x\n", fixed->exit_code);
1076 TRACE("status %08x\n", fixed->status);
1077 This->status = fixed->status;
1078 TRACE("flags %08x\n", fixed->flags);
1079 This->flags = fixed->flags;
1080 st = &fixed->last_runtime;
1081 TRACE("last_runtime %d/%d/%d wday %d %d:%d:%d.%03d\n",
1082 st->wDay, st->wMonth, st->wYear, st->wDayOfWeek,
1083 st->wHour, st->wMinute, st->wSecond, st->wMilliseconds);
1085 /* Instance Count */
1086 if (size < sizeof(*fixed) + sizeof(USHORT))
1088 TRACE("no space for instance count\n");
1089 return SCHED_E_INVALID_TASK;
1092 This->instance_count = *(const USHORT *)(data + sizeof(*fixed));
1093 TRACE("instance count %u\n", This->instance_count);
1095 if (fixed->name_size_offset + sizeof(USHORT) < size)
1096 unicode_strings_size = load_unicode_strings(task, data + fixed->name_size_offset, size - fixed->name_size_offset);
1097 else
1099 TRACE("invalid name_size_offset\n");
1100 return SCHED_E_INVALID_TASK;
1102 TRACE("unicode strings end at %#x\n", fixed->name_size_offset + unicode_strings_size);
1104 if (size < fixed->trigger_offset + sizeof(USHORT))
1106 TRACE("no space for triggers count\n");
1107 return SCHED_E_INVALID_TASK;
1109 trigger_count = *(const USHORT *)(data + fixed->trigger_offset);
1110 TRACE("trigger_count %u\n", trigger_count);
1111 triggers_size = size - fixed->trigger_offset - sizeof(USHORT);
1112 TRACE("triggers_size %u\n", triggers_size);
1113 task_trigger = (TASK_TRIGGER *)(data + fixed->trigger_offset + sizeof(USHORT));
1115 data += fixed->name_size_offset + unicode_strings_size;
1116 size -= fixed->name_size_offset + unicode_strings_size;
1118 /* User Data */
1119 if (size < sizeof(USHORT))
1121 TRACE("no space for user data size\n");
1122 return SCHED_E_INVALID_TASK;
1125 data_size = *(const USHORT *)data;
1126 if (size < sizeof(USHORT) + data_size)
1128 TRACE("no space for user data\n");
1129 return SCHED_E_INVALID_TASK;
1131 TRACE("User Data size %#x\n", data_size);
1132 ITask_SetWorkItemData(task, data_size, data + sizeof(USHORT));
1134 size -= sizeof(USHORT) + data_size;
1135 data += sizeof(USHORT) + data_size;
1137 /* Reserved Data */
1138 if (size < sizeof(USHORT))
1140 TRACE("no space for reserved data size\n");
1141 return SCHED_E_INVALID_TASK;
1144 data_size = *(const USHORT *)data;
1145 if (size < sizeof(USHORT) + data_size)
1147 TRACE("no space for reserved data\n");
1148 return SCHED_E_INVALID_TASK;
1150 TRACE("Reserved Data size %#x\n", data_size);
1152 size -= sizeof(USHORT) + data_size;
1153 data += sizeof(USHORT) + data_size;
1155 /* Trigger Data */
1156 TRACE("trigger_offset %04x, triggers end at %04x\n", fixed->trigger_offset,
1157 (DWORD)(fixed->trigger_offset + sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER)));
1159 task_trigger = (TASK_TRIGGER *)(data + sizeof(USHORT));
1161 if (trigger_count * sizeof(TASK_TRIGGER) > triggers_size)
1163 TRACE("no space for triggers data\n");
1164 return SCHED_E_INVALID_TASK;
1167 This->trigger_count = 0;
1169 for (i = 0; i < trigger_count; i++)
1171 ITaskTrigger *trigger;
1172 WORD idx;
1174 hr = ITask_CreateTrigger(task, &idx, &trigger);
1175 if (hr != S_OK) return hr;
1177 hr = ITaskTrigger_SetTrigger(trigger, &task_trigger[i]);
1178 ITaskTrigger_Release(trigger);
1179 if (hr != S_OK)
1181 ITask_DeleteTrigger(task, idx);
1182 return hr;
1186 size -= sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1187 data += sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1189 if (size < 2 * sizeof(USHORT) + 64)
1191 TRACE("no space for signature\n");
1192 return S_OK; /* signature is optional */
1195 signature = (const USHORT *)data;
1196 TRACE("signature version %04x, client version %04x\n", signature[0], signature[1]);
1198 return S_OK;
1201 static HRESULT WINAPI MSTASK_IPersistFile_Load(IPersistFile *iface, LPCOLESTR file_name, DWORD mode)
1203 TaskImpl *This = impl_from_IPersistFile(iface);
1204 HRESULT hr;
1205 HANDLE file, mapping;
1206 DWORD access, sharing, size;
1207 void *data;
1209 TRACE("(%p, %s, 0x%08x)\n", iface, debugstr_w(file_name), mode);
1211 switch (mode & 0x000f)
1213 default:
1214 case STGM_READ:
1215 access = GENERIC_READ;
1216 break;
1217 case STGM_WRITE:
1218 case STGM_READWRITE:
1219 access = GENERIC_READ | GENERIC_WRITE;
1220 break;
1223 switch (mode & 0x00f0)
1225 default:
1226 case STGM_SHARE_DENY_NONE:
1227 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1228 break;
1229 case STGM_SHARE_DENY_READ:
1230 sharing = FILE_SHARE_WRITE;
1231 break;
1232 case STGM_SHARE_DENY_WRITE:
1233 sharing = FILE_SHARE_READ;
1234 break;
1235 case STGM_SHARE_EXCLUSIVE:
1236 sharing = 0;
1237 break;
1240 file = CreateFileW(file_name, access, sharing, NULL, OPEN_EXISTING, 0, 0);
1241 if (file == INVALID_HANDLE_VALUE)
1243 TRACE("Failed to open %s, error %u\n", debugstr_w(file_name), GetLastError());
1244 return HRESULT_FROM_WIN32(GetLastError());
1247 size = GetFileSize(file, NULL);
1249 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, 0);
1250 if (!mapping)
1252 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name), GetLastError());
1253 CloseHandle(file);
1254 return HRESULT_FROM_WIN32(GetLastError());
1257 data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1258 if (data)
1260 hr = load_job_data(This, data, size);
1261 if (hr == S_OK) This->is_dirty = FALSE;
1262 UnmapViewOfFile(data);
1264 else
1265 hr = HRESULT_FROM_WIN32(GetLastError());
1267 CloseHandle(mapping);
1268 CloseHandle(file);
1270 return hr;
1273 static BOOL write_signature(HANDLE hfile)
1275 struct
1277 USHORT SignatureVersion;
1278 USHORT ClientVersion;
1279 BYTE md5[64];
1280 } signature;
1281 DWORD size;
1283 signature.SignatureVersion = 0x0001;
1284 signature.ClientVersion = 0x0001;
1285 memset(&signature.md5, 0, sizeof(signature.md5));
1287 return WriteFile(hfile, &signature, sizeof(signature), &size, NULL);
1290 static BOOL write_reserved_data(HANDLE hfile)
1292 static const struct
1294 USHORT size;
1295 BYTE data[8];
1296 } user = { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1297 DWORD size;
1299 return WriteFile(hfile, &user, sizeof(user), &size, NULL);
1302 static BOOL write_user_data(HANDLE hfile, BYTE *data, WORD data_size)
1304 DWORD size;
1306 if (!WriteFile(hfile, &data_size, sizeof(data_size), &size, NULL))
1307 return FALSE;
1309 if (!data_size) return TRUE;
1311 return WriteFile(hfile, data, data_size, &size, NULL);
1314 static HRESULT write_triggers(TaskImpl *This, HANDLE hfile)
1316 WORD count, i, idx = 0xffff;
1317 DWORD size;
1318 HRESULT hr = S_OK;
1319 ITaskTrigger *trigger;
1321 count = This->trigger_count;
1323 /* Windows saves a .job with at least 1 trigger */
1324 if (!count)
1326 hr = ITask_CreateTrigger(&This->ITask_iface, &idx, &trigger);
1327 if (hr != S_OK) return hr;
1328 ITaskTrigger_Release(trigger);
1330 count = 1;
1333 if (WriteFile(hfile, &count, sizeof(count), &size, NULL))
1335 for (i = 0; i < count; i++)
1337 if (!WriteFile(hfile, &This->trigger[i], sizeof(This->trigger[0]), &size, NULL))
1339 hr = HRESULT_FROM_WIN32(GetLastError());
1340 break;
1344 else
1345 hr = HRESULT_FROM_WIN32(GetLastError());
1347 if (idx != 0xffff)
1348 ITask_DeleteTrigger(&This->ITask_iface, idx);
1350 return hr;
1353 static BOOL write_unicode_string(HANDLE hfile, const WCHAR *str)
1355 USHORT count;
1356 DWORD size;
1358 count = str ? (lstrlenW(str) + 1) : 0;
1359 if (!WriteFile(hfile, &count, sizeof(count), &size, NULL))
1360 return FALSE;
1362 if (!str) return TRUE;
1364 count *= sizeof(WCHAR);
1365 return WriteFile(hfile, str, count, &size, NULL);
1368 static HRESULT WINAPI MSTASK_IPersistFile_Save(IPersistFile *iface, LPCOLESTR task_name, BOOL remember)
1370 static WCHAR authorW[] = { 'W','i','n','e',0 };
1371 static WCHAR commentW[] = { 'C','r','e','a','t','e','d',' ','b','y',' ','W','i','n','e',0 };
1372 FIXDLEN_DATA fixed;
1373 WORD word, user_data_size = 0;
1374 HANDLE hfile;
1375 DWORD size, ver, disposition, try;
1376 TaskImpl *This = impl_from_IPersistFile(iface);
1377 ITask *task = &This->ITask_iface;
1378 LPWSTR appname = NULL, params = NULL, workdir = NULL, creator = NULL, comment = NULL;
1379 BYTE *user_data = NULL;
1380 HRESULT hr;
1382 TRACE("(%p, %s, %d)\n", iface, debugstr_w(task_name), remember);
1384 disposition = task_name ? CREATE_NEW : OPEN_ALWAYS;
1386 if (!task_name)
1388 task_name = This->task_name;
1389 remember = FALSE;
1392 ITask_GetComment(task, &comment);
1393 if (!comment) comment = commentW;
1394 ITask_GetCreator(task, &creator);
1395 if (!creator) creator = authorW;
1396 ITask_GetApplicationName(task, &appname);
1397 ITask_GetParameters(task, &params);
1398 ITask_GetWorkingDirectory(task, &workdir);
1399 ITask_GetWorkItemData(task, &user_data_size, &user_data);
1401 ver = GetVersion();
1402 fixed.product_version = MAKEWORD(ver >> 8, ver);
1403 fixed.file_version = 0x0001;
1404 CoCreateGuid(&fixed.uuid);
1405 fixed.name_size_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1406 fixed.trigger_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1407 fixed.trigger_offset += sizeof(USHORT); /* Application Name */
1408 if (appname)
1409 fixed.trigger_offset += (lstrlenW(appname) + 1) * sizeof(WCHAR);
1410 fixed.trigger_offset += sizeof(USHORT); /* Parameters */
1411 if (params)
1412 fixed.trigger_offset += (lstrlenW(params) + 1) * sizeof(WCHAR);
1413 fixed.trigger_offset += sizeof(USHORT); /* Working Directory */
1414 if (workdir)
1415 fixed.trigger_offset += (lstrlenW(workdir) + 1) * sizeof(WCHAR);
1416 fixed.trigger_offset += sizeof(USHORT); /* Author */
1417 if (creator)
1418 fixed.trigger_offset += (lstrlenW(creator) + 1) * sizeof(WCHAR);
1419 fixed.trigger_offset += sizeof(USHORT); /* Comment */
1420 if (comment)
1421 fixed.trigger_offset += (lstrlenW(comment) + 1) * sizeof(WCHAR);
1422 fixed.trigger_offset += sizeof(USHORT) + user_data_size; /* User Data */
1423 fixed.trigger_offset += 10; /* Reserved Data */
1425 fixed.error_retry_count = 0;
1426 fixed.error_retry_interval = 0;
1427 fixed.idle_wait = This->idle_minutes;
1428 fixed.idle_deadline = This->deadline_minutes;
1429 fixed.priority = This->priority;
1430 fixed.maximum_runtime = This->maxRunTime;
1431 fixed.exit_code = 0;
1432 if (This->status == SCHED_S_TASK_NOT_SCHEDULED && This->trigger_count)
1433 This->status = SCHED_S_TASK_HAS_NOT_RUN;
1434 fixed.status = This->status;
1435 fixed.flags = This->flags;
1436 memset(&fixed.last_runtime, 0, sizeof(fixed.last_runtime));
1438 try = 1;
1439 for (;;)
1441 hfile = CreateFileW(task_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, disposition, 0, 0);
1442 if (hfile != INVALID_HANDLE_VALUE) break;
1444 if (try++ >= 3) return HRESULT_FROM_WIN32(GetLastError());
1445 Sleep(100);
1448 if (!WriteFile(hfile, &fixed, sizeof(fixed), &size, NULL))
1450 hr = HRESULT_FROM_WIN32(GetLastError());
1451 goto failed;
1454 /* Instance Count: don't touch it in the client */
1455 if (SetFilePointer(hfile, sizeof(word), NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
1457 hr = HRESULT_FROM_WIN32(GetLastError());
1458 goto failed;
1460 /* Application Name */
1461 if (!write_unicode_string(hfile, appname))
1463 hr = HRESULT_FROM_WIN32(GetLastError());
1464 goto failed;
1466 /* Parameters */
1467 if (!write_unicode_string(hfile, params))
1469 hr = HRESULT_FROM_WIN32(GetLastError());
1470 goto failed;
1472 /* Working Directory */
1473 if (!write_unicode_string(hfile, workdir))
1475 hr = HRESULT_FROM_WIN32(GetLastError());
1476 goto failed;
1478 /* Author */
1479 if (!write_unicode_string(hfile, creator))
1481 hr = HRESULT_FROM_WIN32(GetLastError());
1482 goto failed;
1484 /* Comment */
1485 if (!write_unicode_string(hfile, comment))
1487 hr = HRESULT_FROM_WIN32(GetLastError());
1488 goto failed;
1491 /* User Data */
1492 if (!write_user_data(hfile, user_data, user_data_size))
1494 hr = HRESULT_FROM_WIN32(GetLastError());
1495 goto failed;
1498 /* Reserved Data */
1499 if (!write_reserved_data(hfile))
1501 hr = HRESULT_FROM_WIN32(GetLastError());
1502 goto failed;
1505 /* Triggers */
1506 hr = write_triggers(This, hfile);
1507 if (hr != S_OK)
1508 goto failed;
1510 /* Signature */
1511 if (!write_signature(hfile))
1513 hr = HRESULT_FROM_WIN32(GetLastError());
1514 goto failed;
1517 hr = S_OK;
1518 This->is_dirty = FALSE;
1520 failed:
1521 CoTaskMemFree(appname);
1522 CoTaskMemFree(params);
1523 CoTaskMemFree(workdir);
1524 if (creator != authorW)
1525 CoTaskMemFree(creator);
1526 if (comment != commentW)
1527 CoTaskMemFree(comment);
1528 CoTaskMemFree(user_data);
1530 CloseHandle(hfile);
1531 if (hr != S_OK)
1532 DeleteFileW(task_name);
1533 else if (remember)
1535 heap_free(This->task_name);
1536 This->task_name = heap_strdupW(task_name);
1538 return hr;
1541 static HRESULT WINAPI MSTASK_IPersistFile_SaveCompleted(
1542 IPersistFile* iface,
1543 LPCOLESTR pszFileName)
1545 FIXME("(%p, %p): stub\n", iface, pszFileName);
1546 return E_NOTIMPL;
1549 static HRESULT WINAPI MSTASK_IPersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *file_name)
1551 TaskImpl *This = impl_from_IPersistFile(iface);
1553 TRACE("(%p, %p)\n", iface, file_name);
1555 *file_name = CoTaskMemAlloc((strlenW(This->task_name) + 1) * sizeof(WCHAR));
1556 if (!*file_name) return E_OUTOFMEMORY;
1558 strcpyW(*file_name, This->task_name);
1559 return S_OK;
1562 static const ITaskVtbl MSTASK_ITaskVtbl =
1564 MSTASK_ITask_QueryInterface,
1565 MSTASK_ITask_AddRef,
1566 MSTASK_ITask_Release,
1567 MSTASK_ITask_CreateTrigger,
1568 MSTASK_ITask_DeleteTrigger,
1569 MSTASK_ITask_GetTriggerCount,
1570 MSTASK_ITask_GetTrigger,
1571 MSTASK_ITask_GetTriggerString,
1572 MSTASK_ITask_GetRunTimes,
1573 MSTASK_ITask_GetNextRunTime,
1574 MSTASK_ITask_SetIdleWait,
1575 MSTASK_ITask_GetIdleWait,
1576 MSTASK_ITask_Run,
1577 MSTASK_ITask_Terminate,
1578 MSTASK_ITask_EditWorkItem,
1579 MSTASK_ITask_GetMostRecentRunTime,
1580 MSTASK_ITask_GetStatus,
1581 MSTASK_ITask_GetExitCode,
1582 MSTASK_ITask_SetComment,
1583 MSTASK_ITask_GetComment,
1584 MSTASK_ITask_SetCreator,
1585 MSTASK_ITask_GetCreator,
1586 MSTASK_ITask_SetWorkItemData,
1587 MSTASK_ITask_GetWorkItemData,
1588 MSTASK_ITask_SetErrorRetryCount,
1589 MSTASK_ITask_GetErrorRetryCount,
1590 MSTASK_ITask_SetErrorRetryInterval,
1591 MSTASK_ITask_GetErrorRetryInterval,
1592 MSTASK_ITask_SetFlags,
1593 MSTASK_ITask_GetFlags,
1594 MSTASK_ITask_SetAccountInformation,
1595 MSTASK_ITask_GetAccountInformation,
1596 MSTASK_ITask_SetApplicationName,
1597 MSTASK_ITask_GetApplicationName,
1598 MSTASK_ITask_SetParameters,
1599 MSTASK_ITask_GetParameters,
1600 MSTASK_ITask_SetWorkingDirectory,
1601 MSTASK_ITask_GetWorkingDirectory,
1602 MSTASK_ITask_SetPriority,
1603 MSTASK_ITask_GetPriority,
1604 MSTASK_ITask_SetTaskFlags,
1605 MSTASK_ITask_GetTaskFlags,
1606 MSTASK_ITask_SetMaxRunTime,
1607 MSTASK_ITask_GetMaxRunTime
1610 static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
1612 MSTASK_IPersistFile_QueryInterface,
1613 MSTASK_IPersistFile_AddRef,
1614 MSTASK_IPersistFile_Release,
1615 MSTASK_IPersistFile_GetClassID,
1616 MSTASK_IPersistFile_IsDirty,
1617 MSTASK_IPersistFile_Load,
1618 MSTASK_IPersistFile_Save,
1619 MSTASK_IPersistFile_SaveCompleted,
1620 MSTASK_IPersistFile_GetCurFile
1623 HRESULT TaskConstructor(ITaskService *service, const WCHAR *name, ITask **task)
1625 static const WCHAR tasksW[] = { '\\','T','a','s','k','s','\\',0 };
1626 static const WCHAR jobW[] = { '.','j','o','b',0 };
1627 TaskImpl *This;
1628 WCHAR task_name[MAX_PATH];
1629 ITaskDefinition *taskdef;
1630 IActionCollection *actions;
1631 HRESULT hr;
1633 TRACE("(%s, %p)\n", debugstr_w(name), task);
1635 if (strchrW(name, '.')) return E_INVALIDARG;
1637 GetWindowsDirectoryW(task_name, MAX_PATH);
1638 lstrcatW(task_name, tasksW);
1639 lstrcatW(task_name, name);
1640 lstrcatW(task_name, jobW);
1642 hr = ITaskService_NewTask(service, 0, &taskdef);
1643 if (hr != S_OK) return hr;
1645 This = heap_alloc(sizeof(*This));
1646 if (!This)
1648 ITaskDefinition_Release(taskdef);
1649 return E_OUTOFMEMORY;
1652 This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
1653 This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
1654 This->ref = 1;
1655 This->task = taskdef;
1656 This->task_name = heap_strdupW(task_name);
1657 This->flags = 0;
1658 This->status = SCHED_S_TASK_NOT_SCHEDULED;
1659 This->idle_minutes = 10;
1660 This->deadline_minutes = 60;
1661 This->priority = NORMAL_PRIORITY_CLASS;
1662 This->accountName = NULL;
1663 This->trigger_count = 0;
1664 This->trigger = NULL;
1665 This->is_dirty = FALSE;
1666 This->instance_count = 0;
1668 /* Default time is 3 days = 259200000 ms */
1669 This->maxRunTime = 259200000;
1671 hr = ITaskDefinition_get_Actions(This->task, &actions);
1672 if (hr == S_OK)
1674 hr = IActionCollection_Create(actions, TASK_ACTION_EXEC, (IAction **)&This->action);
1675 IActionCollection_Release(actions);
1676 if (hr == S_OK)
1678 *task = &This->ITask_iface;
1679 InterlockedIncrement(&dll_ref);
1680 return S_OK;
1684 ITaskDefinition_Release(This->task);
1685 ITask_Release(&This->ITask_iface);
1686 return hr;