2 * Copyright 2012 Detlef Riekenberg
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(schtasks
);
28 static const WCHAR change_optW
[] = {'/','c','h','a','n','g','e',0};
29 static const WCHAR create_optW
[] = {'/','c','r','e','a','t','e',0};
30 static const WCHAR delete_optW
[] = {'/','d','e','l','e','t','e',0};
31 static const WCHAR enable_optW
[] = {'/','e','n','a','b','l','e',0};
32 static const WCHAR f_optW
[] = {'/','f',0};
33 static const WCHAR ru_optW
[] = {'/','r','u',0};
34 static const WCHAR tn_optW
[] = {'/','t','n',0};
35 static const WCHAR tr_optW
[] = {'/','t','r',0};
36 static const WCHAR xml_optW
[] = {'/','x','m','l',0};
38 static ITaskFolder
*get_tasks_root_folder(void)
40 ITaskService
*service
;
45 hres
= CoCreateInstance(&CLSID_TaskScheduler
, NULL
, CLSCTX_INPROC_SERVER
,
46 &IID_ITaskService
, (void**)&service
);
50 V_VT(&empty
) = VT_EMPTY
;
51 hres
= ITaskService_Connect(service
, empty
, empty
, empty
, empty
);
53 FIXME("Connect failed: %08x\n", hres
);
57 hres
= ITaskService_GetFolder(service
, NULL
, &root
);
58 ITaskService_Release(service
);
60 FIXME("GetFolder failed: %08x\n", hres
);
67 static IRegisteredTask
*get_registered_task(const WCHAR
*name
)
69 IRegisteredTask
*registered_task
;
74 root
= get_tasks_root_folder();
78 str
= SysAllocString(name
);
79 hres
= ITaskFolder_GetTask(root
, str
, ®istered_task
);
81 ITaskFolder_Release(root
);
83 FIXME("GetTask failed: %08x\n", hres
);
87 return registered_task
;
90 static BSTR
read_file_to_bstr(const WCHAR
*file_name
)
92 LARGE_INTEGER file_size
;
93 DWORD read_size
, size
;
99 file
= CreateFileW(file_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
100 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
101 if (file
== INVALID_HANDLE_VALUE
) {
102 FIXME("Could not open file\n");
106 if (!GetFileSizeEx(file
, &file_size
) || !file_size
.QuadPart
) {
107 FIXME("Empty file\n");
112 data
= HeapAlloc(GetProcessHeap(), 0, file_size
.QuadPart
);
114 r
= ReadFile(file
, data
, file_size
.QuadPart
, &read_size
, NULL
);
117 FIXME("Read filed\n");
118 HeapFree(GetProcessHeap(), 0, data
);
122 if (read_size
> 2 && data
[0] == 0xff && data
[1] == 0xfe) { /* UTF-16 BOM */
123 ret
= SysAllocStringLen((const WCHAR
*)(data
+ 2), (read_size
- 2) / sizeof(WCHAR
));
125 size
= MultiByteToWideChar(CP_ACP
, 0, (const char *)data
, read_size
, NULL
, 0);
126 ret
= SysAllocStringLen(NULL
, size
);
128 MultiByteToWideChar(CP_ACP
, 0, (const char *)data
, read_size
, ret
, size
);
130 HeapFree(GetProcessHeap(), 0, data
);
135 static int change_command(int argc
, WCHAR
*argv
[])
137 BOOL have_option
= FALSE
, enable
= FALSE
;
138 const WCHAR
*task_name
= NULL
;
139 IRegisteredTask
*task
;
143 if(!wcsicmp(argv
[0], tn_optW
)) {
145 FIXME("Missing /tn value\n");
150 FIXME("Duplicated /tn argument\n");
157 }else if (!wcsicmp(argv
[0], enable_optW
)) {
162 }else if (!wcsicmp(argv
[0], tr_optW
)) {
164 FIXME("Missing /tr value\n");
168 FIXME("Unsupported /tr option %s\n", debugstr_w(argv
[1]));
173 FIXME("Unsupported arguments %s\n", debugstr_w(argv
[0]));
179 FIXME("Missing /tn option\n");
184 FIXME("Missing change options\n");
188 task
= get_registered_task(task_name
);
193 hres
= IRegisteredTask_put_Enabled(task
, VARIANT_TRUE
);
195 IRegisteredTask_Release(task
);
196 FIXME("put_Enabled failed: %08x\n", hres
);
201 IRegisteredTask_Release(task
);
205 static int create_command(int argc
, WCHAR
*argv
[])
207 const WCHAR
*task_name
= NULL
, *xml_file
= NULL
;
208 ITaskFolder
*root
= NULL
;
209 LONG flags
= TASK_CREATE
;
210 IRegisteredTask
*task
;
216 if (!wcsicmp(argv
[0], xml_optW
)) {
218 FIXME("Missing /xml value\n");
223 FIXME("Duplicated /xml argument\n");
230 }else if(!wcsicmp(argv
[0], tn_optW
)) {
232 FIXME("Missing /tn value\n");
237 FIXME("Duplicated /tn argument\n");
244 }else if(!wcsicmp(argv
[0], f_optW
)) {
245 flags
= TASK_CREATE_OR_UPDATE
;
248 }else if (!wcsicmp(argv
[0], ru_optW
)) {
250 FIXME("Missing /ru value\n");
254 FIXME("Unsupported /ru option %s\n", debugstr_w(argv
[1]));
258 FIXME("Unsupported argument %s\n", debugstr_w(argv
[0]));
264 FIXME("Missing /tn argument\n");
269 FIXME("Missing /xml argument\n");
273 xml
= read_file_to_bstr(xml_file
);
277 root
= get_tasks_root_folder();
283 V_VT(&empty
) = VT_EMPTY
;
284 str
= SysAllocString(task_name
);
285 hres
= ITaskFolder_RegisterTask(root
, str
, xml
, flags
, empty
, empty
,
286 TASK_LOGON_NONE
, empty
, &task
);
289 ITaskFolder_Release(root
);
293 IRegisteredTask_Release(task
);
297 static int delete_command(int argc
, WCHAR
*argv
[])
299 const WCHAR
*task_name
= NULL
;
300 ITaskFolder
*root
= NULL
;
305 if (!wcsicmp(argv
[0], f_optW
)) {
306 TRACE("force opt\n");
309 }else if(!wcsicmp(argv
[0], tn_optW
)) {
311 FIXME("Missing /tn value\n");
316 FIXME("Duplicated /tn argument\n");
324 FIXME("Unsupported argument %s\n", debugstr_w(argv
[0]));
330 FIXME("Missing /tn argument\n");
334 root
= get_tasks_root_folder();
338 str
= SysAllocString(task_name
);
339 hres
= ITaskFolder_DeleteTask(root
, str
, 0);
341 ITaskFolder_Release(root
);
348 int wmain(int argc
, WCHAR
*argv
[])
352 for (i
= 0; i
< argc
; i
++)
353 TRACE(" %s", wine_dbgstr_w(argv
[i
]));
359 FIXME("Print current tasks state\n");
360 else if (!wcsicmp(argv
[1], change_optW
))
361 ret
= change_command(argc
- 2, argv
+ 2);
362 else if (!wcsicmp(argv
[1], create_optW
))
363 ret
= create_command(argc
- 2, argv
+ 2);
364 else if (!wcsicmp(argv
[1], delete_optW
))
365 ret
= delete_command(argc
- 2, argv
+ 2);
367 FIXME("Unsupported command %s\n", debugstr_w(argv
[1]));