[PATCH 3/3] mstask: Reimplement ITask constructor using ITaskFolder methods.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 3/3] mstask: Reimplement ITask constructor using ITaskFolder methods.

Dmitry Timoshkov
Signed-off-by: Dmitry Timoshkov <[hidden email]>
---
 dlls/mstask/factory.c        |  1 +
 dlls/mstask/mstask_main.c    |  1 +
 dlls/mstask/mstask_private.h |  2 +-
 dlls/mstask/task.c           | 50 +++++++++++++++++++++++++++++++-------------
 dlls/mstask/task_scheduler.c | 26 ++++++++++++++++-------
 dlls/mstask/task_trigger.c   |  1 +
 6 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/dlls/mstask/factory.c b/dlls/mstask/factory.c
index 97a5113868..260a93066f 100644
--- a/dlls/mstask/factory.c
+++ b/dlls/mstask/factory.c
@@ -23,6 +23,7 @@
 #include "windef.h"
 #include "winbase.h"
 #include "objbase.h"
+#include "taskschd.h"
 #include "mstask.h"
 #include "mstask_private.h"
 #include "wine/debug.h"
diff --git a/dlls/mstask/mstask_main.c b/dlls/mstask/mstask_main.c
index bb19b89da2..eae85562f5 100644
--- a/dlls/mstask/mstask_main.c
+++ b/dlls/mstask/mstask_main.c
@@ -24,6 +24,7 @@
 #include "winbase.h"
 #include "objbase.h"
 #include "rpcproxy.h"
+#include "taskschd.h"
 #include "mstask.h"
 #include "mstask_private.h"
 #include "wine/debug.h"
diff --git a/dlls/mstask/mstask_private.h b/dlls/mstask/mstask_private.h
index 9180e4a51f..511019ded6 100644
--- a/dlls/mstask/mstask_private.h
+++ b/dlls/mstask/mstask_private.h
@@ -26,6 +26,6 @@ extern ClassFactoryImpl MSTASK_ClassFactory DECLSPEC_HIDDEN;
 
 extern HRESULT TaskTriggerConstructor(LPVOID *ppObj) DECLSPEC_HIDDEN;
 extern HRESULT TaskSchedulerConstructor(LPVOID *ppObj) DECLSPEC_HIDDEN;
-extern HRESULT TaskConstructor(LPCWSTR pwszTaskName, LPVOID *ppObj) DECLSPEC_HIDDEN;
+extern HRESULT TaskConstructor(ITaskFolder *folder, const WCHAR *task_name, ITask **task, BOOL create) DECLSPEC_HIDDEN;
 
 #endif /* __MSTASK_PRIVATE_H__ */
diff --git a/dlls/mstask/task.c b/dlls/mstask/task.c
index 872e445b5e..095431cb04 100644
--- a/dlls/mstask/task.c
+++ b/dlls/mstask/task.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2008 Google (Roy Shea)
+ * Copyright (C) 2018 Dmitry Timoshkov
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -23,6 +24,7 @@
 #include "windef.h"
 #include "winbase.h"
 #include "objbase.h"
+#include "taskschd.h"
 #include "mstask.h"
 #include "mstask_private.h"
 #include "wine/debug.h"
@@ -34,7 +36,7 @@ typedef struct
     ITask ITask_iface;
     IPersistFile IPersistFile_iface;
     LONG ref;
-    LPWSTR taskName;
+    IRegisteredTask *regtask;
     LPWSTR applicationName;
     LPWSTR parameters;
     LPWSTR comment;
@@ -58,7 +60,6 @@ static void TaskDestructor(TaskImpl *This)
     HeapFree(GetProcessHeap(), 0, This->accountName);
     HeapFree(GetProcessHeap(), 0, This->comment);
     HeapFree(GetProcessHeap(), 0, This->parameters);
-    HeapFree(GetProcessHeap(), 0, This->taskName);
     HeapFree(GetProcessHeap(), 0, This);
     InterlockedDecrement(&dll_ref);
 }
@@ -761,28 +762,49 @@ static const IPersistFileVtbl MSTASK_IPersistFileVtbl =
     MSTASK_IPersistFile_GetCurFile
 };
 
-HRESULT TaskConstructor(LPCWSTR pwszTaskName, LPVOID *ppObj)
+HRESULT TaskConstructor(ITaskFolder *folder, const WCHAR *task_name, ITask **task, BOOL create)
 {
     TaskImpl *This;
-    int n;
+    IRegisteredTask *regtask;
+    BSTR bstr;
+    HRESULT hr;
 
-    TRACE("(%s, %p)\n", debugstr_w(pwszTaskName), ppObj);
+    TRACE("(%s, %p)\n", debugstr_w(task_name), task);
+
+    bstr = SysAllocString(task_name);
+    if (!bstr) return E_OUTOFMEMORY;
+
+    if (create)
+    {
+        static const char xml_tmplate[] =
+            "<?xml version=\"1.0\"?>\n"
+            "<Task xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
+            "</Task>\n";
+        WCHAR xmlW[sizeof(xml_tmplate)];
+        VARIANT v_null;
+
+        MultiByteToWideChar(CP_ACP, 0, xml_tmplate, -1, xmlW, sizeof(xmlW)/sizeof(xmlW[0]));
+
+        V_VT(&v_null) = VT_NULL;
+        hr = ITaskFolder_RegisterTask(folder, bstr, xmlW, TASK_CREATE | TASK_UPDATE,
+                                      v_null, v_null, TASK_LOGON_NONE, v_null, &regtask);
+    }
+    else
+        hr = ITaskFolder_GetTask(folder, bstr, &regtask);
+    SysFreeString(bstr);
+    if (hr != S_OK) return hr;
 
     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
     if (!This)
+    {
+        IRegisteredTask_Release(regtask);
         return E_OUTOFMEMORY;
+    }
 
     This->ITask_iface.lpVtbl = &MSTASK_ITaskVtbl;
     This->IPersistFile_iface.lpVtbl = &MSTASK_IPersistFileVtbl;
     This->ref = 1;
-    n = (lstrlenW(pwszTaskName) + 1) * sizeof(WCHAR);
-    This->taskName = HeapAlloc(GetProcessHeap(), 0, n);
-    if (!This->taskName)
-    {
-        HeapFree(GetProcessHeap(), 0, This);
-        return E_OUTOFMEMORY;
-    }
-    lstrcpyW(This->taskName, pwszTaskName);
+    This->regtask = regtask;
     This->applicationName = NULL;
     This->parameters = NULL;
     This->comment = NULL;
@@ -791,7 +813,7 @@ HRESULT TaskConstructor(LPCWSTR pwszTaskName, LPVOID *ppObj)
     /* Default time is 3 days = 259200000 ms */
     This->maxRunTime = 259200000;
 
-    *ppObj = &This->ITask_iface;
+    *task = &This->ITask_iface;
     InterlockedIncrement(&dll_ref);
     return S_OK;
 }
diff --git a/dlls/mstask/task_scheduler.c b/dlls/mstask/task_scheduler.c
index 1ace4dd7a2..e9b8727633 100644
--- a/dlls/mstask/task_scheduler.c
+++ b/dlls/mstask/task_scheduler.c
@@ -37,6 +37,7 @@ typedef struct
     ITaskScheduler ITaskScheduler_iface;
     LONG ref;
     ITaskService *service;
+    ITaskFolder *root;
 } TaskSchedulerImpl;
 
 typedef struct
@@ -58,6 +59,7 @@ static inline EnumWorkItemsImpl *impl_from_IEnumWorkItems(IEnumWorkItems *iface)
 static void TaskSchedulerDestructor(TaskSchedulerImpl *This)
 {
     TRACE("%p\n", This);
+    ITaskFolder_Release(This->root);
     ITaskService_Release(This->service);
     HeapFree(GetProcessHeap(), 0, This);
     InterlockedDecrement(&dll_ref);
@@ -290,14 +292,15 @@ static HRESULT WINAPI MSTASK_ITaskScheduler_Delete(
 
 static HRESULT WINAPI MSTASK_ITaskScheduler_NewWorkItem(
         ITaskScheduler* iface,
-        LPCWSTR pwszTaskName,
+        LPCWSTR task_name,
         REFCLSID rclsid,
         REFIID riid,
-        IUnknown **ppunk)
+        IUnknown **task)
 {
-    HRESULT hr;
-    TRACE("(%p, %s, %s, %s, %p)\n", iface, debugstr_w(pwszTaskName),
-            debugstr_guid(rclsid) ,debugstr_guid(riid),  ppunk);
+    TaskSchedulerImpl *This = impl_from_ITaskScheduler(iface);
+
+    TRACE("(%p, %s, %s, %s, %p)\n", iface, debugstr_w(task_name),
+            debugstr_guid(rclsid), debugstr_guid(riid), task);
 
     if (!IsEqualGUID(rclsid, &CLSID_CTask))
         return CLASS_E_CLASSNOTAVAILABLE;
@@ -305,8 +308,7 @@ static HRESULT WINAPI MSTASK_ITaskScheduler_NewWorkItem(
     if (!IsEqualGUID(riid, &IID_ITask))
         return E_NOINTERFACE;
 
-    hr = TaskConstructor(pwszTaskName, (LPVOID *)ppunk);
-    return hr;
+    return TaskConstructor(This->root, task_name, (ITask **)task, TRUE);
 }
 
 static HRESULT WINAPI MSTASK_ITaskScheduler_AddWorkItem(
@@ -347,6 +349,7 @@ HRESULT TaskSchedulerConstructor(LPVOID *ppObj)
 {
     TaskSchedulerImpl *This;
     ITaskService *service;
+    ITaskFolder *root;
     VARIANT v_null;
     HRESULT hr;
 
@@ -363,15 +366,24 @@ HRESULT TaskSchedulerConstructor(LPVOID *ppObj)
         return hr;
     }
 
+    hr = ITaskService_GetFolder(service, NULL, &root);
+    if (hr != S_OK)
+    {
+        ITaskService_Release(service);
+        return hr;
+    }
+
     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
     if (!This)
     {
+        ITaskFolder_Release(root);
         ITaskService_Release(service);
         return E_OUTOFMEMORY;
     }
 
     This->ITaskScheduler_iface.lpVtbl = &MSTASK_ITaskSchedulerVtbl;
     This->service = service;
+    This->root = root;
     This->ref = 1;
 
     *ppObj = &This->ITaskScheduler_iface;
diff --git a/dlls/mstask/task_trigger.c b/dlls/mstask/task_trigger.c
index 9475e5608a..56bfbed54f 100644
--- a/dlls/mstask/task_trigger.c
+++ b/dlls/mstask/task_trigger.c
@@ -24,6 +24,7 @@
 #include "winbase.h"
 #include "objbase.h"
 #include "winternl.h"
+#include "taskschd.h"
 #include "mstask.h"
 #include "mstask_private.h"
 #include "wine/debug.h"
--
2.16.2