-
Notifications
You must be signed in to change notification settings - Fork 33
/
NtUtils.TaskScheduler.pas
325 lines (267 loc) · 8.23 KB
/
NtUtils.TaskScheduler.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
unit NtUtils.TaskScheduler;
{
This file provides wrappers for interacting with the Task Scheduler service.
}
interface
uses
Ntapi.taskschd, Ntapi.ObjBase, NtUtils;
type
ITaskService = Ntapi.taskschd.ITaskService;
ITaskFolder = Ntapi.taskschd.ITaskFolder;
IRegisteredTask = Ntapi.taskschd.IRegisteredTask;
// Opening registered tasks requires opening a task folder first. This
// enumeration defines which folder the caller wants to use by default.
TRootTaskFolderMode = (
rmUseGlobalRoot,
rmUseClosestParent
);
// Make a connection to the ITaskService interface
[RequiresCOM]
function ComxTaskSchedulerConnect(
out TaskService: ITaskService;
[opt] const ServerName: String = '';
[opt] const DomainName: String = '';
[opt] const UserName: String = '';
[opt] const Password: String = ''
): TNtxStatus;
// Open a task folder from under the root
[RequiresCOM]
function ComxTaskSchedulerOpenFolder(
out TaskFolder: ITaskFolder;
const Path: String;
[opt] TaskService: ITaskService = nil
): TNtxStatus;
// Enumerate all sub-folders of a task scheduler folder object
[RequiresCOM]
function ComxTaskSchedulerEnumerateFolders(
out SubFolders: TArray<ITaskFolder>;
const TaskFolder: ITaskFolder
): TNtxStatus;
// Make a for-in iterator for enumerating sub-folders of a task scheduler folder.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
[RequiresCOM]
function ComxTaskSchedulerIterateFolders(
[out, opt] Status: PNtxStatus;
const TaskFolder: ITaskFolder
): IEnumerable<ITaskFolder>;
// Enumerate all tasks in a task scheduler folder
[RequiresCOM]
function ComxTaskSchedulerEnumerateTasks(
out Tasks: TArray<IRegisteredTask>;
const TaskFolder: ITaskFolder;
Flags: TTaskEnumFlags = TASK_ENUM_HIDDEN
): TNtxStatus;
// Make a for-in iterator for enumerating tasks in a task scheduler folder.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
[RequiresCOM]
function ComxTaskSchedulerIterateTasks(
[out, opt] Status: PNtxStatus;
const TaskFolder: ITaskFolder;
Flags: TTaskEnumFlags = TASK_ENUM_HIDDEN
): IEnumerable<IRegisteredTask>;
// Open a task by name
[RequiresCOM]
function ComxTaskSchedulerOpenTask(
out Task: IRegisteredTask;
const Path: String;
[opt] Root: ITaskFolder = nil;
RootMode: TRootTaskFolderMode = rmUseGlobalRoot
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, NtUtils.Com, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function ComxTaskSchedulerConnect;
var
ServerStr, UserStr, DomainStr, PasswordStr: WideString;
begin
Result := ComxCreateInstanceWithFallback(taskschd, CLSID_TaskScheduler,
ITaskService, TaskService, 'CLSID_TaskScheduler');
if not Result.IsSuccess then
Exit;
ServerStr := ServerName;
UserStr := UserName;
DomainStr := DomainName;
PasswordStr := Password;
Result.Location := 'ITaskService::Connect';
Result.HResult := TaskService.Connect(VarFromWideString(ServerStr),
VarFromWideString(DomainStr), VarFromWideString(UserStr),
VarFromWideString(PasswordStr));
end;
function ComxTaskSchedulerOpenFolder;
begin
// Connect if necessary
if not Assigned(TaskService) then
begin
Result := ComxTaskSchedulerConnect(TaskService);
if not Result.IsSuccess then
Exit;
end;
Result.Location := 'ITaskService::GetFolder';
Result.LastCall.Parameter := Path;
Result.HResult := TaskService.GetFolder(Path, TaskFolder);
end;
function ComxTaskSchedulerEnumerateFolders;
var
FolderCollection: ITaskFolderCollection;
Count: Cardinal;
i: Integer;
begin
Result.Location := 'ITaskFolder::GetFolders';
Result.HResult := TaskFolder.GetFolders(0, FolderCollection);
if not Result.IsSuccess then
Exit;
Result.Location := 'ITaskFolderCollection::get_Count';
Result.HResult := FolderCollection.get_Count(Count);
if not Result.IsSuccess then
Exit;
SetLength(SubFolders, Count);
for i := 0 to High(SubFolders) do
begin
Result.Location := 'ITaskFolderCollection::get_Item';
Result.HResult := FolderCollection.get_Item(VarFromInteger(Succ(i)),
SubFolders[i]);
if not Result.IsSuccess then
Exit;
end;
end;
function ComxTaskSchedulerIterateFolders;
var
FolderCollection: ITaskFolderCollection;
Index, Count: Cardinal;
begin
// Task folder indexing starts with 1
FolderCollection := nil;
Index := 1;
Result := NtxAuto.Iterate<ITaskFolder>(Status,
function (out Entry: ITaskFolder): TNtxStatus
begin
// Initialize the task folder collection
if not Assigned(FolderCollection) then
begin
Result.Location := 'ITaskFolder::GetFolders';
Result.HResult := TaskFolder.GetFolders(0, FolderCollection);
if not Result.IsSuccess then
Exit;
Result.Location := 'ITaskFolderCollection::get_Count';
Result.HResult := FolderCollection.get_Count(Count);
if not Result.IsSuccess then
Exit;
end;
if Index <= Count then
begin
// Retrieve an entry by index
Result.Location := 'ITaskFolderCollection::get_Item';
Result.HResult := FolderCollection.get_Item(VarFromCardinal(Index),
Entry)
end
else
begin
// Report the end
Result.Location := 'ComxTaskSchedulerIterateFolders';
Result.Status := STATUS_NO_MORE_ENTRIES;
end;
if not Result.IsSuccess then
Exit;
// Advance to the next entry
Inc(Index);
end
);
end;
function ComxTaskSchedulerEnumerateTasks;
var
TaskCollection: IRegisteredTaskCollection;
Count: Cardinal;
i: Integer;
begin
Result.Location := 'ITaskFolder::GetTasks';
Result.HResult := TaskFolder.GetTasks(Flags, TaskCollection);
if not Result.IsSuccess then
Exit;
Result.Location := 'IRegisteredTaskCollection::get_Count';
Result.HResult := TaskCollection.get_Count(Count);
if not Result.IsSuccess then
Exit;
SetLength(Tasks, Count);
for i := 0 to High(Tasks) do
begin
Result.Location := 'IRegisteredTaskCollection::get_Item';
Result.HResult := TaskCollection.get_Item(VarFromInteger(Succ(i)),
Tasks[i]);
if not Result.IsSuccess then
Exit;
end;
end;
function ComxTaskSchedulerIterateTasks;
var
TaskCollection: IRegisteredTaskCollection;
Index, Count: Cardinal;
begin
// Task indexing starts with 1
TaskCollection := nil;
Index := 1;
Result := NtxAuto.Iterate<IRegisteredTask>(Status,
function (out Entry: IRegisteredTask): TNtxStatus
begin
// Initialize the task collection
if not Assigned(TaskCollection) then
begin
Result.Location := 'ITaskFolder::GetTasks';
Result.HResult := TaskFolder.GetTasks(Flags, TaskCollection);
if not Result.IsSuccess then
Exit;
Result.Location := 'IRegisteredTaskCollection::get_Count';
Result.HResult := TaskCollection.get_Count(Count);
if not Result.IsSuccess then
Exit;
end;
if Index <= Count then
begin
// Retrieve an entry by index
Result.Location := 'IRegisteredTaskCollection::get_Item';
Result.HResult := TaskCollection.get_Item(VarFromCardinal(Index),
Entry)
end
else
begin
// Report the end
Result.Location := 'ComxTaskSchedulerIterateTasks';
Result.Status := STATUS_NO_MORE_ENTRIES;
end;
if not Result.IsSuccess then
Exit;
// Advance to the next entry
Inc(Index);
end
);
end;
function ComxTaskSchedulerOpenTask;
var
ParentName, ChildName: String;
begin
// We need to open the task relative to something. If not explicitly given a
// root, either open the global one or the closest parent path, depending on
// the preference.
// Split or use as-is
if Assigned(Root) or (RootMode <> rmUseClosestParent) or not
RtlxSplitPath(Path, ParentName, ChildName) then
begin
ParentName := '';
ChildName := Path;
end;
// Open the root if not provided
if not Assigned(Root) then
begin
Result := ComxTaskSchedulerOpenFolder(Root, ParentName);
if not Result.IsSuccess then
Exit;
end;
Result.Location := 'ITaskFolder::GetTask';
Result.LastCall.Parameter := Path;
Result.HResult := Root.GetTask(ChildName, Task);
end;
end.