Class: Win32::TaskScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/win32/taskscheduler.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =

The version of the win32-taskscheduler library

'0.2.0'
IDLE =

Shorthand constants

IDLE_PRIORITY_CLASS
NORMAL =
NORMAL_PRIORITY_CLASS
HIGH =
HIGH_PRIORITY_CLASS
REALTIME =
REALTIME_PRIORITY_CLASS
BELOW_NORMAL =
BELOW_NORMAL_PRIORITY_CLASS
ABOVE_NORMAL =
ABOVE_NORMAL_PRIORITY_CLASS
ONCE =
TASK_TIME_TRIGGER_ONCE
DAILY =
TASK_TIME_TRIGGER_DAILY
WEEKLY =
TASK_TIME_TRIGGER_WEEKLY
MONTHLYDATE =
TASK_TIME_TRIGGER_MONTHLYDATE
MONTHLYDOW =
TASK_TIME_TRIGGER_MONTHLYDOW
ON_IDLE =
TASK_EVENT_TRIGGER_ON_IDLE
AT_SYSTEMSTART =
TASK_EVENT_TRIGGER_AT_SYSTEMSTART
AT_LOGON =
TASK_EVENT_TRIGGER_AT_LOGON
FIRST_WEEK =
TASK_FIRST_WEEK
SECOND_WEEK =
TASK_SECOND_WEEK
THIRD_WEEK =
TASK_THIRD_WEEK
FOURTH_WEEK =
TASK_FOURTH_WEEK
LAST_WEEK =
TASK_LAST_WEEK
SUNDAY =
TASK_SUNDAY
MONDAY =
TASK_MONDAY
TUESDAY =
TASK_TUESDAY
WEDNESDAY =
TASK_WEDNESDAY
THURSDAY =
TASK_THURSDAY
FRIDAY =
TASK_FRIDAY
SATURDAY =
TASK_SATURDAY
JANUARY =
TASK_JANUARY
FEBRUARY =
TASK_FEBRUARY
MARCH =
TASK_MARCH
APRIL =
TASK_APRIL
MAY =
TASK_MAY
JUNE =
TASK_JUNE
JULY =
TASK_JULY
AUGUST =
TASK_AUGUST
SEPTEMBER =
TASK_SEPTEMBER
OCTOBER =
TASK_OCTOBER
NOVEMBER =
TASK_NOVEMBER
DECEMBER =
TASK_DECEMBER
INTERACTIVE =
TASK_FLAG_INTERACTIVE
DELETE_WHEN_DONE =
TASK_FLAG_DELETE_WHEN_DONE
DISABLED =
TASK_FLAG_DISABLED
START_ONLY_IF_IDLE =
TASK_FLAG_START_ONLY_IF_IDLE
KILL_ON_IDLE_END =
TASK_FLAG_KILL_ON_IDLE_END
DONT_START_IF_ON_BATTERIES =
TASK_FLAG_DONT_START_IF_ON_BATTERIES
KILL_IF_GOING_ON_BATTERIES =
TASK_FLAG_KILL_IF_GOING_ON_BATTERIES
RUN_ONLY_IF_DOCKED =
TASK_FLAG_RUN_ONLY_IF_DOCKED
HIDDEN =
TASK_FLAG_HIDDEN
RUN_IF_CONNECTED_TO_INTERNET =
TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET
RESTART_ON_IDLE_RESUME =
TASK_FLAG_RESTART_ON_IDLE_RESUME
SYSTEM_REQUIRED =
TASK_FLAG_SYSTEM_REQUIRED
RUN_ONLY_IF_LOGGED_ON =
TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
FLAG_HAS_END_DATE =
TASK_TRIGGER_FLAG_HAS_END_DATE
FLAG_KILL_AT_DURATION_END =
TASK_TRIGGER_FLAG_KILL_AT_DURATION_END
FLAG_DISABLED =
TASK_TRIGGER_FLAG_DISABLED
MAX_RUN_TIMES =
TASK_MAX_RUN_TIMES

Instance Method Summary collapse

Constructor Details

#initialize(work_item = nil, trigger = nil) ⇒ TaskScheduler

Returns a new TaskScheduler object. If a work_item (and possibly the the trigger) are passed as arguments then a new work item is created and associated with that trigger, although you can still activate other tasks with the same handle.

This is really just a bit of convenience. Passing arguments to the constructor is the same as calling TaskScheduler.new plus TaskScheduler#new_work_item.



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
# File 'lib/win32/taskscheduler.rb', line 158

def initialize(work_item=nil, trigger=nil)
   @pITS   = nil
   @pITask = nil

   hr = CoInitialize(0)

   if SUCCEEDED(hr)
      ptr = 0.chr * 4

      hr = CoCreateInstance(
         CLSID_CTaskScheduler,
         nil,
         CLSCTX_INPROC_SERVER,
         IID_ITaskScheduler,
         ptr
      )

      if FAILED(hr)
         raise Error, get_last_error
      end

      @pITS = ptr.unpack('L').first
   else
      raise Error, get_last_error
   end

   if work_item
      if trigger
         raise TypeError unless trigger.is_a?(Hash)
         new_work_item(work_item, trigger)
      end
   end

   self
end

Instance Method Details

#account_informationObject

Returns the user associated with the task or nil if no user has yet been associated with the task.

Raises:



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/win32/taskscheduler.rb', line 516

def 
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 128

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table,lpVtbl.unpack('L').first, 128)
   table = table.unpack('L*')

   getAccountInformation = Win32::API::Function.new(table[31], 'PP', 'L')

   ptr = 0.chr * 4
   hr = getAccountInformation.call(@pITask, ptr)

   if hr == 0x8004130F # SCHED_E_ACCOUNT_INFORMATION_NOT_SET
      user = nil
   elsif hr >= 0 && hr != 0x80041312 # SCHED_E_NO_SECURITY_SERVICES
      str = 0.chr * 256
      wcscpy(str, ptr.unpack('L').first)
      CoTaskMemFree(ptr.unpack('L').first)
      user = wide_to_multi(str)            
   else
      CoTaskMemFree(p.unpack('L').first)
      raise Error,get_last_error(hr)
   end

   user
end

#activate(task) ⇒ Object

Activate the specified task.

Raises:



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
# File 'lib/win32/taskscheduler.rb', line 254

def activate(task)
   raise Error, 'null pointer' if @pITS.nil?
   raise TypeError unless task.is_a?(String)

   task = multi_to_wide(task)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 28

   memcpy(lpVtbl, @pITS, 4)
   memcpy(table, lpVtbl.unpack('L').first, 28)
   table = table.unpack('L*')

   activate = Win32::API::Function.new(table[6], 'PPPP', 'L')

   ptr = 0.chr * 4
   hr  = activate.call(@pITS, task, IID_ITask, ptr)

   if hr != S_OK
      raise Error, get_last_error
   end

   @pITask = ptr.unpack('L').first

   self
end

#add_trigger(index, trigger) ⇒ Object

Adds a trigger at the specified index.

Raises:



1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/win32/taskscheduler.rb', line 1190

def add_trigger(index, trigger)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless trigger.is_a?(Hash)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 28

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 28)
   table = table.unpack('L*')

   getTrigger = Win32::API::Function.new(table[6], 'PLP', 'L')
   ptr = 0.chr * 4
   hr = getTrigger.call(@pITask, index, ptr)

   if hr != S_OK
      raise Error, get_last_error
   end            

   pITaskTrigger = ptr.unpack('L').first
   lpVtbl = 0.chr * 4
   table = 0.chr * 16

   memcpy(lpVtbl, pITaskTrigger,4)
   memcpy(table, lpVtbl.unpack('L').first,16)
   table = table.unpack('L*')

   release = Win32::API::Function.new(table[2], 'P', 'L')
   setTrigger = Win32::API::Function.new(table[3], 'PP', 'L')
      
   type1 = 0
   type2 = 0
   tmp = trigger['type']
   tmp = nil unless tmp.is_a?(Hash)

   case trigger['trigger_type']
      when TASK_TIME_TRIGGER_DAILY
         if tmp && tmp['days_interval']
            type1 = [tmp['days_interval'],0].pack('SS').unpack('L').first
         end
      when TASK_TIME_TRIGGER_WEEKLY
         if tmp && tmp['weeks_interval'] && tmp['days_of_week']
            type1 = [tmp['weeks_interval'],tmp['days_of_week']].pack('SS').unpack('L').first
         end                
      when TASK_TIME_TRIGGER_MONTHLYDATE
         if tmp && tmp['months'] && tmp['days']
            type2 = [tmp['months'],0].pack('SS').unpack('L').first
            type1 = tmp['days']
         end                
      when TASK_TIME_TRIGGER_MONTHLYDOW
         if tmp && tmp['weeks'] && tmp['days_of_week'] && tmp['months']
            type1 = [tmp['weeks'],tmp['days_of_week']].pack('SS').unpack('L').first
            type2 = [tmp['months'],0].pack('SS').unpack('L').first
         end      
      when TASK_TIME_TRIGGER_ONCE
         # Do nothing. The Type member of the TASK_TRIGGER struct is ignored.
      else
         raise Error, 'Unknown trigger type'
   end

   pTrigger = [
      48,
      0,
      trigger['start_year'] || 0,
      trigger['start_month'] || 0,
      trigger['start_day'] || 0,
      trigger['end_year'] || 0,
      trigger['end_month'] || 0,
      trigger['end_day'] || 0,
      trigger['start_hour'] || 0,
      trigger['start_minute'] || 0,
      trigger['minutes_duration'] || 0,
      trigger['minutes_interval'] || 0,
      trigger['flags'] || 0,
      trigger['trigger_type'] || 0,
      type1,
      type2,
      0,
      trigger['random_minutes_interval'] || 0
   ].pack('S10L4LLSS')

   hr = setTrigger.call(pITaskTrigger, pTrigger)

   if hr != S_OK
      raise Error, get_last_error
   end

   release.call(pITaskTrigger)
   true
end

#application_nameObject

Returns the name of the application associated with the task.

Raises:



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/win32/taskscheduler.rb', line 549

def application_name
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 136

   memcpy(lpVtbl, @pITask,4)
   memcpy(table, lpVtbl.unpack('L').first, 136)
   table = table.unpack('L*')

   getApplicationName = Win32::API::Function.new(table[33],'PP','L')

   ptr = 0.chr * 4
   hr  = getApplicationName.call(@pITask, ptr)

   if hr >= S_OK
      str = 0.chr * 256
      wcscpy(str, ptr.unpack('L').first)
      app = wide_to_multi(str)
      CoTaskMemFree(ptr.unpack('L').first)
   else
      raise Error, get_last_error
   end

   app
end

#application_name=(app) ⇒ Object

Sets the application name associated with the task.

Raises:



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/win32/taskscheduler.rb', line 579

def application_name=(app)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless app.is_a?(String)

   app_w = multi_to_wide(app)

   lpVtbl = 0.chr * 4
   table = 0.chr * 132
   memcpy(lpVtbl,@pITask,4)
   memcpy(table,lpVtbl.unpack('L').first,132)
   table = table.unpack('L*')
   setApplicationName = Win32::API::Function.new(table[32],'PP','L')

   hr = setApplicationName.call(@pITask,app_w)

   if hr != S_OK
      raise Error,get_last_error(hr)
   end
   app
end

#commentObject

Returns the comment associated with the task, if any.

Raises:



1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
# File 'lib/win32/taskscheduler.rb', line 1393

def comment
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 80

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 80)
   table = table.unpack('L*')

   getComment = Win32::API::Function.new(table[19], 'PP', 'L')
   ptr = 0.chr * 4
   hr = getComment.call(@pITask, ptr)

   if hr != S_OK
      raise Error,get_last_error
   end

   str = 0.chr * 256
   wcscpy(str, ptr.unpack('L').first)
   CoTaskMemFree(ptr.unpack('L').first)
   wide_to_multi(str)
end

#comment=(comment) ⇒ Object

Sets the comment for the task.

Raises:



1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/win32/taskscheduler.rb', line 1420

def comment=(comment)
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless comment.is_a?(String)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 76

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 76)
   table = table.unpack('L*')

   setComment = Win32::API::Function.new(table[18], 'PP', 'L')
   comment_w = multi_to_wide(comment) 
   hr = setComment.call(@pITask, comment_w)

   if hr != S_OK
      raise Error, get_last_error
   end

   comment
end

#creatorObject

Returns the name of the user who created the task.

Raises:



1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
# File 'lib/win32/taskscheduler.rb', line 1445

def creator
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 88

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 88)
   table = table.unpack('L*')

   getCreator = Win32::API::Function.new(table[21], 'PP', 'L')
   ptr = 0.chr * 4
   hr = getCreator.call(@pITask, ptr)

   if hr != S_OK
      raise Error, get_last_error
   end

   str = 0.chr * 256
   wcscpy(str, ptr.unpack('L').first)
   CoTaskMemFree(ptr.unpack('L').first)
   wide_to_multi(str)
end

#creator=(creator) ⇒ Object

Sets the creator for the task.

Raises:



1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
# File 'lib/win32/taskscheduler.rb', line 1472

def creator=(creator)
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless creator.is_a?(String)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 84

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 84)
   table = table.unpack('L*')

   setCreator = Win32::API::Function.new(table[20], 'PP', 'L')
   creator_w = multi_to_wide(creator) 
   hr = setCreator.call(@pITask, creator_w)

   if hr != S_OK
      raise Error, get_last_error
   end

   creator
end

#delete(task) ⇒ Object

Delete the specified task name.

Raises:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/win32/taskscheduler.rb', line 283

def delete(task)
   raise Error, 'null pointer' if @pITS.nil?
   raise TypeError unless task.is_a?(String)

   task = multi_to_wide(task)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 32

   memcpy(lpVtbl, @pITS, 4)
   memcpy(table, lpVtbl.unpack('L').first, 32)
   table = table.unpack('L*')

   delete = Win32::API::Function.new(table[7], 'PP', 'L')

   hr = delete.call(@pITS,task)

   if hr != S_OK
      raise Error, get_last_error
   end

   self
end

#delete_trigger(index) ⇒ Object

Deletes the trigger at the specified index.

Raises:



978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
# File 'lib/win32/taskscheduler.rb', line 978

def delete_trigger(index)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 20

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 20)
   table = table.unpack('L*')

   deleteTrigger = Win32::API::Function.new(table[4], 'PL', 'L')
   hr = deleteTrigger.call(@pITask,index)

   if hr != S_OK
      raise Error, get_last_error
   end

   index
end

#enumObject Also known as: tasks

Returns an array of scheduled task names.

Raises:



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
# File 'lib/win32/taskscheduler.rb', line 196

def enum
   raise Error, 'null pointer' if @pITS.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 24

   memcpy(lpVtbl, @pITS, 4)
   memcpy(table, lpVtbl.unpack('L').first, 24)
   table = table.unpack('L*')

   enum = Win32::API::Function.new(table[5], 'PP', 'L')

   ptr = 0.chr * 4
   hr  = enum.call(@pITS, ptr)

   raise Error, get_last_error if hr != S_OK

   pIEnum = ptr.unpack('L').first
   lpVtbl = 0.chr * 4
   table  = 0.chr * 16

   memcpy(lpVtbl, pIEnum, 4)
   memcpy(table, lpVtbl.unpack('L').first, 16)
   table = table.unpack('L*')

   _next   = Win32::API::Function.new(table[3], 'PLPP', 'L')
   release = Win32::API::Function.new(table[2], 'P', 'L')

   array = []
   fetched_tasks = 0.chr * 4
   pnames = 0.chr * 4

   while (_next.call(pIEnum, TASKS_TO_RETRIEVE, pnames, fetched_tasks) >= S_OK) &&
         (fetched_tasks.unpack('L').first != 0)

      tasks = fetched_tasks.unpack('L').first
      names = 0.chr * 4 * tasks
      memcpy(names, pnames.unpack('L').first, 4 * tasks)

      for i in 0 ... tasks
         str = 0.chr * 256
         wcscpy(str, names[i*4, 4].unpack('L').first)
         array.push(wide_to_multi(str))
         CoTaskMemFree(names[i*4, 4].unpack('L').first)
      end

      CoTaskMemFree(pnames.unpack('L').first)
   end

   release.call(pIEnum)

   array
end

#exists?(job_name) ⇒ Boolean

Returns whether or not the scheduled task exists.

Returns:

  • (Boolean)


1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
# File 'lib/win32/taskscheduler.rb', line 1605

def exists?(job_name)
   bool = false
   Dir.foreach('C:/Windows/Tasks'){ |file|
      if File.basename(file, '.job') == job_name
         bool = true
         break     
      end 
   }
   bool
end

#exit_codeObject

Returns the exit code from the last scheduled run.

Raises:



1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
# File 'lib/win32/taskscheduler.rb', line 1369

def exit_code
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table = 0.chr * 72

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 72)
   table = table.unpack('L*')

   getExitCode = Win32::API::Function.new(table[17], 'PP', 'L')
   ptr = 0.chr * 4
   hr = getExitCode.call(@pITask, ptr)

   if hr > 0x80000000
      raise Error, get_last_error
   end

   exit_code = ptr.unpack('L').first
end

#flagsObject

Returns the flags (integer) that modify the behavior of the work item. You must OR the return value to determine the flags yourself.

Raises:



1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'lib/win32/taskscheduler.rb', line 1285

def flags
   raise Error, 'null pointer' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 120

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 120)
   table = table.unpack('L*')

   getFlags = Win32::API::Function.new(table[29], 'PP', 'L')
   ptr = 0.chr * 4
   hr = getFlags.call(@pITask, ptr)

   if hr != S_OK
       raise Error, get_last_error
   end

   flags = ptr.unpack('L').first
end

#flags=(flags) ⇒ Object

Sets an OR’d value of flags that modify the behavior of the work item.

Raises:



1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
# File 'lib/win32/taskscheduler.rb', line 1308

def flags=(flags)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 116

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 116)
   table = table.unpack('L*')

   setFlags = Win32::API::Function.new(table[28], 'PL', 'L')
   hr = setFlags.call(@pITask, flags)

   if hr != S_OK
      raise Error, get_last_error
   end

   flags
end

#machine=(host) ⇒ Object Also known as: host=

Set the host on which the various TaskScheduler methods will execute.

Raises:



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/win32/taskscheduler.rb', line 435

def machine=(host)
   raise Error, 'null pointer' if @pITS.nil?
   raise TypeError unless host.is_a?(String)

   host_w = multi_to_wide(host)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 16

   memcpy(lpVtbl, @pITS, 4)
   memcpy(table, lpVtbl.unpack('L').first, 16)
   table = table.unpack('L*')

   setTargetComputer = Win32::API::Function.new(table[3], 'PP', 'L')

   hr = setTargetComputer.call(@pITS, host_w)

   if hr != S_OK
      raise Error, get_last_error
   end

   host
end

#max_run_timeObject

Returns the maximum length of time, in milliseconds, that the task will run before terminating.

Raises:



1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
# File 'lib/win32/taskscheduler.rb', line 1556

def max_run_time
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 176

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 176)
   table = table.unpack('L*')

   getMaxRunTime = Win32::API::Function.new(table[43], 'PP', 'L')

   ptr = 0.chr * 4
   hr = getMaxRunTime.call(@pITask, ptr)

   if hr != S_OK
      raise Error, get_last_error
   end

   max_run_time = ptr.unpack('L').first
end

#max_run_time=(max_run_time) ⇒ Object

Sets the maximum length of time, in milliseconds, that the task can run before terminating. Returns the value you specified if successful.

Raises:



1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
# File 'lib/win32/taskscheduler.rb', line 1582

def max_run_time=(max_run_time)
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless max_run_time.is_a?(Numeric)
      
   lpVtbl = 0.chr * 4
   table  = 0.chr * 172

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 172)
   table = table.unpack('L*')

   setMaxRunTime = Win32::API::Function.new(table[42], 'PL', 'L')
   hr = setMaxRunTime.call(@pITask, max_run_time)

   if hr != S_OK
      raise Error,get_last_error
   end

   max_run_time
end

#most_recent_run_timeObject

Returns a Time object indicating the most recent time the task ran or nil if the task has never run.

Raises:



1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
# File 'lib/win32/taskscheduler.rb', line 1525

def most_recent_run_time
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 64

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 64)
   table = table.unpack('L*')

   getMostRecentRunTime = Win32::API::Function.new(table[15], 'PP', 'L')
   st = 0.chr * 16
   hr = getMostRecentRunTime.call(@pITask, st)

   if hr == 0x00041303 # SCHED_S_TASK_HAS_NOT_RUN
      time = nil
   elsif hr == S_OK
      a1, a2, _, a3, a4, a5, a6, a7 = st.unpack('S*')
      a7 *= 1000
      time = Time.local(a1, a2, a3, a4, a5, a6, a7)     
   else
      raise Error, get_last_error
   end

   time
end

#new_work_item(task, trigger) ⇒ Object Also known as: new_task

Creates a new work item (scheduled job) with the given trigger. The trigger variable is a hash of options that define when the scheduled job should run.

Raises:

  • (TypeError)


786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'lib/win32/taskscheduler.rb', line 786

def new_work_item(task, trigger)
   raise TypeError unless trigger.is_a?(Hash)
   raise Error, 'null pointer' if @pITS.nil?

   trigger = transform_and_validate(trigger)

   if @pITask
      lpVtbl = 0.chr * 4
      table  = 0.chr * 12

      memcpy(lpVtbl, @pITask, 4)
      memcpy(table, lpVtbl.unpack('L').first, 12)
      table = table.unpack('L*')

      release = Win32::API::Function.new(table[2], 'P', 'L')
      release.call(@pITask)

      @pITask = nil
   end

   task = multi_to_wide(task)
   lpVtbl = 0.chr * 4
   table  = 0.chr * 36

   memcpy(lpVtbl, @pITS, 4)
   memcpy(table, lpVtbl.unpack('L').first, 36)
   table = table.unpack('L*')

   newWorkItem = Win32::API::Function.new(table[8], 'PPPPP', 'L')

   ptr = 0.chr * 4

   hr = newWorkItem.call(@pITS, task, CLSID_CTask, IID_ITask, ptr)

   if FAILED(hr)
      raise Error, get_last_error
   end

   @pITask = ptr.unpack('L').first
   lpVtbl = 0.chr * 4
   table  = 0.chr * 16

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 16)
   table = table.unpack('L*')

   createTrigger = Win32::API::Function.new(table[3], 'PPP', 'L')
   p1 = 0.chr * 4
   p2 = 0.chr * 4

   hr = createTrigger.call(@pITask, p1, p2)

   if hr != S_OK
      raise Error, get_last_error
   end

   pITaskTrigger = p2.unpack('L').first
   lpVtbl = 0.chr * 4
   table  = 0.chr * 16

   memcpy(lpVtbl, pITaskTrigger, 4)
   memcpy(table, lpVtbl.unpack('L').first, 16)
   table = table.unpack('L*')

   release = Win32::API::Function.new(table[2], 'P', 'L')
   setTrigger = Win32::API::Function.new(table[3], 'PP', 'L')
      
   type1 = 0
   type2 = 0
   tmp = trigger['type']
   tmp = nil unless tmp.is_a?(Hash)

   case trigger['trigger_type']
      when TASK_TIME_TRIGGER_DAILY
         if tmp && tmp['days_interval']
            type1 = [tmp['days_interval'],0].pack('SS').unpack('L').first
         end
      when TASK_TIME_TRIGGER_WEEKLY
         if tmp && tmp['weeks_interval'] && tmp['days_of_week']
            type1 = [tmp['weeks_interval'],tmp['days_of_week']].pack('SS').unpack('L').first
         end                
      when TASK_TIME_TRIGGER_MONTHLYDATE
         if tmp && tmp['months'] && tmp['days']
            type2 = [tmp['months'],0].pack('SS').unpack('L').first
            type1 = tmp['days']
         end                
      when TASK_TIME_TRIGGER_MONTHLYDOW
         if tmp && tmp['weeks'] && tmp['days_of_week'] && tmp['months']
            type1 = [tmp['weeks'],tmp['days_of_week']].pack('SS').unpack('L').first
            type2 = [tmp['months'],0].pack('SS').unpack('L').first
         end      
      when TASK_TIME_TRIGGER_ONCE
         # Do nothing. The Type member of the TASK_TRIGGER struct is ignored.
      else
         raise Error, 'Unknown trigger type'
   end

   pTrigger = [
      48,
      0,
      trigger['start_year'] || 0,
      trigger['start_month'] || 0,
      trigger['start_day'] || 0,
      trigger['end_year'] || 0,
      trigger['end_month'] || 0,
      trigger['end_day'] || 0,
      trigger['start_hour'] || 0,
      trigger['start_minute'] || 0,
      trigger['minutes_duration'] || 0,
      trigger['minutes_interval'] || 0,
      trigger['flags'] || 0,
      trigger['trigger_type'] || 0,
      type1,
      type2,
      0,
      trigger['random_minutes_interval'] || 0
   ].pack('S10L4LLSS')

   hr = setTrigger.call(pITaskTrigger, pTrigger)

   if hr != S_OK
      raise Error, get_last_error
   end

   release.call(pITaskTrigger)

   self
end

#next_run_timeObject

Returns a Time object that indicates the next time the task will run.

Raises:



1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
# File 'lib/win32/taskscheduler.rb', line 1497

def next_run_time
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 40

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 40)
   table = table.unpack('L*')

   getNextRunTime = Win32::API::Function.new(table[9], 'PP', 'L')
   st = 0.chr * 16
   hr = getNextRunTime.call(@pITask, st)

   if hr != S_OK
      raise Error, get_last_error
   end

   a1,a2,_,a3,a4,a5,a6,a7 = st.unpack('S*')
   a7 *= 1000

   Time.local(a1,a2,a3,a4,a5,a6,a7)
end

#parametersObject

Returns the command line parameters for the task.

Raises:



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/win32/taskscheduler.rb', line 603

def parameters
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 144

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 144)
   table = table.unpack('L*')

   getParameters = Win32::API::Function.new(table[35], 'PP', 'L')
   ptr = 0.chr * 4
   hr = getParameters.call(@pITask, ptr)

   if hr >= S_OK
      str = 0.chr * 256
      wcscpy(str, ptr.unpack('L').first)
      param = wide_to_multi(str)
      CoTaskMemFree(ptr.unpack('L').first)
   else
      raise Error, get_last_error
   end

   param
end

#parameters=(param) ⇒ Object

Sets the parameters for the task. These parameters are passed as command line arguments to the application the task will run. To clear the command line parameters set it to an empty string.

Raises:



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/win32/taskscheduler.rb', line 634

def parameters=(param)
   raise Error, 'null pointer(ts_set_parameters)' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless param.is_a?(String)

   param_w = multi_to_wide(param)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 140

   memcpy(lpVtbl,@pITask,4)
   memcpy(table,lpVtbl.unpack('L').first,140)
   table = table.unpack('L*')

   setParameters = Win32::API::Function.new(table[34],'PP','L')
   hr = setParameters.call(@pITask,param_w)

   if hr != S_OK
      raise Error, get_last_error(hr)
   end
   param
end

#priorityObject

Returns the task’s priority level. Possible values are ‘idle’, ‘normal’, ‘high’, ‘realtime’, ‘below_normal’, ‘above_normal’, and ‘unknown’.

Raises:



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/win32/taskscheduler.rb', line 717

def priority
   raise Error, 'null pointer(ts_get_priority)' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 160

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 160)
   table = table.unpack('L*')

   getPriority = Win32::API::Function.new(table[39], 'PP', 'L')

   ptr = 0.chr * 4
   hr  = getPriority.call(@pITask, ptr)

   if hr >= S_OK
      pri = ptr.unpack('L').first
      if (pri & IDLE_PRIORITY_CLASS) != 0
         priority = 'idle'
      elsif (pri & NORMAL_PRIORITY_CLASS) != 0
         priority = 'normal'
      elsif (pri & HIGH_PRIORITY_CLASS) != 0
         priority = 'high'
      elsif (pri & REALTIME_PRIORITY_CLASS) != 0
         priority = 'realtime'
      elsif (pri & BELOW_NORMAL_PRIORITY_CLASS) != 0
         priority = 'below_normal'
      elsif (pri & ABOVE_NORMAL_PRIORITY_CLASS) != 0
         priority = 'above_normal'
      else
         priority = 'unknown'
      end
   else
      raise Error, get_last_error
   end

   priority
end

#priority=(priority) ⇒ Object

Sets the priority of the task. The priority should be a numeric priority constant value.

Raises:



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/win32/taskscheduler.rb', line 760

def priority=(priority)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless priority.is_a?(Numeric)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 156

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 156)
   table = table.unpack('L*')

   setPriority = Win32::API::Function.new(table[38], 'PL', 'L')
   hr = setPriority.call(@pITask, priority)

   if hr != S_OK
      raise Error, get_last_error(hr)
   end

   priority
end

#runObject

Execute the current task.

Raises:



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/win32/taskscheduler.rb', line 309

def run
   raise Error, 'null pointer' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 52

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 52)
   table = table.unpack('L*')

   run = Win32::API::Function.new(table[12], 'P', 'L')

   hr = run.call(@pITask)

   if hr != S_OK
      raise Error,get_last_error
   end

   self
end

#save(file = nil) ⇒ Object

Saves the current task. Tasks must be saved before they can be activated. The .job file itself is typically stored in the C:WINDOWSTasks folder.

If file (an absolute path) is specified then the job is saved to that file instead. A ‘.job’ extension is recommended but not enforced.

Note that calling TaskScheduler#save also resets the TaskScheduler object so that there is no currently active task.

Raises:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/win32/taskscheduler.rb', line 339

def save(file = nil)
   raise Error, 'null pointer' if @pITask.nil?
   file = multi_to_wide(file) if file

   lpVtbl = 0.chr * 4
   table  = 0.chr * 12

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 12)
   table = table.unpack('L*')

   queryinterface = Win32::API::Function.new(table[0],'PPP','L')
   release = Win32::API::Function.new(table[2],'P','L')

   ptr = 0.chr * 4

   hr = queryinterface.call(@pITask, IID_IPersistFile, ptr)

   if hr != S_OK
      raise Error, get_last_error
   end

   pIPersistFile = ptr.unpack('L').first

   lpVtbl = 0.chr * 4
   table = 0.chr * 28

   memcpy(lpVtbl, pIPersistFile,4)
   memcpy(table, lpVtbl.unpack('L').first, 28)
   table = table.unpack('L*')

   save = Win32::API::Function.new(table[6],'PPL','L')
   release = Win32::API::Function.new(table[2],'P','L')

   hr = save.call(pIPersistFile,file,1)

   if hr != S_OK
      raise Error,get_last_error
   end

   release.call(pIPersistFile)

   CoUninitialize()
   hr = CoInitialize(nil)

   if hr >= 0
      ptr = 0.chr * 4

      hr = CoCreateInstance(
         CLSID_CTaskScheduler,
         nil,
         CLSCTX_INPROC_SERVER,
         IID_ITaskScheduler,
         ptr
      )

      if hr != S_OK
         CoUninitialize()
         raise Error, get_last_error
      end

      @pITS = ptr.unpack('L').first
   else
      raise Error,get_last_error
   end

   release.call(@pITask)
   @pITask = nil

   self
end

#set_account_information(user, password) ⇒ Object

Sets the user and password for the given task. If the user and password are set properly then true is returned.

In some cases the job may be created, but the account information was bad. In this case the task is created but a warning is generated and false is returned.

Raises:



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/win32/taskscheduler.rb', line 468

def (user, password)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 124

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 124)
   table = table.unpack('L*')

   setAccountInformation = Win32::API::Function.new(table[30],'PPP','L')

   if (user.nil? || user=="") && (password.nil? || password=="")
      hr = setAccountInformation.call(@pITask, "", nil)
   else
      user = multi_to_wide(user)
      password = multi_to_wide(password)
      hr = setAccountInformation.call(@pITask, user, password)
   end

   bool = true

   case hr
      when S_OK
         return true
      when 0x80070005 # E_ACCESSDENIED
         raise Error, 'access denied'
      when 0x80070057 # E_INVALIDARG
         raise Error, 'invalid argument'
      when 0x8007000E # E_OUTOFMEMORY
         raise Error, 'out of memory'
      when 0x80041312 # SCHED_E_NO_SECURITY_SERVICES
         raise Error, 'no security services on this platform'
      when 0x80041314 # SCHED_E_UNSUPPORTED_ACCOUNT_OPTION
         raise Error, 'unsupported account option'
      when 0x8004130F # SCHED_E_ACCOUNT_INFORMATION_NOT_SET
         warn 'job created, but password was invalid'
         bool = false
      else
         raise Error, 'unknown error'
      end
   bool
end

#statusObject

Returns the status of the currently active task. Possible values are ‘ready’, ‘running’, ‘not scheduled’ or ‘unknown’.

Raises:



1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
# File 'lib/win32/taskscheduler.rb', line 1332

def status
   raise Error, 'null pointer' if @pITask.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 68

   memcpy(lpVtbl,@pITask,4)
   memcpy(table,lpVtbl.unpack('L').first,68)
   table = table.unpack('L*')

   getStatus = Win32::API::Function.new(table[16], 'PP', 'L')
   ptr = 0.chr * 4
   hr = getStatus.call(@pITask, ptr)

   if hr != S_OK
      raise Error,get_last_error
   end

   st = ptr.unpack('L').first

   case st
      when 0x00041300 # SCHED_S_TASK_READY
          status = 'ready'
      when 0x00041301 # SCHED_S_TASK_RUNNING
          status = 'running'
      when 0x00041305 # SCHED_S_TASK_NOT_SCHEDULED
          status = 'not scheduled'
      else
          status = 'unknown'
   end

   status
end

#terminateObject

Terminate the current task.

Raises:



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/win32/taskscheduler.rb', line 413

def terminate
   raise Error, 'null pointer' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 56

   memcpy(lpVtbl,@pITask,4)
   memcpy(table,lpVtbl.unpack('L').first,56)
   table = table.unpack('L*')

   teriminate = Win32::API::Function.new(table[13],'P','L')
   hr = teriminate.call(@pITask)

   if hr != S_OK
      raise Error,get_last_error
   end

   self
end

#trigger(index) ⇒ Object

Returns a hash that describes the trigger at the given index for the current task.

Raises:



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/win32/taskscheduler.rb', line 1002

def trigger(index)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 28

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 28)
   table = table.unpack('L*')

   getTrigger = Win32::API::Function.new(table[6], 'PLP', 'L')
   ptr = 0.chr * 4
   hr = getTrigger.call(@pITask, index, ptr)

   if hr != S_OK
      raise Error, get_last_error
   end            

   pITaskTrigger = ptr.unpack('L').first
   lpVtbl = 0.chr * 4
   table  = 0.chr * 20

   memcpy(lpVtbl, pITaskTrigger, 4)
   memcpy(table, lpVtbl.unpack('L').first, 20)
   table = table.unpack('L*')

   release = Win32::API::Function.new(table[2], 'P', 'L')
   getTrigger = Win32::API::Function.new(table[4], 'PP', 'L')

   pTrigger = [48].pack('S') + 0.chr * 46
   hr = getTrigger.call(pITaskTrigger, pTrigger)

   if hr != S_OK
      error = get_last_error
      release.call(pITaskTrigger)
      raise Error, error
   end

   tr = pTrigger.unpack('S10L4LLSS')

   trigger = {}
   trigger['start_year'] = tr[2]
   trigger['start_month'] = tr[3]
   trigger['start_day'] = tr[4]
   trigger['end_year'] = tr[5]
   trigger['end_month'] = tr[6]
   trigger['end_day'] = tr[7]
   trigger['start_hour'] = tr[8]
   trigger['start_minute'] = tr[9]
   trigger['minutes_duration'] = tr[10]
   trigger['minutes_interval'] = tr[11]
   trigger['flags'] = tr[12]
   trigger['trigger_type'] = tr[13]            
   trigger['random_minutes_interval'] = tr[17]

   case tr[13]
      when TASK_TIME_TRIGGER_DAILY
         tmp = {}
         tmp['days_interval'] = [tr[14]].pack('L').unpack('SS').first
         trigger['type'] = tmp
      when TASK_TIME_TRIGGER_WEEKLY
         tmp = {}
         tmp['weeks_interval'],tmp['days_of_week'] = [tr[14]].pack('L').unpack('SS')
         trigger['type'] = tmp
      when TASK_TIME_TRIGGER_MONTHLYDATE
         tmp = {}
         tmp['days'] = tr[14]
         tmp['months'] = [tr[15]].pack('L').unpack('SS').first
         trigger['type'] = tmp
      when TASK_TIME_TRIGGER_MONTHLYDOW
         tmp = {}
         tmp['weeks'],tmp['days_of_week'] = [tr[14]].pack('L').unpack('SS')
         tmp['months'] = [tr[15]].pack('L').unpack('SS').first
         trigger['type'] = tmp
      when TASK_TIME_TRIGGER_ONCE
         tmp = {}
         tmp['once'] = nil
         trigger['type'] = tmp
       else
         raise Error, 'Unknown trigger type'
   end

   release.call(pITaskTrigger)

   trigger
end

#trigger=(trigger) ⇒ Object

Sets the trigger for the currently active task.

Raises:



1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/win32/taskscheduler.rb', line 1092

def trigger=(trigger)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless trigger.is_a?(Hash)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 16

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 16)
   table = table.unpack('L*')

   createTrigger = Win32::API::Function.new(table[3], 'PPP', 'L')

   p1 = 0.chr * 4
   p2 = 0.chr * 4

   hr = createTrigger.call(@pITask, p1, p2)

   if hr != S_OK
      raise Error, get_last_error
   end

   pITaskTrigger = p2.unpack('L').first
   lpVtbl = 0.chr * 4
   table  = 0.chr * 16

   memcpy(lpVtbl, pITaskTrigger, 4)
   memcpy(table, lpVtbl.unpack('L').first, 16)
   table = table.unpack('L*')

   release = Win32::API::Function.new(table[2], 'P', 'L')
   setTrigger = Win32::API::Function.new(table[3], 'PP', 'L')
      
   type1 = 0
   type2 = 0
   tmp = trigger['type']
   tmp = nil unless tmp.is_a?(Hash)

   case trigger['trigger_type']
      when TASK_TIME_TRIGGER_DAILY
         if tmp && tmp['days_interval']
            type1 = [tmp['days_interval'],0].pack('SS').unpack('L').first
         end
      when TASK_TIME_TRIGGER_WEEKLY
         if tmp && tmp['weeks_interval'] && tmp['days_of_week']
            type1 = [tmp['weeks_interval'],tmp['days_of_week']].pack('SS').unpack('L').first
         end                
      when TASK_TIME_TRIGGER_MONTHLYDATE
         if tmp && tmp['months'] && tmp['days']
            type2 = [tmp['months'],0].pack('SS').unpack('L').first
            type1 = tmp['days']
         end                
      when TASK_TIME_TRIGGER_MONTHLYDOW
         if tmp && tmp['weeks'] && tmp['days_of_week'] && tmp['months']
            type1 = [tmp['weeks'],tmp['days_of_week']].pack('SS').unpack('L').first
            type2 = [tmp['months'],0].pack('SS').unpack('L').first
         end      
      when TASK_TIME_TRIGGER_ONCE
         # Do nothing. The Type member of the TASK_TRIGGER struct is ignored.
      else
         raise Error, 'Unknown trigger type'
   end

   pTrigger = [
      48,
      0,
      trigger['start_year'] || 0,
      trigger['start_month'] || 0,
      trigger['start_day'] || 0,
      trigger['end_year'] || 0,
      trigger['end_month'] || 0,
      trigger['end_day'] || 0,
      trigger['start_hour'] || 0,
      trigger['start_minute'] || 0,
      trigger['minutes_duration'] || 0,
      trigger['minutes_interval'] || 0,
      trigger['flags'] || 0,
      trigger['trigger_type'] || 0,
      type1,
      type2,
      0,
      trigger['random_minutes_interval'] || 0
   ].pack('S10L4LLSS')

   hr = setTrigger.call(pITaskTrigger, pTrigger)

   if hr != S_OK
      raise Error, get_last_error
   end

   release.call(pITaskTrigger)      

   trigger
end

#trigger_countObject

Returns the number of triggers associated with the active task.

Raises:



919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/win32/taskscheduler.rb', line 919

def trigger_count
   raise Error, "null pointer" if @pITS.nil?
   raise Error, "No currently active task" if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 24

   memcpy(lpVtbl, @pITask,4)
   memcpy(table, lpVtbl.unpack('L').first, 24)
   table = table.unpack('L*')

   getTriggerCount = Win32::API::Function.new(table[5], 'PP', 'L')
   ptr = 0.chr * 4
   hr  = getTriggerCount.call(@pITask, ptr)

   if hr >= S_OK
      count = ptr.unpack('L').first
   else
      raise Error, get_last_error
   end

   count
end

#trigger_string(index) ⇒ Object

Returns a string that describes the current trigger at the specified index for the active task.

Example: “At 7:14 AM every day, starting 4/11/2009”

Raises:



948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/win32/taskscheduler.rb', line 948

def trigger_string(index)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless index.is_a?(Numeric)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 32

   memcpy(lpVtbl, @pITask, 4)
   memcpy(table, lpVtbl.unpack('L').first, 32)
   table = table.unpack('L*')

   getTriggerString = Win32::API::Function.new(table[7], 'PLP', 'L')
   ptr = 0.chr * 4
   hr  = getTriggerString.call(@pITask, index, ptr)

   if hr == S_OK
      str = 0.chr * 256
      wcscpy(str, ptr.unpack('L').first)
      trigger = wide_to_multi(str)
      CoTaskMemFree(ptr.unpack('L').first)                
   else
      raise Error, get_last_error
   end

   trigger
end

#working_directoryObject

Returns the working directory for the task.

Raises:



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/win32/taskscheduler.rb', line 659

def working_directory
   raise Error,"fatal error: null pointer(ts_get_parameters)" if @pITS.nil?
   raise Error,"No currently active task" if @pITask.nil?

   lpVtbl = 0.chr * 4
   table  = 0.chr * 152

   memcpy(lpVtbl, @pITask,4)
   memcpy(table, lpVtbl.unpack('L').first,152)
   table = table.unpack('L*')

   getWorkingDirectory = Win32::API::Function.new(table[37],'PP','L')

   ptr = 0.chr * 4
   hr  = getWorkingDirectory.call(@pITask, ptr)

   if hr >= S_OK
      str = 0.chr * 256
      wcscpy(str, ptr.unpack('L').first)
      dir = wide_to_multi(str)
      CoTaskMemFree(ptr.unpack('L').first)
   else
      raise Error, get_last_error
   end

   dir
end

#working_directory=(dir) ⇒ Object

Sets the working directory for the task.

Raises:



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/win32/taskscheduler.rb', line 689

def working_directory=(dir)
   raise Error, 'null pointer' if @pITS.nil?
   raise Error, 'No currently active task' if @pITask.nil?
   raise TypeError unless dir.is_a?(String)

   dir_w = multi_to_wide(dir)

   lpVtbl = 0.chr * 4
   table  = 0.chr * 148

   memcpy(lpVtbl, @pITask,4)
   memcpy(table, lpVtbl.unpack('L').first, 148)
   table = table.unpack('L*')

   setWorkingDirectory = Win32::API::Function.new(table[36], 'PP', 'L')
   hr = setWorkingDirectory.call(@pITask, dir_w)

   if hr != S_OK
      raise Error, get_last_error(hr)
   end

   dir
end