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
30 #include "mstask_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mstask
);
37 USHORT product_version
;
40 USHORT name_size_offset
;
41 USHORT trigger_offset
;
42 USHORT error_retry_count
;
43 USHORT error_retry_interval
;
51 SYSTEMTIME last_runtime
;
57 IPersistFile IPersistFile_iface
;
59 ITaskDefinition
*task
;
66 WORD idle_minutes
, deadline_minutes
;
67 DWORD flags
, priority
, maxRunTime
, exit_code
;
68 SYSTEMTIME last_runtime
;
71 TASK_TRIGGER
*trigger
;
73 USHORT instance_count
;
76 static inline TaskImpl
*impl_from_ITask(ITask
*iface
)
78 return CONTAINING_RECORD(iface
, TaskImpl
, ITask_iface
);
81 static inline TaskImpl
*impl_from_IPersistFile( IPersistFile
*iface
)
83 return CONTAINING_RECORD(iface
, TaskImpl
, IPersistFile_iface
);
86 static void TaskDestructor(TaskImpl
*This
)
90 IExecAction_Release(This
->action
);
91 ITaskDefinition_Release(This
->task
);
93 free(This
->task_name
);
94 free(This
->accountName
);
97 InterlockedDecrement(&dll_ref
);
100 static HRESULT WINAPI
MSTASK_ITask_QueryInterface(
105 TaskImpl
* This
= impl_from_ITask(iface
);
107 TRACE("IID: %s\n", debugstr_guid(riid
));
108 if (ppvObject
== NULL
)
111 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
112 IsEqualGUID(riid
, &IID_ITask
))
114 *ppvObject
= &This
->ITask_iface
;
118 else if (IsEqualGUID(riid
, &IID_IPersistFile
))
120 *ppvObject
= &This
->IPersistFile_iface
;
125 WARN("Unknown interface: %s\n", debugstr_guid(riid
));
127 return E_NOINTERFACE
;
130 static ULONG WINAPI
MSTASK_ITask_AddRef(
133 TaskImpl
*This
= impl_from_ITask(iface
);
136 ref
= InterlockedIncrement(&This
->ref
);
140 static ULONG WINAPI
MSTASK_ITask_Release(
143 TaskImpl
* This
= impl_from_ITask(iface
);
146 ref
= InterlockedDecrement(&This
->ref
);
148 TaskDestructor(This
);
152 HRESULT
task_set_trigger(ITask
*task
, WORD idx
, const TASK_TRIGGER
*src
)
154 TaskImpl
*This
= impl_from_ITask(task
);
155 TIME_FIELDS field_time
;
156 LARGE_INTEGER sys_time
;
159 TRACE("(%p, %u, %p)\n", task
, idx
, src
);
161 if (idx
>= This
->trigger_count
)
164 /* Verify valid structure size */
165 if (src
->cbTriggerSize
!= sizeof(*src
))
167 dst
.cbTriggerSize
= src
->cbTriggerSize
;
169 /* Reserved field must be zero */
172 /* Verify and set valid start date and time */
173 memset(&field_time
, 0, sizeof(field_time
));
174 field_time
.Year
= src
->wBeginYear
;
175 field_time
.Month
= src
->wBeginMonth
;
176 field_time
.Day
= src
->wBeginDay
;
177 field_time
.Hour
= src
->wStartHour
;
178 field_time
.Minute
= src
->wStartMinute
;
179 if (!RtlTimeFieldsToTime(&field_time
, &sys_time
))
181 dst
.wBeginYear
= src
->wBeginYear
;
182 dst
.wBeginMonth
= src
->wBeginMonth
;
183 dst
.wBeginDay
= src
->wBeginDay
;
184 dst
.wStartHour
= src
->wStartHour
;
185 dst
.wStartMinute
= src
->wStartMinute
;
187 /* Verify valid end date if TASK_TRIGGER_FLAG_HAS_END_DATE flag is set */
188 if (src
->rgFlags
& TASK_TRIGGER_FLAG_HAS_END_DATE
)
190 memset(&field_time
, 0, sizeof(field_time
));
191 field_time
.Year
= src
->wEndYear
;
192 field_time
.Month
= src
->wEndMonth
;
193 field_time
.Day
= src
->wEndDay
;
194 if (!RtlTimeFieldsToTime(&field_time
, &sys_time
))
198 /* Set valid end date independent of TASK_TRIGGER_FLAG_HAS_END_DATE flag */
199 dst
.wEndYear
= src
->wEndYear
;
200 dst
.wEndMonth
= src
->wEndMonth
;
201 dst
.wEndDay
= src
->wEndDay
;
203 /* Verify duration and interval pair */
204 if (src
->MinutesDuration
<= src
->MinutesInterval
&& src
->MinutesInterval
> 0)
206 dst
.MinutesDuration
= src
->MinutesDuration
;
207 dst
.MinutesInterval
= src
->MinutesInterval
;
209 /* Copy over flags */
210 dst
.rgFlags
= src
->rgFlags
;
212 /* Set TriggerType dependent fields of Type union */
213 dst
.TriggerType
= src
->TriggerType
;
214 switch (src
->TriggerType
)
216 case TASK_TIME_TRIGGER_DAILY
:
217 dst
.Type
.Daily
.DaysInterval
= src
->Type
.Daily
.DaysInterval
;
219 case TASK_TIME_TRIGGER_WEEKLY
:
220 dst
.Type
.Weekly
.WeeksInterval
= src
->Type
.Weekly
.WeeksInterval
;
221 dst
.Type
.Weekly
.rgfDaysOfTheWeek
= src
->Type
.Weekly
.rgfDaysOfTheWeek
;
223 case TASK_TIME_TRIGGER_MONTHLYDATE
:
224 dst
.Type
.MonthlyDate
.rgfDays
= src
->Type
.MonthlyDate
.rgfDays
;
225 dst
.Type
.MonthlyDate
.rgfMonths
= src
->Type
.MonthlyDate
.rgfMonths
;
227 case TASK_TIME_TRIGGER_MONTHLYDOW
:
228 dst
.Type
.MonthlyDOW
.wWhichWeek
= src
->Type
.MonthlyDOW
.wWhichWeek
;
229 dst
.Type
.MonthlyDOW
.rgfDaysOfTheWeek
= src
->Type
.MonthlyDOW
.rgfDaysOfTheWeek
;
230 dst
.Type
.MonthlyDOW
.rgfMonths
= src
->Type
.MonthlyDOW
.rgfMonths
;
232 case TASK_TIME_TRIGGER_ONCE
:
233 case TASK_EVENT_TRIGGER_ON_IDLE
:
234 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART
:
235 case TASK_EVENT_TRIGGER_AT_LOGON
:
237 dst
.Type
= src
->Type
;
241 /* Reserved field must be zero */
244 /* wRandomMinutesInterval not currently used and is initialized to zero */
245 dst
.wRandomMinutesInterval
= 0;
247 This
->trigger
[idx
] = dst
;
252 HRESULT
task_get_trigger(ITask
*task
, WORD idx
, TASK_TRIGGER
*dst
)
254 TaskImpl
*This
= impl_from_ITask(task
);
257 TRACE("(%p, %u, %p)\n", task
, idx
, dst
);
259 if (idx
>= This
->trigger_count
)
260 return SCHED_E_TRIGGER_NOT_FOUND
;
262 src
= &This
->trigger
[idx
];
264 /* Native implementation doesn't verify equivalent cbTriggerSize fields */
266 /* Copy relevant fields of the structure */
267 dst
->cbTriggerSize
= src
->cbTriggerSize
;
269 dst
->wBeginYear
= src
->wBeginYear
;
270 dst
->wBeginMonth
= src
->wBeginMonth
;
271 dst
->wBeginDay
= src
->wBeginDay
;
272 dst
->wEndYear
= src
->wEndYear
;
273 dst
->wEndMonth
= src
->wEndMonth
;
274 dst
->wEndDay
= src
->wEndDay
;
275 dst
->wStartHour
= src
->wStartHour
;
276 dst
->wStartMinute
= src
->wStartMinute
;
277 dst
->MinutesDuration
= src
->MinutesDuration
;
278 dst
->MinutesInterval
= src
->MinutesInterval
;
279 dst
->rgFlags
= src
->rgFlags
;
280 dst
->TriggerType
= src
->TriggerType
;
281 switch (src
->TriggerType
)
283 case TASK_TIME_TRIGGER_DAILY
:
284 dst
->Type
.Daily
.DaysInterval
= src
->Type
.Daily
.DaysInterval
;
286 case TASK_TIME_TRIGGER_WEEKLY
:
287 dst
->Type
.Weekly
.WeeksInterval
= src
->Type
.Weekly
.WeeksInterval
;
288 dst
->Type
.Weekly
.rgfDaysOfTheWeek
= src
->Type
.Weekly
.rgfDaysOfTheWeek
;
290 case TASK_TIME_TRIGGER_MONTHLYDATE
:
291 dst
->Type
.MonthlyDate
.rgfDays
= src
->Type
.MonthlyDate
.rgfDays
;
292 dst
->Type
.MonthlyDate
.rgfMonths
= src
->Type
.MonthlyDate
.rgfMonths
;
294 case TASK_TIME_TRIGGER_MONTHLYDOW
:
295 dst
->Type
.MonthlyDOW
.wWhichWeek
= src
->Type
.MonthlyDOW
.wWhichWeek
;
296 dst
->Type
.MonthlyDOW
.rgfDaysOfTheWeek
= src
->Type
.MonthlyDOW
.rgfDaysOfTheWeek
;
297 dst
->Type
.MonthlyDOW
.rgfMonths
= src
->Type
.MonthlyDOW
.rgfMonths
;
299 case TASK_TIME_TRIGGER_ONCE
:
300 case TASK_EVENT_TRIGGER_ON_IDLE
:
301 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART
:
302 case TASK_EVENT_TRIGGER_AT_LOGON
:
307 dst
->wRandomMinutesInterval
= 0;
312 static HRESULT WINAPI
MSTASK_ITask_CreateTrigger(ITask
*iface
, WORD
*idx
, ITaskTrigger
**task_trigger
)
314 TaskImpl
*This
= impl_from_ITask(iface
);
315 TASK_TRIGGER
*new_trigger
;
319 TRACE("(%p, %p, %p)\n", iface
, idx
, task_trigger
);
321 hr
= TaskTriggerConstructor(iface
, This
->trigger_count
, task_trigger
);
322 if (hr
!= S_OK
) return hr
;
325 new_trigger
= realloc(This
->trigger
, sizeof(This
->trigger
[0]) * (This
->trigger_count
+ 1));
327 new_trigger
= malloc(sizeof(This
->trigger
[0]));
330 ITaskTrigger_Release(*task_trigger
);
331 return E_OUTOFMEMORY
;
334 This
->trigger
= new_trigger
;
336 new_trigger
= &This
->trigger
[This
->trigger_count
];
338 /* Most fields default to zero. Initialize other fields to default values. */
339 memset(new_trigger
, 0, sizeof(*new_trigger
));
341 new_trigger
->cbTriggerSize
= sizeof(*new_trigger
);
342 new_trigger
->wBeginYear
= time
.wYear
;
343 new_trigger
->wBeginMonth
= time
.wMonth
;
344 new_trigger
->wBeginDay
= time
.wDay
;
345 new_trigger
->wStartHour
= time
.wHour
;
346 new_trigger
->wStartMinute
= time
.wMinute
;
347 new_trigger
->rgFlags
= TASK_TRIGGER_FLAG_DISABLED
;
348 new_trigger
->TriggerType
= TASK_TIME_TRIGGER_DAILY
;
349 new_trigger
->Type
.Daily
.DaysInterval
= 1;
351 *idx
= This
->trigger_count
++;
356 static HRESULT WINAPI
MSTASK_ITask_DeleteTrigger(ITask
*iface
, WORD idx
)
358 TaskImpl
*This
= impl_from_ITask(iface
);
360 TRACE("(%p, %u)\n", iface
, idx
);
362 if (idx
>= This
->trigger_count
)
363 return SCHED_E_TRIGGER_NOT_FOUND
;
365 This
->trigger_count
--;
366 memmove(&This
->trigger
[idx
], &This
->trigger
[idx
+ 1], (This
->trigger_count
- idx
) * sizeof(This
->trigger
[0]));
367 /* this shouldn't fail in practice since we're shrinking the memory block */
368 This
->trigger
= realloc(This
->trigger
, sizeof(This
->trigger
[0]) * This
->trigger_count
);
373 static HRESULT WINAPI
MSTASK_ITask_GetTriggerCount(ITask
*iface
, WORD
*count
)
375 TaskImpl
*This
= impl_from_ITask(iface
);
377 TRACE("(%p, %p)\n", iface
, count
);
379 *count
= This
->trigger_count
;
383 static HRESULT WINAPI
MSTASK_ITask_GetTrigger(ITask
*iface
, WORD idx
, ITaskTrigger
**trigger
)
385 TaskImpl
*This
= impl_from_ITask(iface
);
387 TRACE("(%p, %u, %p)\n", iface
, idx
, trigger
);
389 if (idx
>= This
->trigger_count
)
390 return SCHED_E_TRIGGER_NOT_FOUND
;
392 return TaskTriggerConstructor(iface
, idx
, trigger
);
395 static HRESULT WINAPI
MSTASK_ITask_GetTriggerString(
398 LPWSTR
*ppwszTrigger
)
400 FIXME("(%p, %d, %p): stub\n", iface
, iTrigger
, ppwszTrigger
);
404 static HRESULT WINAPI
MSTASK_ITask_GetRunTimes(
406 const LPSYSTEMTIME pstBegin
,
407 const LPSYSTEMTIME pstEnd
,
409 LPSYSTEMTIME
*rgstTaskTimes
)
411 FIXME("(%p, %p, %p, %p, %p): stub\n", iface
, pstBegin
, pstEnd
, pCount
,
416 static void get_begin_time(const TASK_TRIGGER
*trigger
, FILETIME
*ft
)
420 st
.wYear
= trigger
->wBeginYear
;
421 st
.wMonth
= trigger
->wBeginMonth
;
422 st
.wDay
= trigger
->wBeginDay
;
427 st
.wMilliseconds
= 0;
428 SystemTimeToFileTime(&st
, ft
);
431 static void get_end_time(const TASK_TRIGGER
*trigger
, FILETIME
*ft
)
435 if (!(trigger
->rgFlags
& TASK_TRIGGER_FLAG_HAS_END_DATE
))
437 ft
->dwHighDateTime
= ~0u;
438 ft
->dwLowDateTime
= ~0u;
442 st
.wYear
= trigger
->wEndYear
;
443 st
.wMonth
= trigger
->wEndMonth
;
444 st
.wDay
= trigger
->wEndDay
;
449 st
.wMilliseconds
= 0;
450 SystemTimeToFileTime(&st
, ft
);
453 static void filetime_add_ms(FILETIME
*ft
, ULONGLONG ms
)
459 } *ftll
= (union u_ftll
*)ft
;
461 ftll
->ll
+= ms
* (ULONGLONG
)10000;
464 static void filetime_add_hours(FILETIME
*ft
, ULONG hours
)
466 filetime_add_ms(ft
, (ULONGLONG
)hours
* 60 * 60 * 1000);
469 static void filetime_add_days(FILETIME
*ft
, ULONG days
)
471 filetime_add_hours(ft
, (ULONGLONG
)days
* 24);
474 static void filetime_add_weeks(FILETIME
*ft
, ULONG weeks
)
476 filetime_add_days(ft
, (ULONGLONG
)weeks
* 7);
479 static HRESULT WINAPI
MSTASK_ITask_GetNextRunTime(ITask
*iface
, SYSTEMTIME
*rt
)
481 TaskImpl
*This
= impl_from_ITask(iface
);
482 HRESULT hr
= SCHED_S_TASK_NO_VALID_TRIGGERS
;
483 SYSTEMTIME st
, current_st
;
484 FILETIME current_ft
, trigger_ft
, begin_ft
, end_ft
, best_ft
;
485 BOOL have_best_time
= FALSE
;
488 TRACE("(%p, %p)\n", iface
, rt
);
490 if (This
->flags
& TASK_FLAG_DISABLED
)
492 memset(rt
, 0, sizeof(*rt
));
493 return SCHED_S_TASK_DISABLED
;
496 GetLocalTime(¤t_st
);
497 SystemTimeToFileTime(¤t_st
, ¤t_ft
);
499 for (i
= 0; i
< This
->trigger_count
; i
++)
501 if (!(This
->trigger
[i
].rgFlags
& TASK_TRIGGER_FLAG_DISABLED
))
503 get_begin_time(&This
->trigger
[i
], &begin_ft
);
504 if (CompareFileTime(&begin_ft
, ¤t_ft
) < 0)
505 begin_ft
= current_ft
;
507 get_end_time(&This
->trigger
[i
], &end_ft
);
509 switch (This
->trigger
[i
].TriggerType
)
511 case TASK_EVENT_TRIGGER_ON_IDLE
:
512 case TASK_EVENT_TRIGGER_AT_SYSTEMSTART
:
513 case TASK_EVENT_TRIGGER_AT_LOGON
:
514 hr
= SCHED_S_EVENT_TRIGGER
;
517 case TASK_TIME_TRIGGER_ONCE
:
519 st
.wYear
= This
->trigger
[i
].wBeginYear
;
520 st
.wMonth
= This
->trigger
[i
].wBeginMonth
;
521 st
.wDay
= This
->trigger
[i
].wBeginDay
;
522 st
.wHour
= This
->trigger
[i
].wStartHour
;
523 st
.wMinute
= This
->trigger
[i
].wStartMinute
;
525 st
.wMilliseconds
= 0;
526 SystemTimeToFileTime(&st
, &trigger_ft
);
527 if (CompareFileTime(&begin_ft
, &trigger_ft
) <= 0 && CompareFileTime(&trigger_ft
, &end_ft
) < 0)
529 if (!have_best_time
|| CompareFileTime(&trigger_ft
, &best_ft
) < 0)
531 best_ft
= trigger_ft
;
532 have_best_time
= TRUE
;
537 case TASK_TIME_TRIGGER_DAILY
:
538 if (!This
->trigger
[i
].Type
.Daily
.DaysInterval
)
539 break; /* avoid infinite loop */
542 st
.wHour
= This
->trigger
[i
].wStartHour
;
543 st
.wMinute
= This
->trigger
[i
].wStartMinute
;
545 st
.wMilliseconds
= 0;
546 SystemTimeToFileTime(&st
, &trigger_ft
);
547 while (CompareFileTime(&trigger_ft
, &end_ft
) < 0)
549 if (CompareFileTime(&trigger_ft
, &begin_ft
) >= 0)
551 if (!have_best_time
|| CompareFileTime(&trigger_ft
, &best_ft
) < 0)
553 best_ft
= trigger_ft
;
554 have_best_time
= TRUE
;
559 filetime_add_days(&trigger_ft
, This
->trigger
[i
].Type
.Daily
.DaysInterval
);
563 case TASK_TIME_TRIGGER_WEEKLY
:
564 if (!This
->trigger
[i
].Type
.Weekly
.rgfDaysOfTheWeek
)
565 break; /* avoid infinite loop */
568 st
.wHour
= This
->trigger
[i
].wStartHour
;
569 st
.wMinute
= This
->trigger
[i
].wStartMinute
;
571 st
.wMilliseconds
= 0;
572 SystemTimeToFileTime(&st
, &trigger_ft
);
573 while (CompareFileTime(&trigger_ft
, &end_ft
) < 0)
575 FileTimeToSystemTime(&trigger_ft
, &st
);
577 if (CompareFileTime(&trigger_ft
, &begin_ft
) >= 0)
579 if (This
->trigger
[i
].Type
.Weekly
.rgfDaysOfTheWeek
& (1 << st
.wDayOfWeek
))
581 if (!have_best_time
|| CompareFileTime(&trigger_ft
, &best_ft
) < 0)
583 best_ft
= trigger_ft
;
584 have_best_time
= TRUE
;
590 if (st
.wDayOfWeek
== 0 && This
->trigger
[i
].Type
.Weekly
.WeeksInterval
> 1) /* Sunday, goto next week */
591 filetime_add_weeks(&trigger_ft
, This
->trigger
[i
].Type
.Weekly
.WeeksInterval
- 1);
592 else /* check next weekday */
593 filetime_add_days(&trigger_ft
, 1);
598 FIXME("trigger type %u is not handled\n", This
->trigger
[i
].TriggerType
);
606 FileTimeToSystemTime(&best_ft
, rt
);
610 memset(rt
, 0, sizeof(*rt
));
614 static HRESULT WINAPI
MSTASK_ITask_SetIdleWait(
617 WORD wDeadlineMinutes
)
619 FIXME("(%p, %d, %d): stub\n", iface
, wIdleMinutes
, wDeadlineMinutes
);
623 static HRESULT WINAPI
MSTASK_ITask_GetIdleWait(ITask
*iface
, WORD
*idle_minutes
, WORD
*deadline_minutes
)
625 TaskImpl
*This
= impl_from_ITask(iface
);
627 TRACE("(%p, %p, %p): stub\n", iface
, idle_minutes
, deadline_minutes
);
629 *idle_minutes
= This
->idle_minutes
;
630 *deadline_minutes
= This
->deadline_minutes
;
634 static HRESULT WINAPI
MSTASK_ITask_Run(ITask
*iface
)
636 TaskImpl
*This
= impl_from_ITask(iface
);
638 TRACE("(%p)\n", iface
);
640 if (This
->status
== SCHED_S_TASK_NOT_SCHEDULED
)
641 return SCHED_E_TASK_NOT_READY
;
643 This
->flags
|= 0x04000000;
644 return IPersistFile_Save(&This
->IPersistFile_iface
, NULL
, FALSE
);
647 static HRESULT WINAPI
MSTASK_ITask_Terminate(ITask
*iface
)
649 TaskImpl
*This
= impl_from_ITask(iface
);
651 TRACE("(%p)\n", iface
);
653 if (!This
->instance_count
)
654 return SCHED_E_TASK_NOT_RUNNING
;
656 This
->flags
|= 0x08000000;
657 return IPersistFile_Save(&This
->IPersistFile_iface
, NULL
, FALSE
);
660 static HRESULT WINAPI
MSTASK_ITask_EditWorkItem(
665 FIXME("(%p, %p, %ld): stub\n", iface
, hParent
, dwReserved
);
669 static HRESULT WINAPI
MSTASK_ITask_GetMostRecentRunTime(ITask
*iface
, SYSTEMTIME
*st
)
671 TaskImpl
*This
= impl_from_ITask(iface
);
673 TRACE("(%p, %p)\n", iface
, st
);
675 if (This
->status
== SCHED_S_TASK_NOT_SCHEDULED
)
677 memset(st
, 0, sizeof(*st
));
678 return SCHED_S_TASK_HAS_NOT_RUN
;
681 *st
= This
->last_runtime
;
685 static HRESULT WINAPI
MSTASK_ITask_GetStatus(ITask
*iface
, HRESULT
*status
)
687 TaskImpl
*This
= impl_from_ITask(iface
);
689 TRACE("(%p, %p)\n", iface
, status
);
691 *status
= This
->instance_count
? SCHED_S_TASK_RUNNING
: This
->status
;
695 static HRESULT WINAPI
MSTASK_ITask_GetExitCode(ITask
*iface
, DWORD
*exit_code
)
697 TaskImpl
*This
= impl_from_ITask(iface
);
699 TRACE("(%p, %p)\n", iface
, exit_code
);
701 if (This
->status
== SCHED_S_TASK_NOT_SCHEDULED
)
704 return SCHED_S_TASK_HAS_NOT_RUN
;
707 *exit_code
= This
->exit_code
;
711 static HRESULT WINAPI
MSTASK_ITask_SetComment(ITask
*iface
, LPCWSTR comment
)
713 TaskImpl
*This
= impl_from_ITask(iface
);
714 IRegistrationInfo
*info
;
717 TRACE("(%p, %s)\n", iface
, debugstr_w(comment
));
719 if (!comment
|| !comment
[0])
722 hr
= ITaskDefinition_get_RegistrationInfo(This
->task
, &info
);
725 hr
= IRegistrationInfo_put_Description(info
, (BSTR
)comment
);
726 IRegistrationInfo_Release(info
);
727 This
->is_dirty
= TRUE
;
732 static HRESULT WINAPI
MSTASK_ITask_GetComment(ITask
*iface
, LPWSTR
*comment
)
734 TaskImpl
*This
= impl_from_ITask(iface
);
735 IRegistrationInfo
*info
;
740 TRACE("(%p, %p)\n", iface
, comment
);
742 hr
= ITaskDefinition_get_RegistrationInfo(This
->task
, &info
);
743 if (hr
!= S_OK
) return hr
;
745 hr
= IRegistrationInfo_get_Description(info
, &description
);
748 len
= description
? lstrlenW(description
) + 1 : 1;
749 *comment
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
755 lstrcpyW(*comment
, description
);
761 SysFreeString(description
);
764 IRegistrationInfo_Release(info
);
768 static HRESULT WINAPI
MSTASK_ITask_SetCreator(ITask
*iface
, LPCWSTR creator
)
770 TaskImpl
*This
= impl_from_ITask(iface
);
771 IRegistrationInfo
*info
;
774 TRACE("(%p, %s)\n", iface
, debugstr_w(creator
));
776 if (!creator
|| !creator
[0])
779 hr
= ITaskDefinition_get_RegistrationInfo(This
->task
, &info
);
782 hr
= IRegistrationInfo_put_Author(info
, (BSTR
)creator
);
783 IRegistrationInfo_Release(info
);
784 This
->is_dirty
= TRUE
;
789 static HRESULT WINAPI
MSTASK_ITask_GetCreator(ITask
*iface
, LPWSTR
*creator
)
791 TaskImpl
*This
= impl_from_ITask(iface
);
792 IRegistrationInfo
*info
;
797 TRACE("(%p, %p)\n", iface
, creator
);
799 hr
= ITaskDefinition_get_RegistrationInfo(This
->task
, &info
);
800 if (hr
!= S_OK
) return hr
;
802 hr
= IRegistrationInfo_get_Author(info
, &author
);
805 len
= author
? lstrlenW(author
) + 1 : 1;
806 *creator
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
812 lstrcpyW(*creator
, author
);
818 SysFreeString(author
);
821 IRegistrationInfo_Release(info
);
825 static HRESULT WINAPI
MSTASK_ITask_SetWorkItemData(ITask
*iface
, WORD count
, BYTE data
[])
827 TaskImpl
*This
= impl_from_ITask(iface
);
829 TRACE("(%p, %u, %p)\n", iface
, count
, data
);
833 if (!data
) return E_INVALIDARG
;
836 This
->data
= malloc(count
);
837 if (!This
->data
) return E_OUTOFMEMORY
;
838 memcpy(This
->data
, data
, count
);
839 This
->data_count
= count
;
843 if (data
) return E_INVALIDARG
;
847 This
->data_count
= 0;
853 static HRESULT WINAPI
MSTASK_ITask_GetWorkItemData(ITask
*iface
, WORD
*count
, BYTE
**data
)
855 TaskImpl
*This
= impl_from_ITask(iface
);
857 TRACE("(%p, %p, %p)\n", iface
, count
, data
);
866 *data
= CoTaskMemAlloc(This
->data_count
);
867 if (!*data
) return E_OUTOFMEMORY
;
868 memcpy(*data
, This
->data
, This
->data_count
);
869 *count
= This
->data_count
;
875 static HRESULT WINAPI
MSTASK_ITask_SetErrorRetryCount(
879 FIXME("(%p, %d): stub\n", iface
, wRetryCount
);
883 static HRESULT WINAPI
MSTASK_ITask_GetErrorRetryCount(ITask
*iface
, WORD
*count
)
885 TRACE("(%p, %p)\n", iface
, count
);
889 static HRESULT WINAPI
MSTASK_ITask_SetErrorRetryInterval(
893 FIXME("(%p, %d): stub\n", iface
, wRetryInterval
);
897 static HRESULT WINAPI
MSTASK_ITask_GetErrorRetryInterval(ITask
*iface
, WORD
*interval
)
899 TRACE("(%p, %p)\n", iface
, interval
);
903 static HRESULT WINAPI
MSTASK_ITask_SetFlags(ITask
*iface
, DWORD flags
)
905 TaskImpl
*This
= impl_from_ITask(iface
);
907 TRACE("(%p, 0x%08lx)\n", iface
, flags
);
908 This
->flags
&= 0xffff8000;
909 This
->flags
|= flags
& 0x7fff;
910 This
->is_dirty
= TRUE
;
914 static HRESULT WINAPI
MSTASK_ITask_GetFlags(ITask
*iface
, DWORD
*flags
)
916 TaskImpl
*This
= impl_from_ITask(iface
);
918 TRACE("(%p, %p)\n", iface
, flags
);
919 *flags
= LOWORD(This
->flags
);
923 static HRESULT WINAPI
MSTASK_ITask_SetAccountInformation(
925 LPCWSTR pwszAccountName
,
926 LPCWSTR pwszPassword
)
929 TaskImpl
*This
= impl_from_ITask(iface
);
930 LPWSTR tmp_account_name
;
932 TRACE("(%p, %s, %s): partial stub\n", iface
, debugstr_w(pwszAccountName
),
933 debugstr_w(pwszPassword
));
936 FIXME("Partial stub ignores passwords\n");
938 n
= (lstrlenW(pwszAccountName
) + 1);
939 tmp_account_name
= malloc(n
* sizeof(WCHAR
));
940 if (!tmp_account_name
)
941 return E_OUTOFMEMORY
;
942 lstrcpyW(tmp_account_name
, pwszAccountName
);
943 free(This
->accountName
);
944 This
->accountName
= tmp_account_name
;
945 This
->is_dirty
= TRUE
;
949 static HRESULT WINAPI
MSTASK_ITask_GetAccountInformation(
951 LPWSTR
*ppwszAccountName
)
954 TaskImpl
*This
= impl_from_ITask(iface
);
956 TRACE("(%p, %p): partial stub\n", iface
, ppwszAccountName
);
958 /* This implements the WinXP behavior when accountName has not yet
959 * set. Win2K behaves differently, returning SCHED_E_CANNOT_OPEN_TASK */
960 if (!This
->accountName
)
961 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
963 n
= (lstrlenW(This
->accountName
) + 1);
964 *ppwszAccountName
= CoTaskMemAlloc(n
* sizeof(WCHAR
));
965 if (!*ppwszAccountName
)
966 return E_OUTOFMEMORY
;
967 lstrcpyW(*ppwszAccountName
, This
->accountName
);
971 static HRESULT WINAPI
MSTASK_ITask_SetApplicationName(ITask
*iface
, LPCWSTR appname
)
973 TaskImpl
*This
= impl_from_ITask(iface
);
977 TRACE("(%p, %s)\n", iface
, debugstr_w(appname
));
979 /* Empty application name */
980 if (!appname
|| !appname
[0])
981 return IExecAction_put_Path(This
->action
, NULL
);
983 /* Attempt to set pwszApplicationName to a path resolved application name */
984 len
= SearchPathW(NULL
, appname
, NULL
, 0, NULL
, NULL
);
989 tmp_name
= malloc(len
* sizeof(WCHAR
));
991 return E_OUTOFMEMORY
;
992 len
= SearchPathW(NULL
, appname
, NULL
, len
, tmp_name
, NULL
);
995 hr
= IExecAction_put_Path(This
->action
, tmp_name
);
996 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
999 hr
= HRESULT_FROM_WIN32(GetLastError());
1005 /* If unable to path resolve name, simply set to appname */
1006 hr
= IExecAction_put_Path(This
->action
, (BSTR
)appname
);
1007 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
1011 static HRESULT WINAPI
MSTASK_ITask_GetApplicationName(ITask
*iface
, LPWSTR
*appname
)
1013 TaskImpl
*This
= impl_from_ITask(iface
);
1018 TRACE("(%p, %p)\n", iface
, appname
);
1020 hr
= IExecAction_get_Path(This
->action
, &path
);
1021 if (hr
!= S_OK
) return hr
;
1023 len
= path
? lstrlenW(path
) + 1 : 1;
1024 *appname
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
1030 lstrcpyW(*appname
, path
);
1036 SysFreeString(path
);
1040 static HRESULT WINAPI
MSTASK_ITask_SetParameters(ITask
*iface
, LPCWSTR params
)
1042 TaskImpl
*This
= impl_from_ITask(iface
);
1045 TRACE("(%p, %s)\n", iface
, debugstr_w(params
));
1047 /* Empty parameter list */
1048 if (!params
|| !params
[0])
1051 hr
= IExecAction_put_Arguments(This
->action
, (BSTR
)params
);
1052 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
1056 static HRESULT WINAPI
MSTASK_ITask_GetParameters(ITask
*iface
, LPWSTR
*params
)
1058 TaskImpl
*This
= impl_from_ITask(iface
);
1063 TRACE("(%p, %p)\n", iface
, params
);
1065 hr
= IExecAction_get_Arguments(This
->action
, &args
);
1066 if (hr
!= S_OK
) return hr
;
1068 len
= args
? lstrlenW(args
) + 1 : 1;
1069 *params
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
1075 lstrcpyW(*params
, args
);
1081 SysFreeString(args
);
1085 static HRESULT WINAPI
MSTASK_ITask_SetWorkingDirectory(ITask
* iface
, LPCWSTR workdir
)
1087 TaskImpl
*This
= impl_from_ITask(iface
);
1090 TRACE("(%p, %s)\n", iface
, debugstr_w(workdir
));
1092 if (!workdir
|| !workdir
[0])
1095 hr
= IExecAction_put_WorkingDirectory(This
->action
, (BSTR
)workdir
);
1096 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
1100 static HRESULT WINAPI
MSTASK_ITask_GetWorkingDirectory(ITask
*iface
, LPWSTR
*workdir
)
1102 TaskImpl
*This
= impl_from_ITask(iface
);
1107 TRACE("(%p, %p)\n", iface
, workdir
);
1109 hr
= IExecAction_get_WorkingDirectory(This
->action
, &dir
);
1110 if (hr
!= S_OK
) return hr
;
1112 len
= dir
? lstrlenW(dir
) + 1 : 1;
1113 *workdir
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
1119 lstrcpyW(*workdir
, dir
);
1129 static HRESULT WINAPI
MSTASK_ITask_SetPriority(
1133 FIXME("(%p, 0x%08lx): stub\n", iface
, dwPriority
);
1137 static HRESULT WINAPI
MSTASK_ITask_GetPriority(ITask
*iface
, DWORD
*priority
)
1139 TaskImpl
*This
= impl_from_ITask(iface
);
1141 TRACE("(%p, %p)\n", iface
, priority
);
1143 *priority
= This
->priority
;
1147 static HRESULT WINAPI
MSTASK_ITask_SetTaskFlags(
1151 FIXME("(%p, 0x%08lx): stub\n", iface
, dwFlags
);
1155 static HRESULT WINAPI
MSTASK_ITask_GetTaskFlags(ITask
*iface
, DWORD
*flags
)
1157 FIXME("(%p, %p): stub\n", iface
, flags
);
1162 static HRESULT WINAPI
MSTASK_ITask_SetMaxRunTime(
1166 TaskImpl
*This
= impl_from_ITask(iface
);
1168 TRACE("(%p, %ld)\n", iface
, dwMaxRunTime
);
1170 This
->maxRunTime
= dwMaxRunTime
;
1171 This
->is_dirty
= TRUE
;
1175 static HRESULT WINAPI
MSTASK_ITask_GetMaxRunTime(
1177 DWORD
*pdwMaxRunTime
)
1179 TaskImpl
*This
= impl_from_ITask(iface
);
1181 TRACE("(%p, %p)\n", iface
, pdwMaxRunTime
);
1183 *pdwMaxRunTime
= This
->maxRunTime
;
1187 static HRESULT WINAPI
MSTASK_IPersistFile_QueryInterface(
1188 IPersistFile
* iface
,
1192 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1193 TRACE("(%p, %s, %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
1194 return ITask_QueryInterface(&This
->ITask_iface
, riid
, ppvObject
);
1197 static ULONG WINAPI
MSTASK_IPersistFile_AddRef(
1198 IPersistFile
* iface
)
1200 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1201 return ITask_AddRef(&This
->ITask_iface
);
1204 static ULONG WINAPI
MSTASK_IPersistFile_Release(
1205 IPersistFile
* iface
)
1207 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1208 return ITask_Release(&This
->ITask_iface
);
1211 static HRESULT WINAPI
MSTASK_IPersistFile_GetClassID(IPersistFile
*iface
, CLSID
*clsid
)
1213 TRACE("(%p, %p)\n", iface
, clsid
);
1215 *clsid
= CLSID_CTask
;
1219 static HRESULT WINAPI
MSTASK_IPersistFile_IsDirty(IPersistFile
*iface
)
1221 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1222 TRACE("(%p)\n", iface
);
1223 return This
->is_dirty
? S_OK
: S_FALSE
;
1226 static DWORD
load_unicode_strings(ITask
*task
, BYTE
*data
, DWORD limit
)
1228 DWORD i
, data_size
= 0;
1231 for (i
= 0; i
< 5; i
++)
1233 if (limit
< sizeof(USHORT
))
1235 TRACE("invalid string %lu offset\n", i
);
1239 len
= *(USHORT
*)data
;
1240 data
+= sizeof(USHORT
);
1241 data_size
+= sizeof(USHORT
);
1242 limit
-= sizeof(USHORT
);
1243 if (limit
< len
* sizeof(WCHAR
))
1245 TRACE("invalid string %lu size\n", i
);
1249 TRACE("string %lu: %s\n", i
, wine_dbgstr_wn((const WCHAR
*)data
, len
));
1254 ITask_SetApplicationName(task
, (const WCHAR
*)data
);
1257 ITask_SetParameters(task
, (const WCHAR
*)data
);
1260 ITask_SetWorkingDirectory(task
, (const WCHAR
*)data
);
1263 ITask_SetCreator(task
, (const WCHAR
*)data
);
1266 ITask_SetComment(task
, (const WCHAR
*)data
);
1272 data
+= len
* sizeof(WCHAR
);
1273 data_size
+= len
* sizeof(WCHAR
);
1279 static HRESULT
load_job_data(TaskImpl
*This
, BYTE
*data
, DWORD size
)
1281 ITask
*task
= &This
->ITask_iface
;
1283 const FIXDLEN_DATA
*fixed
;
1284 const SYSTEMTIME
*st
;
1285 DWORD unicode_strings_size
, data_size
, triggers_size
;
1286 USHORT trigger_count
, i
;
1287 const USHORT
*signature
;
1288 TASK_TRIGGER
*task_trigger
;
1290 if (size
< sizeof(*fixed
))
1292 TRACE("no space for FIXDLEN_DATA\n");
1293 return SCHED_E_INVALID_TASK
;
1296 fixed
= (const FIXDLEN_DATA
*)data
;
1298 TRACE("product_version %04x\n", fixed
->product_version
);
1299 TRACE("file_version %04x\n", fixed
->file_version
);
1300 TRACE("uuid %s\n", wine_dbgstr_guid(&fixed
->uuid
));
1302 if (fixed
->file_version
!= 0x0001)
1303 return SCHED_E_INVALID_TASK
;
1305 This
->uuid
= fixed
->uuid
;
1307 TRACE("name_size_offset %04x\n", fixed
->name_size_offset
);
1308 TRACE("trigger_offset %04x\n", fixed
->trigger_offset
);
1309 TRACE("error_retry_count %u\n", fixed
->error_retry_count
);
1310 TRACE("error_retry_interval %u\n", fixed
->error_retry_interval
);
1311 TRACE("idle_deadline %u\n", fixed
->idle_deadline
);
1312 This
->deadline_minutes
= fixed
->idle_deadline
;
1313 TRACE("idle_wait %u\n", fixed
->idle_wait
);
1314 This
->idle_minutes
= fixed
->idle_wait
;
1315 TRACE("priority %08x\n", fixed
->priority
);
1316 This
->priority
= fixed
->priority
;
1317 TRACE("maximum_runtime %u\n", fixed
->maximum_runtime
);
1318 This
->maxRunTime
= fixed
->maximum_runtime
;
1319 TRACE("exit_code %#x\n", fixed
->exit_code
);
1320 This
->exit_code
= fixed
->exit_code
;
1321 TRACE("status %08lx\n", fixed
->status
);
1322 This
->status
= fixed
->status
;
1323 TRACE("flags %08x\n", fixed
->flags
);
1324 This
->flags
= fixed
->flags
;
1325 This
->last_runtime
= fixed
->last_runtime
;
1326 st
= &fixed
->last_runtime
;
1327 TRACE("last_runtime %u/%u/%u wday %u %u:%02u:%02u.%03u\n",
1328 st
->wDay
, st
->wMonth
, st
->wYear
, st
->wDayOfWeek
,
1329 st
->wHour
, st
->wMinute
, st
->wSecond
, st
->wMilliseconds
);
1331 /* Instance Count */
1332 if (size
< sizeof(*fixed
) + sizeof(USHORT
))
1334 TRACE("no space for instance count\n");
1335 return SCHED_E_INVALID_TASK
;
1338 This
->instance_count
= *(const USHORT
*)(data
+ sizeof(*fixed
));
1339 TRACE("instance count %u\n", This
->instance_count
);
1341 if (fixed
->name_size_offset
+ sizeof(USHORT
) < size
)
1342 unicode_strings_size
= load_unicode_strings(task
, data
+ fixed
->name_size_offset
, size
- fixed
->name_size_offset
);
1345 TRACE("invalid name_size_offset\n");
1346 return SCHED_E_INVALID_TASK
;
1348 TRACE("unicode strings end at %#lx\n", fixed
->name_size_offset
+ unicode_strings_size
);
1350 if (size
< fixed
->trigger_offset
+ sizeof(USHORT
))
1352 TRACE("no space for triggers count\n");
1353 return SCHED_E_INVALID_TASK
;
1355 trigger_count
= *(const USHORT
*)(data
+ fixed
->trigger_offset
);
1356 TRACE("trigger_count %u\n", trigger_count
);
1357 triggers_size
= size
- fixed
->trigger_offset
- sizeof(USHORT
);
1358 TRACE("triggers_size %lu\n", triggers_size
);
1359 task_trigger
= (TASK_TRIGGER
*)(data
+ fixed
->trigger_offset
+ sizeof(USHORT
));
1361 data
+= fixed
->name_size_offset
+ unicode_strings_size
;
1362 size
-= fixed
->name_size_offset
+ unicode_strings_size
;
1365 if (size
< sizeof(USHORT
))
1367 TRACE("no space for user data size\n");
1368 return SCHED_E_INVALID_TASK
;
1371 data_size
= *(const USHORT
*)data
;
1372 if (size
< sizeof(USHORT
) + data_size
)
1374 TRACE("no space for user data\n");
1375 return SCHED_E_INVALID_TASK
;
1377 TRACE("User Data size %#lx\n", data_size
);
1378 ITask_SetWorkItemData(task
, data_size
, data
+ sizeof(USHORT
));
1380 size
-= sizeof(USHORT
) + data_size
;
1381 data
+= sizeof(USHORT
) + data_size
;
1384 if (size
< sizeof(USHORT
))
1386 TRACE("no space for reserved data size\n");
1387 return SCHED_E_INVALID_TASK
;
1390 data_size
= *(const USHORT
*)data
;
1391 if (size
< sizeof(USHORT
) + data_size
)
1393 TRACE("no space for reserved data\n");
1394 return SCHED_E_INVALID_TASK
;
1396 TRACE("Reserved Data size %#lx\n", data_size
);
1398 size
-= sizeof(USHORT
) + data_size
;
1399 data
+= sizeof(USHORT
) + data_size
;
1402 TRACE("trigger_offset %04x, triggers end at %04Ix\n", fixed
->trigger_offset
,
1403 fixed
->trigger_offset
+ sizeof(USHORT
) + trigger_count
* sizeof(TASK_TRIGGER
));
1405 task_trigger
= (TASK_TRIGGER
*)(data
+ sizeof(USHORT
));
1407 if (trigger_count
* sizeof(TASK_TRIGGER
) > triggers_size
)
1409 TRACE("no space for triggers data\n");
1410 return SCHED_E_INVALID_TASK
;
1413 This
->trigger_count
= 0;
1415 for (i
= 0; i
< trigger_count
; i
++)
1417 ITaskTrigger
*trigger
;
1420 hr
= ITask_CreateTrigger(task
, &idx
, &trigger
);
1421 if (hr
!= S_OK
) return hr
;
1423 hr
= ITaskTrigger_SetTrigger(trigger
, &task_trigger
[i
]);
1424 ITaskTrigger_Release(trigger
);
1427 ITask_DeleteTrigger(task
, idx
);
1432 size
-= sizeof(USHORT
) + trigger_count
* sizeof(TASK_TRIGGER
);
1433 data
+= sizeof(USHORT
) + trigger_count
* sizeof(TASK_TRIGGER
);
1435 if (size
< 2 * sizeof(USHORT
) + 64)
1437 TRACE("no space for signature\n");
1438 return S_OK
; /* signature is optional */
1441 signature
= (const USHORT
*)data
;
1442 TRACE("signature version %04x, client version %04x\n", signature
[0], signature
[1]);
1447 static HRESULT WINAPI
MSTASK_IPersistFile_Load(IPersistFile
*iface
, LPCOLESTR file_name
, DWORD mode
)
1449 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1451 HANDLE file
, mapping
;
1452 DWORD access
, sharing
, size
, try;
1455 TRACE("(%p, %s, 0x%08lx)\n", iface
, debugstr_w(file_name
), mode
);
1457 switch (mode
& 0x000f)
1461 access
= GENERIC_READ
;
1464 case STGM_READWRITE
:
1465 access
= GENERIC_READ
| GENERIC_WRITE
;
1469 switch (mode
& 0x00f0)
1472 case STGM_SHARE_DENY_NONE
:
1473 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1475 case STGM_SHARE_DENY_READ
:
1476 sharing
= FILE_SHARE_WRITE
;
1478 case STGM_SHARE_DENY_WRITE
:
1479 sharing
= FILE_SHARE_READ
;
1481 case STGM_SHARE_EXCLUSIVE
:
1489 file
= CreateFileW(file_name
, access
, sharing
, NULL
, OPEN_EXISTING
, 0, 0);
1490 if (file
!= INVALID_HANDLE_VALUE
) break;
1492 if (GetLastError() != ERROR_SHARING_VIOLATION
|| try++ >= 3)
1494 TRACE("Failed to open %s, error %lu\n", debugstr_w(file_name
), GetLastError());
1495 return HRESULT_FROM_WIN32(GetLastError());
1500 size
= GetFileSize(file
, NULL
);
1502 mapping
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, 0);
1505 TRACE("Failed to create file mapping %s, error %lu\n", debugstr_w(file_name
), GetLastError());
1507 return HRESULT_FROM_WIN32(GetLastError());
1510 data
= MapViewOfFile(mapping
, FILE_MAP_READ
, 0, 0, 0);
1513 hr
= load_job_data(This
, data
, size
);
1514 if (hr
== S_OK
) This
->is_dirty
= FALSE
;
1515 UnmapViewOfFile(data
);
1518 hr
= HRESULT_FROM_WIN32(GetLastError());
1520 CloseHandle(mapping
);
1526 static BOOL
write_signature(HANDLE hfile
)
1530 USHORT SignatureVersion
;
1531 USHORT ClientVersion
;
1536 signature
.SignatureVersion
= 0x0001;
1537 signature
.ClientVersion
= 0x0001;
1538 memset(&signature
.md5
, 0, sizeof(signature
.md5
));
1540 return WriteFile(hfile
, &signature
, sizeof(signature
), &size
, NULL
);
1543 static BOOL
write_reserved_data(HANDLE hfile
)
1549 } user
= { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1552 return WriteFile(hfile
, &user
, sizeof(user
), &size
, NULL
);
1555 static BOOL
write_user_data(HANDLE hfile
, BYTE
*data
, WORD data_size
)
1559 if (!WriteFile(hfile
, &data_size
, sizeof(data_size
), &size
, NULL
))
1562 if (!data_size
) return TRUE
;
1564 return WriteFile(hfile
, data
, data_size
, &size
, NULL
);
1567 static HRESULT
write_triggers(TaskImpl
*This
, HANDLE hfile
)
1569 WORD count
, i
, idx
= 0xffff;
1572 ITaskTrigger
*trigger
;
1574 count
= This
->trigger_count
;
1576 /* Windows saves a .job with at least 1 trigger */
1579 hr
= ITask_CreateTrigger(&This
->ITask_iface
, &idx
, &trigger
);
1580 if (hr
!= S_OK
) return hr
;
1581 ITaskTrigger_Release(trigger
);
1586 if (WriteFile(hfile
, &count
, sizeof(count
), &size
, NULL
))
1588 for (i
= 0; i
< count
; i
++)
1590 if (!WriteFile(hfile
, &This
->trigger
[i
], sizeof(This
->trigger
[0]), &size
, NULL
))
1592 hr
= HRESULT_FROM_WIN32(GetLastError());
1598 hr
= HRESULT_FROM_WIN32(GetLastError());
1601 ITask_DeleteTrigger(&This
->ITask_iface
, idx
);
1606 static BOOL
write_unicode_string(HANDLE hfile
, const WCHAR
*str
)
1611 count
= str
? (lstrlenW(str
) + 1) : 0;
1612 if (!WriteFile(hfile
, &count
, sizeof(count
), &size
, NULL
))
1615 if (!str
) return TRUE
;
1617 count
*= sizeof(WCHAR
);
1618 return WriteFile(hfile
, str
, count
, &size
, NULL
);
1621 static HRESULT WINAPI
MSTASK_IPersistFile_Save(IPersistFile
*iface
, LPCOLESTR task_name
, BOOL remember
)
1623 static WCHAR authorW
[] = L
"Wine";
1624 static WCHAR commentW
[] = L
"Created by Wine";
1626 WORD word
, user_data_size
= 0;
1628 DWORD size
, ver
, disposition
, try;
1629 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1630 ITask
*task
= &This
->ITask_iface
;
1631 LPWSTR appname
= NULL
, params
= NULL
, workdir
= NULL
, creator
= NULL
, comment
= NULL
;
1632 BYTE
*user_data
= NULL
;
1635 TRACE("(%p, %s, %d)\n", iface
, debugstr_w(task_name
), remember
);
1637 disposition
= task_name
? CREATE_NEW
: OPEN_ALWAYS
;
1641 task_name
= This
->task_name
;
1645 ITask_GetComment(task
, &comment
);
1646 if (!comment
) comment
= commentW
;
1647 ITask_GetCreator(task
, &creator
);
1648 if (!creator
) creator
= authorW
;
1649 ITask_GetApplicationName(task
, &appname
);
1650 ITask_GetParameters(task
, ¶ms
);
1651 ITask_GetWorkingDirectory(task
, &workdir
);
1652 ITask_GetWorkItemData(task
, &user_data_size
, &user_data
);
1655 fixed
.product_version
= MAKEWORD(ver
>> 8, ver
);
1656 fixed
.file_version
= 0x0001;
1657 fixed
.name_size_offset
= sizeof(fixed
) + sizeof(USHORT
); /* FIXDLEN_DATA + Instance Count */
1658 fixed
.trigger_offset
= sizeof(fixed
) + sizeof(USHORT
); /* FIXDLEN_DATA + Instance Count */
1659 fixed
.trigger_offset
+= sizeof(USHORT
); /* Application Name */
1661 fixed
.trigger_offset
+= (lstrlenW(appname
) + 1) * sizeof(WCHAR
);
1662 fixed
.trigger_offset
+= sizeof(USHORT
); /* Parameters */
1664 fixed
.trigger_offset
+= (lstrlenW(params
) + 1) * sizeof(WCHAR
);
1665 fixed
.trigger_offset
+= sizeof(USHORT
); /* Working Directory */
1667 fixed
.trigger_offset
+= (lstrlenW(workdir
) + 1) * sizeof(WCHAR
);
1668 fixed
.trigger_offset
+= sizeof(USHORT
); /* Author */
1670 fixed
.trigger_offset
+= (lstrlenW(creator
) + 1) * sizeof(WCHAR
);
1671 fixed
.trigger_offset
+= sizeof(USHORT
); /* Comment */
1673 fixed
.trigger_offset
+= (lstrlenW(comment
) + 1) * sizeof(WCHAR
);
1674 fixed
.trigger_offset
+= sizeof(USHORT
) + user_data_size
; /* User Data */
1675 fixed
.trigger_offset
+= 10; /* Reserved Data */
1677 fixed
.error_retry_count
= 0;
1678 fixed
.error_retry_interval
= 0;
1679 fixed
.idle_wait
= This
->idle_minutes
;
1680 fixed
.idle_deadline
= This
->deadline_minutes
;
1681 fixed
.priority
= This
->priority
;
1682 fixed
.maximum_runtime
= This
->maxRunTime
;
1683 fixed
.exit_code
= This
->exit_code
;
1684 if (This
->status
== SCHED_S_TASK_NOT_SCHEDULED
&& This
->trigger_count
)
1685 This
->status
= SCHED_S_TASK_HAS_NOT_RUN
;
1686 fixed
.status
= This
->status
;
1687 fixed
.flags
= This
->flags
;
1688 fixed
.last_runtime
= This
->last_runtime
;
1693 hfile
= CreateFileW(task_name
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, disposition
, 0, 0);
1694 if (hfile
!= INVALID_HANDLE_VALUE
) break;
1698 hr
= HRESULT_FROM_WIN32(GetLastError());
1704 if (GetLastError() == ERROR_ALREADY_EXISTS
)
1705 fixed
.uuid
= This
->uuid
;
1707 CoCreateGuid(&fixed
.uuid
);
1709 if (!WriteFile(hfile
, &fixed
, sizeof(fixed
), &size
, NULL
))
1711 hr
= HRESULT_FROM_WIN32(GetLastError());
1715 /* Instance Count: don't touch it in the client */
1716 if (SetFilePointer(hfile
, sizeof(word
), NULL
, FILE_CURRENT
) == INVALID_SET_FILE_POINTER
)
1718 hr
= HRESULT_FROM_WIN32(GetLastError());
1721 /* Application Name */
1722 if (!write_unicode_string(hfile
, appname
))
1724 hr
= HRESULT_FROM_WIN32(GetLastError());
1728 if (!write_unicode_string(hfile
, params
))
1730 hr
= HRESULT_FROM_WIN32(GetLastError());
1733 /* Working Directory */
1734 if (!write_unicode_string(hfile
, workdir
))
1736 hr
= HRESULT_FROM_WIN32(GetLastError());
1740 if (!write_unicode_string(hfile
, creator
))
1742 hr
= HRESULT_FROM_WIN32(GetLastError());
1746 if (!write_unicode_string(hfile
, comment
))
1748 hr
= HRESULT_FROM_WIN32(GetLastError());
1753 if (!write_user_data(hfile
, user_data
, user_data_size
))
1755 hr
= HRESULT_FROM_WIN32(GetLastError());
1760 if (!write_reserved_data(hfile
))
1762 hr
= HRESULT_FROM_WIN32(GetLastError());
1767 hr
= write_triggers(This
, hfile
);
1772 if (!write_signature(hfile
))
1774 hr
= HRESULT_FROM_WIN32(GetLastError());
1779 This
->is_dirty
= FALSE
;
1782 CoTaskMemFree(appname
);
1783 CoTaskMemFree(params
);
1784 CoTaskMemFree(workdir
);
1785 if (creator
!= authorW
)
1786 CoTaskMemFree(creator
);
1787 if (comment
!= commentW
)
1788 CoTaskMemFree(comment
);
1789 CoTaskMemFree(user_data
);
1791 if (hfile
!= INVALID_HANDLE_VALUE
)
1795 DeleteFileW(task_name
);
1798 free(This
->task_name
);
1799 This
->task_name
= wcsdup(task_name
);
1805 static HRESULT WINAPI
MSTASK_IPersistFile_SaveCompleted(
1806 IPersistFile
* iface
,
1807 LPCOLESTR pszFileName
)
1809 FIXME("(%p, %p): stub\n", iface
, pszFileName
);
1813 static HRESULT WINAPI
MSTASK_IPersistFile_GetCurFile(IPersistFile
*iface
, LPOLESTR
*file_name
)
1815 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1817 TRACE("(%p, %p)\n", iface
, file_name
);
1819 *file_name
= CoTaskMemAlloc((lstrlenW(This
->task_name
) + 1) * sizeof(WCHAR
));
1820 if (!*file_name
) return E_OUTOFMEMORY
;
1822 lstrcpyW(*file_name
, This
->task_name
);
1826 static const ITaskVtbl MSTASK_ITaskVtbl
=
1828 MSTASK_ITask_QueryInterface
,
1829 MSTASK_ITask_AddRef
,
1830 MSTASK_ITask_Release
,
1831 MSTASK_ITask_CreateTrigger
,
1832 MSTASK_ITask_DeleteTrigger
,
1833 MSTASK_ITask_GetTriggerCount
,
1834 MSTASK_ITask_GetTrigger
,
1835 MSTASK_ITask_GetTriggerString
,
1836 MSTASK_ITask_GetRunTimes
,
1837 MSTASK_ITask_GetNextRunTime
,
1838 MSTASK_ITask_SetIdleWait
,
1839 MSTASK_ITask_GetIdleWait
,
1841 MSTASK_ITask_Terminate
,
1842 MSTASK_ITask_EditWorkItem
,
1843 MSTASK_ITask_GetMostRecentRunTime
,
1844 MSTASK_ITask_GetStatus
,
1845 MSTASK_ITask_GetExitCode
,
1846 MSTASK_ITask_SetComment
,
1847 MSTASK_ITask_GetComment
,
1848 MSTASK_ITask_SetCreator
,
1849 MSTASK_ITask_GetCreator
,
1850 MSTASK_ITask_SetWorkItemData
,
1851 MSTASK_ITask_GetWorkItemData
,
1852 MSTASK_ITask_SetErrorRetryCount
,
1853 MSTASK_ITask_GetErrorRetryCount
,
1854 MSTASK_ITask_SetErrorRetryInterval
,
1855 MSTASK_ITask_GetErrorRetryInterval
,
1856 MSTASK_ITask_SetFlags
,
1857 MSTASK_ITask_GetFlags
,
1858 MSTASK_ITask_SetAccountInformation
,
1859 MSTASK_ITask_GetAccountInformation
,
1860 MSTASK_ITask_SetApplicationName
,
1861 MSTASK_ITask_GetApplicationName
,
1862 MSTASK_ITask_SetParameters
,
1863 MSTASK_ITask_GetParameters
,
1864 MSTASK_ITask_SetWorkingDirectory
,
1865 MSTASK_ITask_GetWorkingDirectory
,
1866 MSTASK_ITask_SetPriority
,
1867 MSTASK_ITask_GetPriority
,
1868 MSTASK_ITask_SetTaskFlags
,
1869 MSTASK_ITask_GetTaskFlags
,
1870 MSTASK_ITask_SetMaxRunTime
,
1871 MSTASK_ITask_GetMaxRunTime
1874 static const IPersistFileVtbl MSTASK_IPersistFileVtbl
=
1876 MSTASK_IPersistFile_QueryInterface
,
1877 MSTASK_IPersistFile_AddRef
,
1878 MSTASK_IPersistFile_Release
,
1879 MSTASK_IPersistFile_GetClassID
,
1880 MSTASK_IPersistFile_IsDirty
,
1881 MSTASK_IPersistFile_Load
,
1882 MSTASK_IPersistFile_Save
,
1883 MSTASK_IPersistFile_SaveCompleted
,
1884 MSTASK_IPersistFile_GetCurFile
1887 HRESULT
TaskConstructor(ITaskService
*service
, const WCHAR
*name
, ITask
**task
)
1890 WCHAR task_name
[MAX_PATH
];
1891 ITaskDefinition
*taskdef
;
1892 IActionCollection
*actions
;
1895 TRACE("(%s, %p)\n", debugstr_w(name
), task
);
1897 if (wcschr(name
, '.')) return E_INVALIDARG
;
1899 GetWindowsDirectoryW(task_name
, MAX_PATH
);
1900 lstrcatW(task_name
, L
"\\Tasks\\");
1901 lstrcatW(task_name
, name
);
1902 lstrcatW(task_name
, L
".job");
1904 hr
= ITaskService_NewTask(service
, 0, &taskdef
);
1905 if (hr
!= S_OK
) return hr
;
1907 This
= malloc(sizeof(*This
));
1910 ITaskDefinition_Release(taskdef
);
1911 return E_OUTOFMEMORY
;
1914 This
->ITask_iface
.lpVtbl
= &MSTASK_ITaskVtbl
;
1915 This
->IPersistFile_iface
.lpVtbl
= &MSTASK_IPersistFileVtbl
;
1917 This
->task
= taskdef
;
1919 This
->data_count
= 0;
1920 This
->task_name
= wcsdup(task_name
);
1922 This
->status
= SCHED_S_TASK_NOT_SCHEDULED
;
1923 This
->exit_code
= 0;
1924 This
->idle_minutes
= 10;
1925 This
->deadline_minutes
= 60;
1926 This
->priority
= NORMAL_PRIORITY_CLASS
;
1927 This
->accountName
= NULL
;
1928 This
->trigger_count
= 0;
1929 This
->trigger
= NULL
;
1930 This
->is_dirty
= FALSE
;
1931 This
->instance_count
= 0;
1933 memset(&This
->last_runtime
, 0, sizeof(This
->last_runtime
));
1934 CoCreateGuid(&This
->uuid
);
1936 /* Default time is 3 days = 259200000 ms */
1937 This
->maxRunTime
= 259200000;
1939 hr
= ITaskDefinition_get_Actions(This
->task
, &actions
);
1942 hr
= IActionCollection_Create(actions
, TASK_ACTION_EXEC
, (IAction
**)&This
->action
);
1943 IActionCollection_Release(actions
);
1946 *task
= &This
->ITask_iface
;
1947 InterlockedIncrement(&dll_ref
);
1952 ITaskDefinition_Release(This
->task
);
1953 ITask_Release(&This
->ITask_iface
);