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
;
64 WORD idle_minutes
, deadline_minutes
;
65 DWORD priority
, maxRunTime
;
68 TASK_TRIGGER
*trigger
;
70 USHORT instance_count
;
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
)
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
);
93 InterlockedDecrement(&dll_ref
);
96 static HRESULT WINAPI
MSTASK_ITask_QueryInterface(
101 TaskImpl
* This
= impl_from_ITask(iface
);
103 TRACE("IID: %s\n", debugstr_guid(riid
));
104 if (ppvObject
== NULL
)
107 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
108 IsEqualGUID(riid
, &IID_ITask
))
110 *ppvObject
= &This
->ITask_iface
;
114 else if (IsEqualGUID(riid
, &IID_IPersistFile
))
116 *ppvObject
= &This
->IPersistFile_iface
;
121 WARN("Unknown interface: %s\n", debugstr_guid(riid
));
123 return E_NOINTERFACE
;
126 static ULONG WINAPI
MSTASK_ITask_AddRef(
129 TaskImpl
*This
= impl_from_ITask(iface
);
132 ref
= InterlockedIncrement(&This
->ref
);
136 static ULONG WINAPI
MSTASK_ITask_Release(
139 TaskImpl
* This
= impl_from_ITask(iface
);
142 ref
= InterlockedDecrement(&This
->ref
);
144 TaskDestructor(This
);
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
;
155 TRACE("(%p, %u, %p)\n", task
, idx
, src
);
157 if (idx
>= This
->trigger_count
)
160 /* Verify valid structure size */
161 if (src
->cbTriggerSize
!= sizeof(*src
))
163 dst
.cbTriggerSize
= src
->cbTriggerSize
;
165 /* Reserved field must be zero */
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
))
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
))
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)
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
;
215 case TASK_TIME_TRIGGER_WEEKLY
:
216 dst
.Type
.Weekly
.WeeksInterval
= src
->Type
.Weekly
.WeeksInterval
;
217 dst
.Type
.Weekly
.rgfDaysOfTheWeek
= src
->Type
.Weekly
.rgfDaysOfTheWeek
;
219 case TASK_TIME_TRIGGER_MONTHLYDATE
:
220 dst
.Type
.MonthlyDate
.rgfDays
= src
->Type
.MonthlyDate
.rgfDays
;
221 dst
.Type
.MonthlyDate
.rgfMonths
= src
->Type
.MonthlyDate
.rgfMonths
;
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
;
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
:
233 dst
.Type
= src
->Type
;
237 /* Reserved field must be zero */
240 /* wRandomMinutesInterval not currently used and is initialized to zero */
241 dst
.wRandomMinutesInterval
= 0;
243 This
->trigger
[idx
] = dst
;
248 HRESULT
task_get_trigger(ITask
*task
, WORD idx
, TASK_TRIGGER
*dst
)
250 TaskImpl
*This
= impl_from_ITask(task
);
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
;
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
;
282 case TASK_TIME_TRIGGER_WEEKLY
:
283 dst
->Type
.Weekly
.WeeksInterval
= src
->Type
.Weekly
.WeeksInterval
;
284 dst
->Type
.Weekly
.rgfDaysOfTheWeek
= src
->Type
.Weekly
.rgfDaysOfTheWeek
;
286 case TASK_TIME_TRIGGER_MONTHLYDATE
:
287 dst
->Type
.MonthlyDate
.rgfDays
= src
->Type
.MonthlyDate
.rgfDays
;
288 dst
->Type
.MonthlyDate
.rgfMonths
= src
->Type
.MonthlyDate
.rgfMonths
;
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
;
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
:
303 dst
->wRandomMinutesInterval
= 0;
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
;
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
;
321 new_trigger
= heap_realloc(This
->trigger
, sizeof(This
->trigger
[0]) * (This
->trigger_count
+ 1));
323 new_trigger
= heap_alloc(sizeof(This
->trigger
[0]));
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
));
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
++;
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
);
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
;
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(
394 LPWSTR
*ppwszTrigger
)
396 FIXME("(%p, %d, %p): stub\n", iface
, iTrigger
, ppwszTrigger
);
400 static HRESULT WINAPI
MSTASK_ITask_GetRunTimes(
402 const LPSYSTEMTIME pstBegin
,
403 const LPSYSTEMTIME pstEnd
,
405 LPSYSTEMTIME
*rgstTaskTimes
)
407 FIXME("(%p, %p, %p, %p, %p): stub\n", iface
, pstBegin
, pstEnd
, pCount
,
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(
423 WORD wDeadlineMinutes
)
425 FIXME("(%p, %d, %d): stub\n", iface
, wIdleMinutes
, wDeadlineMinutes
);
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
;
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(
471 FIXME("(%p, %p, %d): stub\n", iface
, hParent
, dwReserved
);
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
;
493 static HRESULT WINAPI
MSTASK_ITask_GetExitCode(ITask
*iface
, DWORD
*exit_code
)
495 FIXME("(%p, %p): stub\n", iface
, exit_code
);
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
;
507 TRACE("(%p, %s)\n", iface
, debugstr_w(comment
));
509 if (!comment
|| !comment
[0])
512 hr
= ITaskDefinition_get_RegistrationInfo(This
->task
, &info
);
515 hr
= IRegistrationInfo_put_Description(info
, (BSTR
)comment
);
516 IRegistrationInfo_Release(info
);
517 This
->is_dirty
= TRUE
;
522 static HRESULT WINAPI
MSTASK_ITask_GetComment(ITask
*iface
, LPWSTR
*comment
)
524 TaskImpl
*This
= impl_from_ITask(iface
);
525 IRegistrationInfo
*info
;
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
);
538 len
= description
? lstrlenW(description
) + 1 : 1;
539 *comment
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
545 lstrcpyW(*comment
, description
);
551 SysFreeString(description
);
554 IRegistrationInfo_Release(info
);
558 static HRESULT WINAPI
MSTASK_ITask_SetCreator(ITask
*iface
, LPCWSTR creator
)
560 TaskImpl
*This
= impl_from_ITask(iface
);
561 IRegistrationInfo
*info
;
564 TRACE("(%p, %s)\n", iface
, debugstr_w(creator
));
566 if (!creator
|| !creator
[0])
569 hr
= ITaskDefinition_get_RegistrationInfo(This
->task
, &info
);
572 hr
= IRegistrationInfo_put_Author(info
, (BSTR
)creator
);
573 IRegistrationInfo_Release(info
);
574 This
->is_dirty
= TRUE
;
579 static HRESULT WINAPI
MSTASK_ITask_GetCreator(ITask
*iface
, LPWSTR
*creator
)
581 TaskImpl
*This
= impl_from_ITask(iface
);
582 IRegistrationInfo
*info
;
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
);
595 len
= author
? lstrlenW(author
) + 1 : 1;
596 *creator
= CoTaskMemAlloc(len
* sizeof(WCHAR
));
602 lstrcpyW(*creator
, author
);
608 SysFreeString(author
);
611 IRegistrationInfo_Release(info
);
615 static HRESULT WINAPI
MSTASK_ITask_SetWorkItemData(
620 FIXME("(%p, %d, %p): stub\n", iface
, cBytes
, rgbData
);
624 static HRESULT WINAPI
MSTASK_ITask_GetWorkItemData(
629 FIXME("(%p, %p, %p): stub\n", iface
, pcBytes
, ppBytes
);
633 static HRESULT WINAPI
MSTASK_ITask_SetErrorRetryCount(
637 FIXME("(%p, %d): stub\n", iface
, wRetryCount
);
641 static HRESULT WINAPI
MSTASK_ITask_GetErrorRetryCount(ITask
*iface
, WORD
*count
)
643 TRACE("(%p, %p)\n", iface
, count
);
647 static HRESULT WINAPI
MSTASK_ITask_SetErrorRetryInterval(
651 FIXME("(%p, %d): stub\n", iface
, wRetryInterval
);
655 static HRESULT WINAPI
MSTASK_ITask_GetErrorRetryInterval(ITask
*iface
, WORD
*interval
)
657 TRACE("(%p, %p)\n", iface
, interval
);
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
;
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
);
681 static HRESULT WINAPI
MSTASK_ITask_SetAccountInformation(
683 LPCWSTR pwszAccountName
,
684 LPCWSTR pwszPassword
)
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
));
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
;
707 static HRESULT WINAPI
MSTASK_ITask_GetAccountInformation(
709 LPWSTR
*ppwszAccountName
)
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
);
729 static HRESULT WINAPI
MSTASK_ITask_SetApplicationName(ITask
*iface
, LPCWSTR appname
)
731 TaskImpl
*This
= impl_from_ITask(iface
);
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
);
747 tmp_name
= heap_alloc(len
* sizeof(WCHAR
));
749 return E_OUTOFMEMORY
;
750 len
= SearchPathW(NULL
, appname
, NULL
, len
, tmp_name
, NULL
);
753 hr
= IExecAction_put_Path(This
->action
, tmp_name
);
754 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
757 hr
= HRESULT_FROM_WIN32(GetLastError());
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
;
769 static HRESULT WINAPI
MSTASK_ITask_GetApplicationName(ITask
*iface
, LPWSTR
*appname
)
771 TaskImpl
*This
= impl_from_ITask(iface
);
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
));
788 lstrcpyW(*appname
, path
);
798 static HRESULT WINAPI
MSTASK_ITask_SetParameters(ITask
*iface
, LPCWSTR params
)
800 TaskImpl
*This
= impl_from_ITask(iface
);
803 TRACE("(%p, %s)\n", iface
, debugstr_w(params
));
805 /* Empty parameter list */
806 if (!params
|| !params
[0])
809 hr
= IExecAction_put_Arguments(This
->action
, (BSTR
)params
);
810 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
814 static HRESULT WINAPI
MSTASK_ITask_GetParameters(ITask
*iface
, LPWSTR
*params
)
816 TaskImpl
*This
= impl_from_ITask(iface
);
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
));
833 lstrcpyW(*params
, args
);
843 static HRESULT WINAPI
MSTASK_ITask_SetWorkingDirectory(ITask
* iface
, LPCWSTR workdir
)
845 TaskImpl
*This
= impl_from_ITask(iface
);
848 TRACE("(%p, %s)\n", iface
, debugstr_w(workdir
));
850 if (!workdir
|| !workdir
[0])
853 hr
= IExecAction_put_WorkingDirectory(This
->action
, (BSTR
)workdir
);
854 if (hr
== S_OK
) This
->is_dirty
= TRUE
;
858 static HRESULT WINAPI
MSTASK_ITask_GetWorkingDirectory(ITask
*iface
, LPWSTR
*workdir
)
860 TaskImpl
*This
= impl_from_ITask(iface
);
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
));
877 lstrcpyW(*workdir
, dir
);
887 static HRESULT WINAPI
MSTASK_ITask_SetPriority(
891 FIXME("(%p, 0x%08x): stub\n", iface
, dwPriority
);
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
;
905 static HRESULT WINAPI
MSTASK_ITask_SetTaskFlags(
909 FIXME("(%p, 0x%08x): stub\n", iface
, dwFlags
);
913 static HRESULT WINAPI
MSTASK_ITask_GetTaskFlags(ITask
*iface
, DWORD
*flags
)
915 FIXME("(%p, %p): stub\n", iface
, flags
);
920 static HRESULT WINAPI
MSTASK_ITask_SetMaxRunTime(
924 TaskImpl
*This
= impl_from_ITask(iface
);
926 TRACE("(%p, %d)\n", iface
, dwMaxRunTime
);
928 This
->maxRunTime
= dwMaxRunTime
;
929 This
->is_dirty
= TRUE
;
933 static HRESULT WINAPI
MSTASK_ITask_GetMaxRunTime(
935 DWORD
*pdwMaxRunTime
)
937 TaskImpl
*This
= impl_from_ITask(iface
);
939 TRACE("(%p, %p)\n", iface
, pdwMaxRunTime
);
941 *pdwMaxRunTime
= This
->maxRunTime
;
945 static HRESULT WINAPI
MSTASK_IPersistFile_QueryInterface(
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(
958 TaskImpl
*This
= impl_from_IPersistFile(iface
);
959 return ITask_AddRef(&This
->ITask_iface
);
962 static ULONG WINAPI
MSTASK_IPersistFile_Release(
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
;
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;
989 for (i
= 0; i
< 5; i
++)
991 if (limit
< sizeof(USHORT
))
993 TRACE("invalid string %u offset\n", i
);
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
);
1007 TRACE("string %u: %s\n", i
, wine_dbgstr_wn((const WCHAR
*)data
, len
));
1012 ITask_SetApplicationName(task
, (const WCHAR
*)data
);
1015 ITask_SetParameters(task
, (const WCHAR
*)data
);
1018 ITask_SetWorkingDirectory(task
, (const WCHAR
*)data
);
1021 ITask_SetCreator(task
, (const WCHAR
*)data
);
1024 ITask_SetComment(task
, (const WCHAR
*)data
);
1030 data
+= len
* sizeof(WCHAR
);
1031 data_size
+= len
* sizeof(WCHAR
);
1037 static HRESULT
load_job_data(TaskImpl
*This
, BYTE
*data
, DWORD size
)
1039 ITask
*task
= &This
->ITask_iface
;
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
);
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
;
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
;
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
;
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
;
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
);
1181 ITask_DeleteTrigger(task
, idx
);
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]);
1201 static HRESULT WINAPI
MSTASK_IPersistFile_Load(IPersistFile
*iface
, LPCOLESTR file_name
, DWORD mode
)
1203 TaskImpl
*This
= impl_from_IPersistFile(iface
);
1205 HANDLE file
, mapping
;
1206 DWORD access
, sharing
, size
;
1209 TRACE("(%p, %s, 0x%08x)\n", iface
, debugstr_w(file_name
), mode
);
1211 switch (mode
& 0x000f)
1215 access
= GENERIC_READ
;
1218 case STGM_READWRITE
:
1219 access
= GENERIC_READ
| GENERIC_WRITE
;
1223 switch (mode
& 0x00f0)
1226 case STGM_SHARE_DENY_NONE
:
1227 sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
;
1229 case STGM_SHARE_DENY_READ
:
1230 sharing
= FILE_SHARE_WRITE
;
1232 case STGM_SHARE_DENY_WRITE
:
1233 sharing
= FILE_SHARE_READ
;
1235 case STGM_SHARE_EXCLUSIVE
:
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);
1252 TRACE("Failed to create file mapping %s, error %u\n", debugstr_w(file_name
), GetLastError());
1254 return HRESULT_FROM_WIN32(GetLastError());
1257 data
= MapViewOfFile(mapping
, FILE_MAP_READ
, 0, 0, 0);
1260 hr
= load_job_data(This
, data
, size
);
1261 if (hr
== S_OK
) This
->is_dirty
= FALSE
;
1262 UnmapViewOfFile(data
);
1265 hr
= HRESULT_FROM_WIN32(GetLastError());
1267 CloseHandle(mapping
);
1273 static BOOL
write_signature(HANDLE hfile
)
1277 USHORT SignatureVersion
;
1278 USHORT ClientVersion
;
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
)
1296 } user
= { 8, { 0xff,0x0f,0x1d,0,0,0,0,0 } };
1299 return WriteFile(hfile
, &user
, sizeof(user
), &size
, NULL
);
1302 static BOOL
write_user_data(HANDLE hfile
, BYTE
*data
, WORD data_size
)
1306 if (!WriteFile(hfile
, &data_size
, sizeof(data_size
), &size
, NULL
))
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;
1319 ITaskTrigger
*trigger
;
1321 count
= This
->trigger_count
;
1323 /* Windows saves a .job with at least 1 trigger */
1326 hr
= ITask_CreateTrigger(&This
->ITask_iface
, &idx
, &trigger
);
1327 if (hr
!= S_OK
) return hr
;
1328 ITaskTrigger_Release(trigger
);
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());
1345 hr
= HRESULT_FROM_WIN32(GetLastError());
1348 ITask_DeleteTrigger(&This
->ITask_iface
, idx
);
1353 static BOOL
write_unicode_string(HANDLE hfile
, const WCHAR
*str
)
1358 count
= str
? (lstrlenW(str
) + 1) : 0;
1359 if (!WriteFile(hfile
, &count
, sizeof(count
), &size
, NULL
))
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 };
1373 WORD word
, user_data_size
= 0;
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
;
1382 TRACE("(%p, %s, %d)\n", iface
, debugstr_w(task_name
), remember
);
1384 disposition
= task_name
? CREATE_NEW
: OPEN_ALWAYS
;
1388 task_name
= This
->task_name
;
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
, ¶ms
);
1398 ITask_GetWorkingDirectory(task
, &workdir
);
1399 ITask_GetWorkItemData(task
, &user_data_size
, &user_data
);
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 */
1409 fixed
.trigger_offset
+= (lstrlenW(appname
) + 1) * sizeof(WCHAR
);
1410 fixed
.trigger_offset
+= sizeof(USHORT
); /* Parameters */
1412 fixed
.trigger_offset
+= (lstrlenW(params
) + 1) * sizeof(WCHAR
);
1413 fixed
.trigger_offset
+= sizeof(USHORT
); /* Working Directory */
1415 fixed
.trigger_offset
+= (lstrlenW(workdir
) + 1) * sizeof(WCHAR
);
1416 fixed
.trigger_offset
+= sizeof(USHORT
); /* Author */
1418 fixed
.trigger_offset
+= (lstrlenW(creator
) + 1) * sizeof(WCHAR
);
1419 fixed
.trigger_offset
+= sizeof(USHORT
); /* 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
));
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());
1448 if (!WriteFile(hfile
, &fixed
, sizeof(fixed
), &size
, NULL
))
1450 hr
= HRESULT_FROM_WIN32(GetLastError());
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());
1460 /* Application Name */
1461 if (!write_unicode_string(hfile
, appname
))
1463 hr
= HRESULT_FROM_WIN32(GetLastError());
1467 if (!write_unicode_string(hfile
, params
))
1469 hr
= HRESULT_FROM_WIN32(GetLastError());
1472 /* Working Directory */
1473 if (!write_unicode_string(hfile
, workdir
))
1475 hr
= HRESULT_FROM_WIN32(GetLastError());
1479 if (!write_unicode_string(hfile
, creator
))
1481 hr
= HRESULT_FROM_WIN32(GetLastError());
1485 if (!write_unicode_string(hfile
, comment
))
1487 hr
= HRESULT_FROM_WIN32(GetLastError());
1492 if (!write_user_data(hfile
, user_data
, user_data_size
))
1494 hr
= HRESULT_FROM_WIN32(GetLastError());
1499 if (!write_reserved_data(hfile
))
1501 hr
= HRESULT_FROM_WIN32(GetLastError());
1506 hr
= write_triggers(This
, hfile
);
1511 if (!write_signature(hfile
))
1513 hr
= HRESULT_FROM_WIN32(GetLastError());
1518 This
->is_dirty
= FALSE
;
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
);
1532 DeleteFileW(task_name
);
1535 heap_free(This
->task_name
);
1536 This
->task_name
= heap_strdupW(task_name
);
1541 static HRESULT WINAPI
MSTASK_IPersistFile_SaveCompleted(
1542 IPersistFile
* iface
,
1543 LPCOLESTR pszFileName
)
1545 FIXME("(%p, %p): stub\n", iface
, pszFileName
);
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
);
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
,
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 };
1628 WCHAR task_name
[MAX_PATH
];
1629 ITaskDefinition
*taskdef
;
1630 IActionCollection
*actions
;
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
));
1648 ITaskDefinition_Release(taskdef
);
1649 return E_OUTOFMEMORY
;
1652 This
->ITask_iface
.lpVtbl
= &MSTASK_ITaskVtbl
;
1653 This
->IPersistFile_iface
.lpVtbl
= &MSTASK_IPersistFileVtbl
;
1655 This
->task
= taskdef
;
1656 This
->task_name
= heap_strdupW(task_name
);
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
);
1674 hr
= IActionCollection_Create(actions
, TASK_ACTION_EXEC
, (IAction
**)&This
->action
);
1675 IActionCollection_Release(actions
);
1678 *task
= &This
->ITask_iface
;
1679 InterlockedIncrement(&dll_ref
);
1684 ITaskDefinition_Release(This
->task
);
1685 ITask_Release(&This
->ITask_iface
);