winecfg: Constrain DPI values to the commonly supported ones.
[wine.git] / dlls / mstask / task.c
blobd487aeb5a5e9196b30fd991c18b309f437014ddb
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 HRESULT WINAPI MSTASK_ITask_GetNextRunTime(ITask *iface, SYSTEMTIME *st)
413 FIXME("(%p, %p): stub\n", iface, st);
415 memset(st, 0, sizeof(*st));
416 return SCHED_S_TASK_NO_VALID_TRIGGERS;
419 static HRESULT WINAPI MSTASK_ITask_SetIdleWait(
420 ITask* iface,
421 WORD wIdleMinutes,
422 WORD wDeadlineMinutes)
424 FIXME("(%p, %d, %d): stub\n", iface, wIdleMinutes, wDeadlineMinutes);
425 return E_NOTIMPL;
428 static HRESULT WINAPI MSTASK_ITask_GetIdleWait(ITask *iface, WORD *idle_minutes, WORD *deadline_minutes)
430 TaskImpl *This = impl_from_ITask(iface);
432 TRACE("(%p, %p, %p): stub\n", iface, idle_minutes, deadline_minutes);
434 *idle_minutes = This->idle_minutes;
435 *deadline_minutes = This->deadline_minutes;
436 return S_OK;
439 static HRESULT WINAPI MSTASK_ITask_Run(ITask *iface)
441 TaskImpl *This = impl_from_ITask(iface);
443 TRACE("(%p)\n", iface);
445 if (This->status == SCHED_S_TASK_NOT_SCHEDULED)
446 return SCHED_E_TASK_NOT_READY;
448 This->flags |= 0x04000000;
449 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
452 static HRESULT WINAPI MSTASK_ITask_Terminate(ITask *iface)
454 TaskImpl *This = impl_from_ITask(iface);
456 TRACE("(%p)\n", iface);
458 if (!This->instance_count)
459 return SCHED_E_TASK_NOT_RUNNING;
461 This->flags |= 0x08000000;
462 return IPersistFile_Save(&This->IPersistFile_iface, NULL, FALSE);
465 static HRESULT WINAPI MSTASK_ITask_EditWorkItem(
466 ITask* iface,
467 HWND hParent,
468 DWORD dwReserved)
470 FIXME("(%p, %p, %d): stub\n", iface, hParent, dwReserved);
471 return E_NOTIMPL;
474 static HRESULT WINAPI MSTASK_ITask_GetMostRecentRunTime(ITask *iface, SYSTEMTIME *st)
476 FIXME("(%p, %p): stub\n", iface, st);
478 memset(st, 0, sizeof(*st));
479 return SCHED_S_TASK_HAS_NOT_RUN;
482 static HRESULT WINAPI MSTASK_ITask_GetStatus(ITask *iface, HRESULT *status)
484 TaskImpl *This = impl_from_ITask(iface);
486 TRACE("(%p, %p)\n", iface, status);
488 *status = This->instance_count ? SCHED_S_TASK_RUNNING : This->status;
489 return S_OK;
492 static HRESULT WINAPI MSTASK_ITask_GetExitCode(ITask *iface, DWORD *exit_code)
494 TaskImpl *This = impl_from_ITask(iface);
496 TRACE("(%p, %p)\n", iface, exit_code);
498 *exit_code = This->exit_code;
499 return SCHED_S_TASK_HAS_NOT_RUN; /* FIXME */
502 static HRESULT WINAPI MSTASK_ITask_SetComment(ITask *iface, LPCWSTR comment)
504 TaskImpl *This = impl_from_ITask(iface);
505 IRegistrationInfo *info;
506 HRESULT hr;
508 TRACE("(%p, %s)\n", iface, debugstr_w(comment));
510 if (!comment || !comment[0])
511 comment = NULL;
513 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
514 if (hr == S_OK)
516 hr = IRegistrationInfo_put_Description(info, (BSTR)comment);
517 IRegistrationInfo_Release(info);
518 This->is_dirty = TRUE;
520 return hr;
523 static HRESULT WINAPI MSTASK_ITask_GetComment(ITask *iface, LPWSTR *comment)
525 TaskImpl *This = impl_from_ITask(iface);
526 IRegistrationInfo *info;
527 HRESULT hr;
528 BSTR description;
529 DWORD len;
531 TRACE("(%p, %p)\n", iface, comment);
533 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
534 if (hr != S_OK) return hr;
536 hr = IRegistrationInfo_get_Description(info, &description);
537 if (hr == S_OK)
539 len = description ? lstrlenW(description) + 1 : 1;
540 *comment = CoTaskMemAlloc(len * sizeof(WCHAR));
541 if (*comment)
543 if (!description)
544 *comment[0] = 0;
545 else
546 lstrcpyW(*comment, description);
547 hr = S_OK;
549 else
550 hr = E_OUTOFMEMORY;
552 SysFreeString(description);
555 IRegistrationInfo_Release(info);
556 return hr;
559 static HRESULT WINAPI MSTASK_ITask_SetCreator(ITask *iface, LPCWSTR creator)
561 TaskImpl *This = impl_from_ITask(iface);
562 IRegistrationInfo *info;
563 HRESULT hr;
565 TRACE("(%p, %s)\n", iface, debugstr_w(creator));
567 if (!creator || !creator[0])
568 creator = NULL;
570 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
571 if (hr == S_OK)
573 hr = IRegistrationInfo_put_Author(info, (BSTR)creator);
574 IRegistrationInfo_Release(info);
575 This->is_dirty = TRUE;
577 return hr;
580 static HRESULT WINAPI MSTASK_ITask_GetCreator(ITask *iface, LPWSTR *creator)
582 TaskImpl *This = impl_from_ITask(iface);
583 IRegistrationInfo *info;
584 HRESULT hr;
585 BSTR author;
586 DWORD len;
588 TRACE("(%p, %p)\n", iface, creator);
590 hr = ITaskDefinition_get_RegistrationInfo(This->task, &info);
591 if (hr != S_OK) return hr;
593 hr = IRegistrationInfo_get_Author(info, &author);
594 if (hr == S_OK)
596 len = author ? lstrlenW(author) + 1 : 1;
597 *creator = CoTaskMemAlloc(len * sizeof(WCHAR));
598 if (*creator)
600 if (!author)
601 *creator[0] = 0;
602 else
603 lstrcpyW(*creator, author);
604 hr = S_OK;
606 else
607 hr = E_OUTOFMEMORY;
609 SysFreeString(author);
612 IRegistrationInfo_Release(info);
613 return hr;
616 static HRESULT WINAPI MSTASK_ITask_SetWorkItemData(
617 ITask* iface,
618 WORD cBytes,
619 BYTE rgbData[])
621 FIXME("(%p, %d, %p): stub\n", iface, cBytes, rgbData);
622 return E_NOTIMPL;
625 static HRESULT WINAPI MSTASK_ITask_GetWorkItemData(
626 ITask* iface,
627 WORD *pcBytes,
628 BYTE **ppBytes)
630 FIXME("(%p, %p, %p): stub\n", iface, pcBytes, ppBytes);
631 return E_NOTIMPL;
634 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryCount(
635 ITask* iface,
636 WORD wRetryCount)
638 FIXME("(%p, %d): stub\n", iface, wRetryCount);
639 return E_NOTIMPL;
642 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryCount(ITask *iface, WORD *count)
644 TRACE("(%p, %p)\n", iface, count);
645 return E_NOTIMPL;
648 static HRESULT WINAPI MSTASK_ITask_SetErrorRetryInterval(
649 ITask* iface,
650 WORD wRetryInterval)
652 FIXME("(%p, %d): stub\n", iface, wRetryInterval);
653 return E_NOTIMPL;
656 static HRESULT WINAPI MSTASK_ITask_GetErrorRetryInterval(ITask *iface, WORD *interval)
658 TRACE("(%p, %p)\n", iface, interval);
659 return E_NOTIMPL;
662 static HRESULT WINAPI MSTASK_ITask_SetFlags(ITask *iface, DWORD flags)
664 TaskImpl *This = impl_from_ITask(iface);
666 TRACE("(%p, 0x%08x)\n", iface, flags);
667 This->flags &= 0xffff8000;
668 This->flags |= flags & 0x7fff;
669 This->is_dirty = TRUE;
670 return S_OK;
673 static HRESULT WINAPI MSTASK_ITask_GetFlags(ITask *iface, DWORD *flags)
675 TaskImpl *This = impl_from_ITask(iface);
677 TRACE("(%p, %p)\n", iface, flags);
678 *flags = LOWORD(This->flags);
679 return S_OK;
682 static HRESULT WINAPI MSTASK_ITask_SetAccountInformation(
683 ITask* iface,
684 LPCWSTR pwszAccountName,
685 LPCWSTR pwszPassword)
687 DWORD n;
688 TaskImpl *This = impl_from_ITask(iface);
689 LPWSTR tmp_account_name;
691 TRACE("(%p, %s, %s): partial stub\n", iface, debugstr_w(pwszAccountName),
692 debugstr_w(pwszPassword));
694 if (pwszPassword)
695 FIXME("Partial stub ignores passwords\n");
697 n = (lstrlenW(pwszAccountName) + 1);
698 tmp_account_name = heap_alloc(n * sizeof(WCHAR));
699 if (!tmp_account_name)
700 return E_OUTOFMEMORY;
701 lstrcpyW(tmp_account_name, pwszAccountName);
702 heap_free(This->accountName);
703 This->accountName = tmp_account_name;
704 This->is_dirty = TRUE;
705 return S_OK;
708 static HRESULT WINAPI MSTASK_ITask_GetAccountInformation(
709 ITask* iface,
710 LPWSTR *ppwszAccountName)
712 DWORD n;
713 TaskImpl *This = impl_from_ITask(iface);
715 TRACE("(%p, %p): partial stub\n", iface, ppwszAccountName);
717 /* This implements the WinXP behavior when accountName has not yet
718 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
719 if (!This->accountName)
720 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
722 n = (lstrlenW(This->accountName) + 1);
723 *ppwszAccountName = CoTaskMemAlloc(n * sizeof(WCHAR));
724 if (!*ppwszAccountName)
725 return E_OUTOFMEMORY;
726 lstrcpyW(*ppwszAccountName, This->accountName);
727 return S_OK;
730 static HRESULT WINAPI MSTASK_ITask_SetApplicationName(ITask *iface, LPCWSTR appname)
732 TaskImpl *This = impl_from_ITask(iface);
733 DWORD len;
734 HRESULT hr;
736 TRACE("(%p, %s)\n", iface, debugstr_w(appname));
738 /* Empty application name */
739 if (!appname || !appname[0])
740 return IExecAction_put_Path(This->action, NULL);
742 /* Attempt to set pwszApplicationName to a path resolved application name */
743 len = SearchPathW(NULL, appname, NULL, 0, NULL, NULL);
744 if (len)
746 LPWSTR tmp_name;
748 tmp_name = heap_alloc(len * sizeof(WCHAR));
749 if (!tmp_name)
750 return E_OUTOFMEMORY;
751 len = SearchPathW(NULL, appname, NULL, len, tmp_name, NULL);
752 if (len)
754 hr = IExecAction_put_Path(This->action, tmp_name);
755 if (hr == S_OK) This->is_dirty = TRUE;
757 else
758 hr = HRESULT_FROM_WIN32(GetLastError());
760 heap_free(tmp_name);
761 return hr;
764 /* If unable to path resolve name, simply set to appname */
765 hr = IExecAction_put_Path(This->action, (BSTR)appname);
766 if (hr == S_OK) This->is_dirty = TRUE;
767 return hr;
770 static HRESULT WINAPI MSTASK_ITask_GetApplicationName(ITask *iface, LPWSTR *appname)
772 TaskImpl *This = impl_from_ITask(iface);
773 HRESULT hr;
774 BSTR path;
775 DWORD len;
777 TRACE("(%p, %p)\n", iface, appname);
779 hr = IExecAction_get_Path(This->action, &path);
780 if (hr != S_OK) return hr;
782 len = path ? lstrlenW(path) + 1 : 1;
783 *appname = CoTaskMemAlloc(len * sizeof(WCHAR));
784 if (*appname)
786 if (!path)
787 *appname[0] = 0;
788 else
789 lstrcpyW(*appname, path);
790 hr = S_OK;
792 else
793 hr = E_OUTOFMEMORY;
795 SysFreeString(path);
796 return hr;
799 static HRESULT WINAPI MSTASK_ITask_SetParameters(ITask *iface, LPCWSTR params)
801 TaskImpl *This = impl_from_ITask(iface);
802 HRESULT hr;
804 TRACE("(%p, %s)\n", iface, debugstr_w(params));
806 /* Empty parameter list */
807 if (!params || !params[0])
808 params = NULL;
810 hr = IExecAction_put_Arguments(This->action, (BSTR)params);
811 if (hr == S_OK) This->is_dirty = TRUE;
812 return hr;
815 static HRESULT WINAPI MSTASK_ITask_GetParameters(ITask *iface, LPWSTR *params)
817 TaskImpl *This = impl_from_ITask(iface);
818 HRESULT hr;
819 BSTR args;
820 DWORD len;
822 TRACE("(%p, %p)\n", iface, params);
824 hr = IExecAction_get_Arguments(This->action, &args);
825 if (hr != S_OK) return hr;
827 len = args ? lstrlenW(args) + 1 : 1;
828 *params = CoTaskMemAlloc(len * sizeof(WCHAR));
829 if (*params)
831 if (!args)
832 *params[0] = 0;
833 else
834 lstrcpyW(*params, args);
835 hr = S_OK;
837 else
838 hr = E_OUTOFMEMORY;
840 SysFreeString(args);
841 return hr;
844 static HRESULT WINAPI MSTASK_ITask_SetWorkingDirectory(ITask * iface, LPCWSTR workdir)
846 TaskImpl *This = impl_from_ITask(iface);
847 HRESULT hr;
849 TRACE("(%p, %s)\n", iface, debugstr_w(workdir));
851 if (!workdir || !workdir[0])
852 workdir = NULL;
854 hr = IExecAction_put_WorkingDirectory(This->action, (BSTR)workdir);
855 if (hr == S_OK) This->is_dirty = TRUE;
856 return hr;
859 static HRESULT WINAPI MSTASK_ITask_GetWorkingDirectory(ITask *iface, LPWSTR *workdir)
861 TaskImpl *This = impl_from_ITask(iface);
862 HRESULT hr;
863 BSTR dir;
864 DWORD len;
866 TRACE("(%p, %p)\n", iface, workdir);
868 hr = IExecAction_get_WorkingDirectory(This->action, &dir);
869 if (hr != S_OK) return hr;
871 len = dir ? lstrlenW(dir) + 1 : 1;
872 *workdir = CoTaskMemAlloc(len * sizeof(WCHAR));
873 if (*workdir)
875 if (!dir)
876 *workdir[0] = 0;
877 else
878 lstrcpyW(*workdir, dir);
879 hr = S_OK;
881 else
882 hr = E_OUTOFMEMORY;
884 SysFreeString(dir);
885 return hr;
888 static HRESULT WINAPI MSTASK_ITask_SetPriority(
889 ITask* iface,
890 DWORD dwPriority)
892 FIXME("(%p, 0x%08x): stub\n", iface, dwPriority);
893 return E_NOTIMPL;
896 static HRESULT WINAPI MSTASK_ITask_GetPriority(ITask *iface, DWORD *priority)
898 TaskImpl *This = impl_from_ITask(iface);
900 TRACE("(%p, %p)\n", iface, priority);
902 *priority = This->priority;
903 return S_OK;
906 static HRESULT WINAPI MSTASK_ITask_SetTaskFlags(
907 ITask* iface,
908 DWORD dwFlags)
910 FIXME("(%p, 0x%08x): stub\n", iface, dwFlags);
911 return E_NOTIMPL;
914 static HRESULT WINAPI MSTASK_ITask_GetTaskFlags(ITask *iface, DWORD *flags)
916 FIXME("(%p, %p): stub\n", iface, flags);
917 *flags = 0;
918 return S_OK;
921 static HRESULT WINAPI MSTASK_ITask_SetMaxRunTime(
922 ITask* iface,
923 DWORD dwMaxRunTime)
925 TaskImpl *This = impl_from_ITask(iface);
927 TRACE("(%p, %d)\n", iface, dwMaxRunTime);
929 This->maxRunTime = dwMaxRunTime;
930 This->is_dirty = TRUE;
931 return S_OK;
934 static HRESULT WINAPI MSTASK_ITask_GetMaxRunTime(
935 ITask* iface,
936 DWORD *pdwMaxRunTime)
938 TaskImpl *This = impl_from_ITask(iface);
940 TRACE("(%p, %p)\n", iface, pdwMaxRunTime);
942 *pdwMaxRunTime = This->maxRunTime;
943 return S_OK;
946 static HRESULT WINAPI MSTASK_IPersistFile_QueryInterface(
947 IPersistFile* iface,
948 REFIID riid,
949 void **ppvObject)
951 TaskImpl *This = impl_from_IPersistFile(iface);
952 TRACE("(%p, %s, %p)\n", iface, debugstr_guid(riid), ppvObject);
953 return ITask_QueryInterface(&This->ITask_iface, riid, ppvObject);
956 static ULONG WINAPI MSTASK_IPersistFile_AddRef(
957 IPersistFile* iface)
959 TaskImpl *This = impl_from_IPersistFile(iface);
960 return ITask_AddRef(&This->ITask_iface);
963 static ULONG WINAPI MSTASK_IPersistFile_Release(
964 IPersistFile* iface)
966 TaskImpl *This = impl_from_IPersistFile(iface);
967 return ITask_Release(&This->ITask_iface);
970 static HRESULT WINAPI MSTASK_IPersistFile_GetClassID(IPersistFile *iface, CLSID *clsid)
972 TRACE("(%p, %p)\n", iface, clsid);
974 *clsid = CLSID_CTask;
975 return S_OK;
978 static HRESULT WINAPI MSTASK_IPersistFile_IsDirty(IPersistFile *iface)
980 TaskImpl *This = impl_from_IPersistFile(iface);
981 TRACE("(%p)\n", iface);
982 return This->is_dirty ? S_OK : S_FALSE;
985 static DWORD load_unicode_strings(ITask *task, BYTE *data, DWORD limit)
987 DWORD i, data_size = 0;
988 USHORT len;
990 for (i = 0; i < 5; i++)
992 if (limit < sizeof(USHORT))
994 TRACE("invalid string %u offset\n", i);
995 break;
998 len = *(USHORT *)data;
999 data += sizeof(USHORT);
1000 data_size += sizeof(USHORT);
1001 limit -= sizeof(USHORT);
1002 if (limit < len * sizeof(WCHAR))
1004 TRACE("invalid string %u size\n", i);
1005 break;
1008 TRACE("string %u: %s\n", i, wine_dbgstr_wn((const WCHAR *)data, len));
1010 switch (i)
1012 case 0:
1013 ITask_SetApplicationName(task, (const WCHAR *)data);
1014 break;
1015 case 1:
1016 ITask_SetParameters(task, (const WCHAR *)data);
1017 break;
1018 case 2:
1019 ITask_SetWorkingDirectory(task, (const WCHAR *)data);
1020 break;
1021 case 3:
1022 ITask_SetCreator(task, (const WCHAR *)data);
1023 break;
1024 case 4:
1025 ITask_SetComment(task, (const WCHAR *)data);
1026 break;
1027 default:
1028 break;
1031 data += len * sizeof(WCHAR);
1032 data_size += len * sizeof(WCHAR);
1035 return data_size;
1038 static HRESULT load_job_data(TaskImpl *This, BYTE *data, DWORD size)
1040 ITask *task = &This->ITask_iface;
1041 HRESULT hr;
1042 const FIXDLEN_DATA *fixed;
1043 const SYSTEMTIME *st;
1044 DWORD unicode_strings_size, data_size, triggers_size;
1045 USHORT trigger_count, i;
1046 const USHORT *signature;
1047 TASK_TRIGGER *task_trigger;
1049 if (size < sizeof(*fixed))
1051 TRACE("no space for FIXDLEN_DATA\n");
1052 return SCHED_E_INVALID_TASK;
1055 fixed = (const FIXDLEN_DATA *)data;
1057 TRACE("product_version %04x\n", fixed->product_version);
1058 TRACE("file_version %04x\n", fixed->file_version);
1059 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed->uuid));
1061 if (fixed->file_version != 0x0001)
1062 return SCHED_E_INVALID_TASK;
1064 TRACE("name_size_offset %04x\n", fixed->name_size_offset);
1065 TRACE("trigger_offset %04x\n", fixed->trigger_offset);
1066 TRACE("error_retry_count %u\n", fixed->error_retry_count);
1067 TRACE("error_retry_interval %u\n", fixed->error_retry_interval);
1068 TRACE("idle_deadline %u\n", fixed->idle_deadline);
1069 This->deadline_minutes = fixed->idle_deadline;
1070 TRACE("idle_wait %u\n", fixed->idle_wait);
1071 This->idle_minutes = fixed->idle_wait;
1072 TRACE("priority %08x\n", fixed->priority);
1073 This->priority = fixed->priority;
1074 TRACE("maximum_runtime %u\n", fixed->maximum_runtime);
1075 This->maxRunTime = fixed->maximum_runtime;
1076 TRACE("exit_code %#x\n", fixed->exit_code);
1077 This->exit_code = fixed->exit_code;
1078 TRACE("status %08x\n", fixed->status);
1079 This->status = fixed->status;
1080 TRACE("flags %08x\n", fixed->flags);
1081 This->flags = fixed->flags;
1082 st = &fixed->last_runtime;
1083 TRACE("last_runtime %d/%d/%d wday %d %d:%d:%d.%03d\n",
1084 st->wDay, st->wMonth, st->wYear, st->wDayOfWeek,
1085 st->wHour, st->wMinute, st->wSecond, st->wMilliseconds);
1087 /* Instance Count */
1088 if (size < sizeof(*fixed) + sizeof(USHORT))
1090 TRACE("no space for instance count\n");
1091 return SCHED_E_INVALID_TASK;
1094 This->instance_count = *(const USHORT *)(data + sizeof(*fixed));
1095 TRACE("instance count %u\n", This->instance_count);
1097 if (fixed->name_size_offset + sizeof(USHORT) < size)
1098 unicode_strings_size = load_unicode_strings(task, data + fixed->name_size_offset, size - fixed->name_size_offset);
1099 else
1101 TRACE("invalid name_size_offset\n");
1102 return SCHED_E_INVALID_TASK;
1104 TRACE("unicode strings end at %#x\n", fixed->name_size_offset + unicode_strings_size);
1106 if (size < fixed->trigger_offset + sizeof(USHORT))
1108 TRACE("no space for triggers count\n");
1109 return SCHED_E_INVALID_TASK;
1111 trigger_count = *(const USHORT *)(data + fixed->trigger_offset);
1112 TRACE("trigger_count %u\n", trigger_count);
1113 triggers_size = size - fixed->trigger_offset - sizeof(USHORT);
1114 TRACE("triggers_size %u\n", triggers_size);
1115 task_trigger = (TASK_TRIGGER *)(data + fixed->trigger_offset + sizeof(USHORT));
1117 data += fixed->name_size_offset + unicode_strings_size;
1118 size -= fixed->name_size_offset + unicode_strings_size;
1120 /* User Data */
1121 if (size < sizeof(USHORT))
1123 TRACE("no space for user data size\n");
1124 return SCHED_E_INVALID_TASK;
1127 data_size = *(const USHORT *)data;
1128 if (size < sizeof(USHORT) + data_size)
1130 TRACE("no space for user data\n");
1131 return SCHED_E_INVALID_TASK;
1133 TRACE("User Data size %#x\n", data_size);
1134 ITask_SetWorkItemData(task, data_size, data + sizeof(USHORT));
1136 size -= sizeof(USHORT) + data_size;
1137 data += sizeof(USHORT) + data_size;
1139 /* Reserved Data */
1140 if (size < sizeof(USHORT))
1142 TRACE("no space for reserved data size\n");
1143 return SCHED_E_INVALID_TASK;
1146 data_size = *(const USHORT *)data;
1147 if (size < sizeof(USHORT) + data_size)
1149 TRACE("no space for reserved data\n");
1150 return SCHED_E_INVALID_TASK;
1152 TRACE("Reserved Data size %#x\n", data_size);
1154 size -= sizeof(USHORT) + data_size;
1155 data += sizeof(USHORT) + data_size;
1157 /* Trigger Data */
1158 TRACE("trigger_offset %04x, triggers end at %04x\n", fixed->trigger_offset,
1159 (DWORD)(fixed->trigger_offset + sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER)));
1161 task_trigger = (TASK_TRIGGER *)(data + sizeof(USHORT));
1163 if (trigger_count * sizeof(TASK_TRIGGER) > triggers_size)
1165 TRACE("no space for triggers data\n");
1166 return SCHED_E_INVALID_TASK;
1169 This->trigger_count = 0;
1171 for (i = 0; i < trigger_count; i++)
1173 ITaskTrigger *trigger;
1174 WORD idx;
1176 hr = ITask_CreateTrigger(task, &idx, &trigger);
1177 if (hr != S_OK) return hr;
1179 hr = ITaskTrigger_SetTrigger(trigger, &task_trigger[i]);
1180 ITaskTrigger_Release(trigger);
1181 if (hr != S_OK)
1183 ITask_DeleteTrigger(task, idx);
1184 return hr;
1188 size -= sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1189 data += sizeof(USHORT) + trigger_count * sizeof(TASK_TRIGGER);
1191 if (size < 2 * sizeof(USHORT) + 64)
1193 TRACE("no space for signature\n");
1194 return S_OK; /* signature is optional */
1197 signature = (const USHORT *)data;
1198 TRACE("signature version %04x, client version %04x\n", signature[0], signature[1]);
1200 return S_OK;
1203 static HRESULT WINAPI MSTASK_IPersistFile_Load(IPersistFile *iface, LPCOLESTR file_name, DWORD mode)
1205 TaskImpl *This = impl_from_IPersistFile(iface);
1206 HRESULT hr;
1207 HANDLE file, mapping;
1208 DWORD access, sharing, size;
1209 void *data;
1211 TRACE("(%p, %s, 0x%08x)\n", iface, debugstr_w(file_name), mode);
1213 switch (mode & 0x000f)
1215 default:
1216 case STGM_READ:
1217 access = GENERIC_READ;
1218 break;
1219 case STGM_WRITE:
1220 case STGM_READWRITE:
1221 access = GENERIC_READ | GENERIC_WRITE;
1222 break;
1225 switch (mode & 0x00f0)
1227 default:
1228 case STGM_SHARE_DENY_NONE:
1229 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1230 break;
1231 case STGM_SHARE_DENY_READ:
1232 sharing = FILE_SHARE_WRITE;
1233 break;
1234 case STGM_SHARE_DENY_WRITE:
1235 sharing = FILE_SHARE_READ;
1236 break;
1237 case STGM_SHARE_EXCLUSIVE:
1238 sharing = 0;
1239 break;
1242 file = CreateFileW(file_name, access, sharing, NULL, OPEN_EXISTING, 0, 0);
1243 if (file == INVALID_HANDLE_VALUE)
1245 TRACE("Failed to open %s, error %u\n", debugstr_w(file_name), GetLastError());
1246 return HRESULT_FROM_WIN32(GetLastError());
1249 size = GetFileSize(file, NULL);
1251 mapping = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, 0);
1252 if (!mapping)
1254 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name), GetLastError());
1255 CloseHandle(file);
1256 return HRESULT_FROM_WIN32(GetLastError());
1259 data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
1260 if (data)
1262 hr = load_job_data(This, data, size);
1263 if (hr == S_OK) This->is_dirty = FALSE;
1264 UnmapViewOfFile(data);
1266 else
1267 hr = HRESULT_FROM_WIN32(GetLastError());
1269 CloseHandle(mapping);
1270 CloseHandle(file);
1272 return hr;
1275 static BOOL write_signature(HANDLE hfile)
1277 struct
1279 USHORT SignatureVersion;
1280 USHORT ClientVersion;
1281 BYTE md5[64];
1282 } signature;
1283 DWORD size;
1285 signature.SignatureVersion = 0x0001;
1286 signature.ClientVersion = 0x0001;
1287 memset(&signature.md5, 0, sizeof(signature.md5));
1289 return WriteFile(hfile, &signature, sizeof(signature), &size, NULL);
1292 static BOOL write_reserved_data(HANDLE hfile)
1294 static const struct
1296 USHORT size;
1297 BYTE data[8];
1298 } user = { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1299 DWORD size;
1301 return WriteFile(hfile, &user, sizeof(user), &size, NULL);
1304 static BOOL write_user_data(HANDLE hfile, BYTE *data, WORD data_size)
1306 DWORD size;
1308 if (!WriteFile(hfile, &data_size, sizeof(data_size), &size, NULL))
1309 return FALSE;
1311 if (!data_size) return TRUE;
1313 return WriteFile(hfile, data, data_size, &size, NULL);
1316 static HRESULT write_triggers(TaskImpl *This, HANDLE hfile)
1318 WORD count, i, idx = 0xffff;
1319 DWORD size;
1320 HRESULT hr = S_OK;
1321 ITaskTrigger *trigger;
1323 count = This->trigger_count;
1325 /* Windows saves a .job with at least 1 trigger */
1326 if (!count)
1328 hr = ITask_CreateTrigger(&This->ITask_iface, &idx, &trigger);
1329 if (hr != S_OK) return hr;
1330 ITaskTrigger_Release(trigger);
1332 count = 1;
1335 if (WriteFile(hfile, &count, sizeof(count), &size, NULL))
1337 for (i = 0; i < count; i++)
1339 if (!WriteFile(hfile, &This->trigger[i], sizeof(This->trigger[0]), &size, NULL))
1341 hr = HRESULT_FROM_WIN32(GetLastError());
1342 break;
1346 else
1347 hr = HRESULT_FROM_WIN32(GetLastError());
1349 if (idx != 0xffff)
1350 ITask_DeleteTrigger(&This->ITask_iface, idx);
1352 return hr;
1355 static BOOL write_unicode_string(HANDLE hfile, const WCHAR *str)
1357 USHORT count;
1358 DWORD size;
1360 count = str ? (lstrlenW(str) + 1) : 0;
1361 if (!WriteFile(hfile, &count, sizeof(count), &size, NULL))
1362 return FALSE;
1364 if (!str) return TRUE;
1366 count *= sizeof(WCHAR);
1367 return WriteFile(hfile, str, count, &size, NULL);
1370 static HRESULT WINAPI MSTASK_IPersistFile_Save(IPersistFile *iface, LPCOLESTR task_name, BOOL remember)
1372 static WCHAR authorW[] = { 'W','i','n','e',0 };
1373 static WCHAR commentW[] = { 'C','r','e','a','t','e','d',' ','b','y',' ','W','i','n','e',0 };
1374 FIXDLEN_DATA fixed;
1375 WORD word, user_data_size = 0;
1376 HANDLE hfile;
1377 DWORD size, ver, disposition, try;
1378 TaskImpl *This = impl_from_IPersistFile(iface);
1379 ITask *task = &This->ITask_iface;
1380 LPWSTR appname = NULL, params = NULL, workdir = NULL, creator = NULL, comment = NULL;
1381 BYTE *user_data = NULL;
1382 HRESULT hr;
1384 TRACE("(%p, %s, %d)\n", iface, debugstr_w(task_name), remember);
1386 disposition = task_name ? CREATE_NEW : OPEN_ALWAYS;
1388 if (!task_name)
1390 task_name = This->task_name;
1391 remember = FALSE;
1394 ITask_GetComment(task, &comment);
1395 if (!comment) comment = commentW;
1396 ITask_GetCreator(task, &creator);
1397 if (!creator) creator = authorW;
1398 ITask_GetApplicationName(task, &appname);
1399 ITask_GetParameters(task, &params);
1400 ITask_GetWorkingDirectory(task, &workdir);
1401 ITask_GetWorkItemData(task, &user_data_size, &user_data);
1403 ver = GetVersion();
1404 fixed.product_version = MAKEWORD(ver >> 8, ver);
1405 fixed.file_version = 0x0001;
1406 CoCreateGuid(&fixed.uuid);
1407 fixed.name_size_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1408 fixed.trigger_offset = sizeof(fixed) + sizeof(USHORT); /* FIXDLEN_DATA + Instance Count */
1409 fixed.trigger_offset += sizeof(USHORT); /* Application Name */
1410 if (appname)
1411 fixed.trigger_offset += (lstrlenW(appname) + 1) * sizeof(WCHAR);
1412 fixed.trigger_offset += sizeof(USHORT); /* Parameters */
1413 if (params)
1414 fixed.trigger_offset += (lstrlenW(params) + 1) * sizeof(WCHAR);
1415 fixed.trigger_offset += sizeof(USHORT); /* Working Directory */
1416 if (workdir)
1417 fixed.trigger_offset += (lstrlenW(workdir) + 1) * sizeof(WCHAR);
1418 fixed.trigger_offset += sizeof(USHORT); /* Author */
1419 if (creator)
1420 fixed.trigger_offset += (lstrlenW(creator) + 1) * sizeof(WCHAR);
1421 fixed.trigger_offset += sizeof(USHORT); /* Comment */
1422 if (comment)
1423 fixed.trigger_offset += (lstrlenW(comment) + 1) * sizeof(WCHAR);
1424 fixed.trigger_offset += sizeof(USHORT) + user_data_size; /* User Data */
1425 fixed.trigger_offset += 10; /* Reserved Data */
1427 fixed.error_retry_count = 0;
1428 fixed.error_retry_interval = 0;
1429 fixed.idle_wait = This->idle_minutes;
1430 fixed.idle_deadline = This->deadline_minutes;
1431 fixed.priority = This->priority;
1432 fixed.maximum_runtime = This->maxRunTime;
1433 fixed.exit_code = This->exit_code;
1434 if (This->status == SCHED_S_TASK_NOT_SCHEDULED && This->trigger_count)
1435 This->status = SCHED_S_TASK_HAS_NOT_RUN;
1436 fixed.status = This->status;
1437 fixed.flags = This->flags;
1438 memset(&fixed.last_runtime, 0, sizeof(fixed.last_runtime));
1440 try = 1;
1441 for (;;)
1443 hfile = CreateFileW(task_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, disposition, 0, 0);
1444 if (hfile != INVALID_HANDLE_VALUE) break;
1446 if (try++ >= 3) return HRESULT_FROM_WIN32(GetLastError());
1447 Sleep(100);
1450 if (!WriteFile(hfile, &fixed, sizeof(fixed), &size, NULL))
1452 hr = HRESULT_FROM_WIN32(GetLastError());
1453 goto failed;
1456 /* Instance Count: don't touch it in the client */
1457 if (SetFilePointer(hfile, sizeof(word), NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
1459 hr = HRESULT_FROM_WIN32(GetLastError());
1460 goto failed;
1462 /* Application Name */
1463 if (!write_unicode_string(hfile, appname))
1465 hr = HRESULT_FROM_WIN32(GetLastError());
1466 goto failed;
1468 /* Parameters */
1469 if (!write_unicode_string(hfile, params))
1471 hr = HRESULT_FROM_WIN32(GetLastError());
1472 goto failed;
1474 /* Working Directory */
1475 if (!write_unicode_string(hfile, workdir))
1477 hr = HRESULT_FROM_WIN32(GetLastError());
1478 goto failed;
1480 /* Author */
1481 if (!write_unicode_string(hfile, creator))
1483 hr = HRESULT_FROM_WIN32(GetLastError());
1484 goto failed;
1486 /* Comment */
1487 if (!write_unicode_string(hfile, comment))
1489 hr = HRESULT_FROM_WIN32(GetLastError());
1490 goto failed;
1493 /* User Data */
1494 if (!write_user_data(hfile, user_data, user_data_size))
1496 hr = HRESULT_FROM_WIN32(GetLastError());
1497 goto failed;
1500 /* Reserved Data */
1501 if (!write_reserved_data(hfile))
1503 hr = HRESULT_FROM_WIN32(GetLastError());
1504 goto failed;
1507 /* Triggers */
1508 hr = write_triggers(This, hfile);
1509 if (hr != S_OK)
1510 goto failed;
1512 /* Signature */
1513 if (!write_signature(hfile))
1515 hr = HRESULT_FROM_WIN32(GetLastError());
1516 goto failed;
1519 hr = S_OK;
1520 This->is_dirty = FALSE;
1522 failed:
1523 CoTaskMemFree(appname);
1524 CoTaskMemFree(params);
1525 CoTaskMemFree(workdir);
1526 if (creator != authorW)
1527 CoTaskMemFree(creator);
1528 if (comment != commentW)
1529 CoTaskMemFree(comment);
1530 CoTaskMemFree(user_data);
1532 CloseHandle(hfile);
1533 if (hr != S_OK)
1534 DeleteFileW(task_name);
1535 else if (remember)
1537 heap_free(This->task_name);
1538 This->task_name = heap_strdupW(task_name);
1540 return hr;
1543 static HRESULT WINAPI MSTASK_IPersistFile_SaveCompleted(
1544 IPersistFile* iface,
1545 LPCOLESTR pszFileName)
1547 FIXME("(%p, %p): stub\n", iface, pszFileName);
1548 return E_NOTIMPL;
1551 static HRESULT WINAPI MSTASK_IPersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *file_name)
1553 TaskImpl *This = impl_from_IPersistFile(iface);
1555 TRACE("(%p, %p)\n", iface, file_name);
1557 *file_name = CoTaskMemAlloc((strlenW(This->task_name) + 1) * sizeof(WCHAR));
1558 if (!*file_name) return E_OUTOFMEMORY;
1560 strcpyW(*file_name, This->task_name);
1561 return S_OK;
1564 static const ITaskVtbl MSTASK_ITaskVtbl =
1566 MSTASK_ITask_QueryInterface,
1567 MSTASK_ITask_AddRef,
1568 MSTASK_ITask_Release,
1569 MSTASK_ITask_CreateTrigger,
1570 MSTASK_ITask_DeleteTrigger,
1571 MSTASK_ITask_GetTriggerCount,
1572 MSTASK_ITask_GetTrigger,
1573 MSTASK_ITask_GetTriggerString,
1574 MSTASK_ITask_GetRunTimes,
1575 MSTASK_ITask_GetNextRunTime,
1576 MSTASK_ITask_SetIdleWait,
1577 MSTASK_ITask_GetIdleWait,
1578 MSTASK_ITask_Run,
1579 MSTASK_ITask_Terminate,
1580 MSTASK_ITask_EditWorkItem,
1581 MSTASK_ITask_GetMostRecentRunTime,
1582 MSTASK_ITask_GetStatus,
1583 MSTASK_ITask_GetExitCode,
1584 MSTASK_ITask_SetComment,
1585 MSTASK_ITask_GetComment,
1586 MSTASK_ITask_SetCreator,
1587 MSTASK_ITask_GetCreator,
1588 MSTASK_ITask_SetWorkItemData,
1589 MSTASK_ITask_GetWorkItemData,
1590 MSTASK_ITask_SetErrorRetryCount,
1591 MSTASK_ITask_GetErrorRetryCount,
1592 MSTASK_ITask_SetErrorRetryInterval,
1593 MSTASK_ITask_GetErrorRetryInterval,
1594 MSTASK_ITask_SetFlags,
1595 MSTASK_ITask_GetFlags,
1596 MSTASK_ITask_SetAccountInformation,
1597 MSTASK_ITask_GetAccountInformation,
1598 MSTASK_ITask_SetApplicationName,
1599 MSTASK_ITask_GetApplicationName,
1600 MSTASK_ITask_SetParameters,
1601 MSTASK_ITask_GetParameters,
1602 MSTASK_ITask_SetWorkingDirectory,
1603 MSTASK_ITask_GetWorkingDirectory,
1604 MSTASK_ITask_SetPriority,
1605 MSTASK_ITask_GetPriority,
1606 MSTASK_ITask_SetTaskFlags,
1607 MSTASK_ITask_GetTaskFlags,
1608 MSTASK_ITask_SetMaxRunTime,
1609 MSTASK_ITask_GetMaxRunTime
1612 static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
1614 MSTASK_IPersistFile_QueryInterface,
1615 MSTASK_IPersistFile_AddRef,
1616 MSTASK_IPersistFile_Release,
1617 MSTASK_IPersistFile_GetClassID,
1618 MSTASK_IPersistFile_IsDirty,
1619 MSTASK_IPersistFile_Load,
1620 MSTASK_IPersistFile_Save,
1621 MSTASK_IPersistFile_SaveCompleted,
1622 MSTASK_IPersistFile_GetCurFile
1625 HRESULT TaskConstructor(ITaskService *service, const WCHAR *name, ITask **task)
1627 static const WCHAR tasksW[] = { '\\','T','a','s','k','s','\\',0 };
1628 static const WCHAR jobW[] = { '.','j','o','b',0 };
1629 TaskImpl *This;
1630 WCHAR task_name[MAX_PATH];
1631 ITaskDefinition *taskdef;
1632 IActionCollection *actions;
1633 HRESULT hr;
1635 TRACE("(%s, %p)\n", debugstr_w(name), task);
1637 if (strchrW(name, '.')) return E_INVALIDARG;
1639 GetWindowsDirectoryW(task_name, MAX_PATH);
1640 lstrcatW(task_name, tasksW);
1641 lstrcatW(task_name, name);
1642 lstrcatW(task_name, jobW);
1644 hr = ITaskService_NewTask(service, 0, &taskdef);
1645 if (hr != S_OK) return hr;
1647 This = heap_alloc(sizeof(*This));
1648 if (!This)
1650 ITaskDefinition_Release(taskdef);
1651 return E_OUTOFMEMORY;
1654 This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
1655 This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
1656 This->ref = 1;
1657 This->task = taskdef;
1658 This->task_name = heap_strdupW(task_name);
1659 This->flags = 0;
1660 This->status = SCHED_S_TASK_NOT_SCHEDULED;
1661 This->exit_code = 0;
1662 This->idle_minutes = 10;
1663 This->deadline_minutes = 60;
1664 This->priority = NORMAL_PRIORITY_CLASS;
1665 This->accountName = NULL;
1666 This->trigger_count = 0;
1667 This->trigger = NULL;
1668 This->is_dirty = FALSE;
1669 This->instance_count = 0;
1671 /* Default time is 3 days = 259200000 ms */
1672 This->maxRunTime = 259200000;
1674 hr = ITaskDefinition_get_Actions(This->task, &actions);
1675 if (hr == S_OK)
1677 hr = IActionCollection_Create(actions, TASK_ACTION_EXEC, (IAction **)&This->action);
1678 IActionCollection_Release(actions);
1679 if (hr == S_OK)
1681 *task = &This->ITask_iface;
1682 InterlockedIncrement(&dll_ref);
1683 return S_OK;
1687 ITaskDefinition_Release(This->task);
1688 ITask_Release(&This->ITask_iface);
1689 return hr;