Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1409 lines
38 KiB

  1. //+-----------------------------------------------------
  2. //
  3. // file: test.cxx
  4. //
  5. // Called from main.cxx, this file actually runs the
  6. // test suite on the interfaces. We test the following:
  7. // 2. ITaskScheduler
  8. // 3. ITask
  9. // 4. ITaskTrigger
  10. // 5. IEnumWorkItem
  11. // We sort of test IUnknown, in that we use it to get
  12. // the other interfaces.
  13. //
  14. // History: 10-31-96 created
  15. //
  16. //-------------------------------------------------------
  17. #include <string.h>
  18. #include <tchar.h>
  19. #include <wchar.h>
  20. #include <windows.h>
  21. #include <objbase.h>
  22. #include <initguid.h>
  23. #include <oleguid.h>
  24. #include <mstask.h>
  25. #include <msterr.h>
  26. #include "tint.hxx"
  27. //+------------------------------------------------------
  28. //
  29. // Function: TestISchedAgent
  30. //
  31. // Synopsis: Uh. What it says.
  32. //
  33. // Arguments: None
  34. //
  35. // Returns: S_OK, but typically caller discards as
  36. // implicit cast to void.
  37. //
  38. // History: 10-31-96 created
  39. //--------------------------------------------------------
  40. HRESULT TestISchedAgent()
  41. {
  42. HRESULT hr = S_OK;
  43. // Tell where we are
  44. // wprintf(L"\n----------------------------------------------\n");
  45. // wprintf(L"Beginning TestISchedAgent\n\n");
  46. //
  47. // SetTargetComputer method
  48. //
  49. LPWSTR szMyHostName, szMyUNCNameBad;
  50. DWORD MyHostNameSize;
  51. MyHostNameSize = 32;
  52. szMyHostName = (LPWSTR) malloc(32 * sizeof(WCHAR));
  53. szMyUNCNameBad = (LPWSTR) malloc(32* sizeof(WCHAR));
  54. wcscpy(szMyUNCNameBad,L"\\FOO");
  55. // wprintf(L"SetTargetComputer setting computer to %s\n",szMyUNCNameBad);
  56. hr = g_pISchedAgent -> SetTargetComputer((LPCWSTR) szMyUNCNameBad);
  57. if (SUCCEEDED(hr))
  58. {
  59. wprintf(L"FAILURE! SetTargetComputer on bad name returned %x.\n",hr);
  60. }
  61. // wprintf(L"SetTargetComputer setting computer to localhost\n");
  62. hr = g_pISchedAgent -> SetTargetComputer(NULL);
  63. if (FAILED(hr))
  64. {
  65. wprintf(L"FAILURE! SetTargetComputer on good name returned %x.\n\n",hr);
  66. }
  67. free(szMyUNCNameBad);
  68. free(szMyHostName);
  69. //
  70. // Get Target Computer Method
  71. //
  72. LPWSTR lszMyMachine;
  73. hr = g_pISchedAgent -> GetTargetComputer(&lszMyMachine);
  74. if (FAILED(hr))
  75. wprintf(L"GetTargetComputer returned hr = %x, %s\n\n",hr,lszMyMachine);
  76. CoTaskMemFree(lszMyMachine);
  77. //
  78. // NewTask method
  79. //
  80. IUnknown *pIU;
  81. pIU = NULL;
  82. hr = g_pISchedAgent -> NewWorkItem(L"foo\bar.job", CLSID_CTask, IID_ITask, &pIU);
  83. // File is not returned as error until it is persisted.
  84. if (pIU)
  85. {
  86. IPersistFile *pIPF;
  87. hr = pIU->QueryInterface(IID_IPersistFile, (void **)&pIPF);
  88. if (FAILED(hr))
  89. {
  90. pIU->Release();
  91. pIU = NULL;
  92. wprintf(L"FAIL! Unable to get an IPersistFile for illegal new task.\n");
  93. }
  94. hr = pIPF->Save(NULL, FALSE);
  95. if (SUCCEEDED(hr))
  96. {
  97. wprintf(L"FAIL! Succeeded on saving bad task name\n");
  98. }
  99. pIU->Release();
  100. pIU = NULL;
  101. pIPF->Release();
  102. pIPF = NULL;
  103. }
  104. if (pIU != 0)
  105. {
  106. wprintf(L"FAILURE - new illegal task created\n");
  107. pIU -> Release();
  108. pIU = NULL;
  109. }
  110. else
  111. {
  112. // wprintf(L"New Task (bad) returned a pIUnknown of NULL\n");
  113. }
  114. // wprintf(L"New Task (bad) returned hr = %x\n",hr);
  115. hr = g_pISchedAgent -> NewWorkItem(L"foo.job", CLSID_CTask, IID_ITask, &pIU);
  116. if (pIU != 0)
  117. {
  118. IPersistFile *pIPF;
  119. hr = pIU->QueryInterface(IID_IPersistFile, (void **) &pIPF);
  120. if (pIPF)
  121. {
  122. hr = pIPF->Save(NULL, FALSE);
  123. if (FAILED(hr))
  124. {
  125. wprintf(L"FAIL! IPersistFile->Save, good task creation %x\n",hr);
  126. }
  127. pIPF->Release();
  128. pIPF=NULL;
  129. }
  130. pIU -> Release();
  131. pIU = NULL;
  132. }
  133. else
  134. {
  135. wprintf(L"FAILURE! New Task (good) return a pIUnknown of NULL\n");
  136. }
  137. // wprintf(L"New Task (good - foo.job) returned hr = %x\n\n",hr);
  138. //
  139. // AddTask Method
  140. //
  141. hr = g_pISchedAgent -> AddWorkItem(L"bar\foo2.job", g_pITask);
  142. if (SUCCEEDED(hr))
  143. wprintf(L"FAILURE! Add Task (bad) returned hr = %x\n",hr);
  144. hr = g_pISchedAgent -> AddWorkItem(L"foo2.job", g_pITask);
  145. if (FAILED(hr))
  146. wprintf(L"FAILURE! Add Task (good - foo2.job) returned hr = %x\n\n",hr);
  147. //
  148. // IsTask method (not currently imp)
  149. //
  150. /*
  151. hr = g_pISchedAgent -> IsTask(L"bar.job");
  152. wprintf(L"IsTask (bad) returned hr = %x\n",hr);
  153. hr = g_pISchedAgent -> IsTask(L"foo.job");
  154. wprintf(L"IsTask (good) returned hr = %x\n\n",hr);
  155. */
  156. //
  157. // We were going to test Enum here,
  158. // but wait until later when we need it.
  159. //
  160. //
  161. // Delete Method
  162. //
  163. hr = g_pISchedAgent -> Delete(L"foo.job");
  164. if (FAILED(hr))
  165. wprintf(L"FAILURE! Delete foo.job returned hr = %x\n",hr);
  166. hr = g_pISchedAgent -> Delete(L"bar.job");
  167. if (SUCCEEDED(hr))
  168. wprintf(L"FAILURE! Delete bar.job returned hr = %x\n",hr);
  169. hr = g_pISchedAgent -> Delete(L"bar\foo.job");
  170. if (SUCCEEDED(hr))
  171. wprintf(L"FAILURE! Delete bar\\foo.job returned hr = %x\n\n",hr);
  172. return S_OK;
  173. }
  174. //+----------------------------------------------------------------
  175. //
  176. // function: TestITask();
  177. //
  178. // Arguments: none
  179. //
  180. // returns: HRESULT of S_OK. E_FAIL, if bad handle in
  181. // g_pITask. Typically discarded.
  182. //
  183. // Synopsis: Exercises ITask interface.
  184. // Relies on foo2.job from TestISchedAgent.
  185. //
  186. // History: 11-4-96 camerone created
  187. //
  188. //-------------------------------------------------------------------
  189. HRESULT TestITask()
  190. {
  191. HRESULT hr = S_OK;
  192. if (g_pITask == 0)
  193. {
  194. return E_FAIL;
  195. }
  196. // wprintf(L"------------------------------------------\n\n");
  197. // wprintf(L"Testing ITask interface\n\n");
  198. //
  199. // Need a second job, local
  200. //
  201. IUnknown *pIU = NULL;
  202. ITask *pITask = NULL;
  203. IPersistFile *pIPF = NULL, *pIPF2 = NULL;
  204. pIU = NULL;
  205. pITask = NULL;
  206. pIPF = NULL;
  207. pIPF2 = NULL;
  208. hr = g_pISchedAgent -> NewWorkItem(L"foo.job", CLSID_CTask, IID_ITask, &pIU);
  209. if (FAILED(hr))
  210. {
  211. wprintf(L"Failed to create a second test job");
  212. return hr;
  213. }
  214. hr = pIU -> QueryInterface(IID_ITask, (void **) &pITask);
  215. if (FAILED(hr))
  216. {
  217. wprintf(L"Failed to get ITask on foo.job");
  218. return hr;
  219. }
  220. hr = pIU -> QueryInterface(IID_IPersistFile, (void **) &pIPF);
  221. if (FAILED(hr))
  222. {
  223. wprintf(L"Failed to get IPersistFile on foo.job");
  224. return hr;
  225. }
  226. pIPF -> Save(NULL, FALSE);
  227. pIU -> Release();
  228. pIU = NULL;
  229. hr = g_pITask -> QueryInterface(IID_IPersistFile, (void **) &pIPF2);
  230. if (FAILED(hr))
  231. {
  232. wprintf(L"Failed to get IPersistFile on foo2.job");
  233. return hr;
  234. }
  235. //
  236. // SetCreator Method
  237. //
  238. hr = pITask -> SetCreator(L"Arnold Palmer");
  239. if (FAILED(hr))
  240. wprintf(L"FAILURE! SetCreator on Arnold Palmer returned hr = %x\n",hr);
  241. if (pIPF -> IsDirty() == S_OK)
  242. {
  243. // wprintf(L"Persisting changes...\n");
  244. pIPF -> Save(NULL, FALSE);
  245. }
  246. hr = pITask -> SetCreator(L"gomer");
  247. // wprintf(L"SetCreator on gomer returned hr = %x\n",hr);
  248. if (pIPF -> IsDirty() == S_OK)
  249. {
  250. // wprintf(L"Persisting changes...\n");
  251. pIPF -> Save(NULL, FALSE);
  252. }
  253. //
  254. // GetCreator Method
  255. //
  256. LPWSTR wszName;
  257. hr = pITask -> GetCreator(&wszName);
  258. if (FAILED(hr))
  259. wprintf(L"\nFAILURE! GetCreator on foo.job returned %s, hr = %x. Should be gomer.\n",wszName,hr);
  260. CoTaskMemFree(wszName);
  261. hr = g_pITask -> GetCreator(&wszName);
  262. if (FAILED(hr))
  263. wprintf(L"FAILURE! GetCreator on foo2.job returned %s, hr = %x. Should be you.\n\n",wszName,hr);
  264. CoTaskMemFree(wszName);
  265. //
  266. // SetComment method
  267. //
  268. hr = g_pITask -> SetComment(L"This is a comment");
  269. if (FAILED(hr))
  270. wprintf(L"FAILURE! SetComment returned hr = %x\n",hr);
  271. if (pIPF2 -> IsDirty() == S_OK)
  272. {
  273. // wprintf(L"Persisting changes...\n");
  274. pIPF2 -> Save(NULL,FALSE);
  275. }
  276. //
  277. // GetComment Method
  278. //
  279. hr = g_pITask -> GetComment(&wszName);
  280. if (FAILED(hr))
  281. wprintf(L"\nFAILURE! GetComment returned %s, hr = %x\n\n",wszName,hr);
  282. CoTaskMemFree(wszName);
  283. //
  284. // SetApplicationName method
  285. //
  286. hr = g_pITask -> SetApplicationName(L"cmd.exe");
  287. if (FAILED(hr))
  288. wprintf(L"FAILURE! SetApplicationName to calc.exe returned hr = %x\n",hr);
  289. if (pIPF2 -> IsDirty() == S_OK)
  290. {
  291. // wprintf(L"Persisting changes...\n");
  292. pIPF2 -> Save(NULL,FALSE);
  293. }
  294. //
  295. // GetApplicationName method
  296. //
  297. hr = g_pITask -> GetApplicationName(&wszName);
  298. if (FAILED(hr))
  299. wprintf(L"\nFAILURE! GetApplicationName returned %s, hr = %x\n\n",wszName,hr);
  300. CoTaskMemFree(wszName);
  301. #ifndef _CHICAGO_
  302. // Real OS's support user accounts and credentials
  303. //
  304. // SetAccountInformation method
  305. //
  306. hr = g_pITask -> SetAccountInformation(L"Administrator",L"");
  307. if (FAILED(hr))
  308. wprintf(L"FAILURE! SetAccountInformation returned hr = %x\n",hr);
  309. if (pIPF2 -> IsDirty() == S_OK)
  310. {
  311. pIPF2 -> Save(NULL,FALSE);
  312. }
  313. //
  314. // GetAccountInformation method
  315. //
  316. hr = g_pITask -> GetAccountInformation(&wszName);
  317. if (FAILED(hr))
  318. wprintf(L"\nFAILURE! GetAccountInformation returned hr = %x\n\n",hr);
  319. CoTaskMemFree(wszName);
  320. #endif
  321. // Back to universal stuff
  322. //
  323. // GetWorkingDirectory/SetworkingDirectory
  324. //
  325. hr = g_pITask -> GetWorkingDirectory(&wszName);
  326. if (FAILED(hr))
  327. wprintf(L"FAILURE! GetWorkingDirectory returned %s, should be NULL, hr = %x\n",wszName,hr);
  328. CoTaskMemFree(wszName);
  329. hr = g_pITask -> SetWorkingDirectory(L"C:\\");
  330. if (FAILED(hr))
  331. wprintf(L"FAILURE! SetWorking directory to C:\\ returned hr = %x\n",hr);
  332. hr = g_pITask -> GetWorkingDirectory(&wszName);
  333. if (FAILED(hr))
  334. wprintf(L"FAILURE! GetWorkingDirectory returned %s, hr = %x\n\n",wszName,hr); CoTaskMemFree(wszName) ;
  335. //
  336. // GetPriority/SetPriority
  337. //
  338. DWORD dwPriority;
  339. hr = g_pITask -> GetPriority(&dwPriority);
  340. if (FAILED(hr))
  341. wprintf(L"FAILURE! GetPriority returned ");
  342. switch(dwPriority)
  343. {
  344. case REALTIME_PRIORITY_CLASS:
  345. // wprintf(L"REALTIME ");
  346. break;
  347. case HIGH_PRIORITY_CLASS:
  348. // wprintf(L"HIGH ");
  349. break;
  350. case NORMAL_PRIORITY_CLASS:
  351. // wprintf(L"NORMAL ");
  352. break;
  353. case IDLE_PRIORITY_CLASS:
  354. // wprintf(L"IDLE ");
  355. break;
  356. default: ;
  357. // wprintf(L"Unknown result %x ",dwPriority);
  358. }
  359. // wprintf(L"hr = %x\n",hr);
  360. hr = g_pITask -> SetPriority(IDLE_PRIORITY_CLASS);
  361. if (FAILED(hr))
  362. wprintf(L"FAILURE! SetPriority to IDLE returned hr = %x\n",hr);
  363. if (pIPF2 -> IsDirty() == S_OK)
  364. {
  365. // wprintf(L"Persisting changes...\n");
  366. pIPF2 -> Save(NULL,FALSE);
  367. }
  368. hr = g_pITask -> GetPriority(&dwPriority);
  369. if (FAILED(hr))
  370. wprintf(L"FAILURE! GetPriority returned hr = %x \n",hr);
  371. switch(dwPriority)
  372. {
  373. case REALTIME_PRIORITY_CLASS:
  374. // wprintf(L"REALTIME ");
  375. break;
  376. case HIGH_PRIORITY_CLASS:
  377. // wprintf(L"HIGH ");
  378. break;
  379. case NORMAL_PRIORITY_CLASS:
  380. // wprintf(L"NORMAL ");
  381. break;
  382. case IDLE_PRIORITY_CLASS:
  383. // wprintf(L"IDLE ");
  384. break;
  385. default:
  386. // wprintf(L"Unknown result %x ",dwPriority);
  387. ; }
  388. // wprintf(L"hr = %x\n",hr);
  389. hr = g_pITask -> SetPriority(NORMAL_PRIORITY_CLASS);
  390. if (FAILED(hr))
  391. wprintf(L"FAILURE! SetPriority to NORMAL returned hr = %x\n\n",hr);
  392. //
  393. // GetFlags/SetFlags methods
  394. //
  395. DWORD dwFlags;
  396. hr = g_pITask -> GetFlags(&dwFlags);
  397. if (FAILED(hr))
  398. wprintf(L"FAILURE! GetFlags gave %x with hr = %x\n",dwFlags,hr);
  399. dwFlags = TASK_FLAG_INTERACTIVE | TASK_FLAG_DELETE_WHEN_DONE
  400. | TASK_FLAG_KILL_IF_GOING_ON_BATTERIES;
  401. hr = g_pITask -> SetFlags(dwFlags);
  402. if (FAILED(hr))
  403. wprintf(L"FAILURE! SetFlags INTERACTIVE, DELETEWHENDONE, KILLIFGOINGONBATTERIES gave hr = %x\n",hr);
  404. if (pIPF2 -> IsDirty() == S_OK)
  405. {
  406. // wprintf(L"Persisting changes...\n");
  407. pIPF2 -> Save(NULL,FALSE);
  408. }
  409. hr = g_pITask -> GetFlags(&dwFlags);
  410. if (FAILED(hr))
  411. {
  412. wprintf(L"FAILURE! GetFlags gave hr = %x and flags: \n",hr);
  413. if (dwFlags & TASK_FLAG_INTERACTIVE)
  414. wprintf(L"INTERACTIVE ");
  415. if (dwFlags & TASK_FLAG_DELETE_WHEN_DONE)
  416. wprintf(L"DELETEWHENDONE ");
  417. if (dwFlags & TASK_FLAG_DISABLED)
  418. wprintf(L"DISABLED ");
  419. if (dwFlags & TASK_FLAG_HIDDEN)
  420. wprintf(L"HIDDEN ");
  421. if (dwFlags & TASK_FLAG_START_ONLY_IF_IDLE)
  422. wprintf(L"STARTONLYIFIDLE ");
  423. if (dwFlags & TASK_FLAG_KILL_ON_IDLE_END)
  424. wprintf(L"KILLONIDLEEND ");
  425. if (dwFlags & TASK_FLAG_DONT_START_IF_ON_BATTERIES)
  426. wprintf(L"DONTSTARTONBATTERIES ");
  427. if (dwFlags & TASK_FLAG_KILL_IF_GOING_ON_BATTERIES)
  428. wprintf(L"KILLIFGOINGONBATTERIES ");
  429. wprintf(L"\n\n");
  430. }
  431. /*
  432. //
  433. // SetIdleWait/GetIdleWait methods
  434. //
  435. WORD wMinutes;
  436. hr = g_pITask -> GetIdleWait(&wMinutes);
  437. wprintf(L"GetIdleWait returned %d Minutes, hr = %x\n",wMinutes,hr);
  438. hr = g_pITask -> SetIdleWait(12);
  439. wprintf(L"SetIdleWait 12 minutes returned hr = %x\n",hr);
  440. if (pIPF2 -> IsDirty() == S_OK)
  441. {
  442. wprintf(L"Persisting changes...\n");
  443. pIPF2 -> Save(NULL,FALSE);
  444. }
  445. hr = g_pITask -> GetIdleWait(&wMinutes);
  446. wprintf(L"GetIdleWait returned %d Minutes, hr = %x\n\n",wMinutes,hr);
  447. */
  448. //
  449. // Get/SetMaxRunTime methods
  450. //
  451. DWORD dwMaxRun;
  452. hr = g_pITask -> GetMaxRunTime(&dwMaxRun);
  453. if (FAILED(hr))
  454. wprintf(L"FAILURE! GetMaxRunTime returned %d Minutes, hr = %x\n",dwMaxRun/1000/60,hr);
  455. dwMaxRun = 12000*60;
  456. hr = g_pITask -> SetMaxRunTime(dwMaxRun);
  457. if (FAILED(hr))
  458. wprintf(L"FAILURE! SetMaxRunTime for 12 minutes returned hr = %x\n",hr);
  459. if (pIPF2 -> IsDirty() == S_OK)
  460. {
  461. // wprintf(L"Persisting changes...\n");
  462. pIPF2 -> Save(NULL,FALSE);
  463. }
  464. hr = g_pITask -> GetMaxRunTime(&dwMaxRun);
  465. if (FAILED(hr))
  466. wprintf(L"FAILURE! GetMaxRunTime returned %d Minutes, hr = %x\n\n",dwMaxRun/1000/60,hr);
  467. //
  468. // Get/Set Parameters methods
  469. //
  470. hr = g_pITask -> GetParameters(&wszName);
  471. if (FAILED(hr))
  472. wprintf(L"FAILURE! GetParameters returned %s, hr = %x\n",wszName,hr);
  473. CoTaskMemFree(wszName);
  474. hr = g_pITask -> SetParameters(L"These are my parameters");
  475. if (FAILED(hr))
  476. wprintf(L"FAILURE! SetParameters returned hr = %x\n",hr);
  477. if (pIPF2 -> IsDirty() == S_OK)
  478. {
  479. // wprintf(L"Persisting changes...\n");
  480. pIPF2 -> Save(NULL,FALSE);
  481. }
  482. hr = g_pITask -> GetParameters(&wszName);
  483. if (FAILED(hr))
  484. wprintf(L"FAILURE! GetParameters returned %s, hr = %x\n\n",wszName,hr);
  485. CoTaskMemFree(wszName);
  486. //
  487. // GetMostRecentRunTime method (will revisit)
  488. //
  489. SYSTEMTIME tTime;
  490. hr = g_pITask -> GetMostRecentRunTime(&tTime);
  491. if (FAILED(hr))
  492. wprintf(L"FAILURE! GetMostRecentRunTime returned hr = %x (should not have run)\n\n",hr);
  493. //
  494. // Run Method
  495. //
  496. hr = g_pITask -> Run();
  497. if (FAILED(hr))
  498. wprintf(L"FAILURE! Foo2 run attempt hr = %x\n",hr);
  499. for (int nLoop = 0; nLoop < 50; nLoop++)
  500. {
  501. Sleep(100);
  502. }
  503. //
  504. // Need to reload off of Disk
  505. //
  506. IUnknown *pIUtemp;
  507. g_pITask -> Release();
  508. g_pITask = NULL;
  509. hr = g_pISchedAgent -> Activate(L"foo2.job", IID_ITask, &pIUtemp);
  510. if (FAILED(hr))
  511. wprintf(L"FAILURE! Activate foo2.job returned hr = %x\n\n",hr);
  512. hr = pIUtemp -> QueryInterface(IID_ITask,(void **) &g_pITask);
  513. pIUtemp -> Release();
  514. pIUtemp = NULL;
  515. // Get Status
  516. HRESULT hrStatus;
  517. hr = g_pITask -> GetStatus(&hrStatus);
  518. if (FAILED(hr))
  519. wprintf(L"FAILURE! Status of job Foo2 is %x, hr = %x\n",hrStatus, hr);
  520. hr = pITask -> Run();
  521. if (SUCCEEDED(hr))
  522. wprintf(L"FAILURE! Foo run attempt (should be error) hr = %x\n\n",hr);
  523. hr = g_pITask -> GetStatus(&hrStatus);
  524. if (FAILED(hr))
  525. wprintf(L"Status of job Foo is %x, hr = %x\n",hrStatus, hr);
  526. //
  527. // Terminate Method
  528. //
  529. // wprintf(L"Sleeping for 10 seconds to let job catch up.\n");
  530. Sleep(5000);
  531. Sleep(5000);
  532. // Reload again
  533. g_pITask -> Release();
  534. g_pITask = NULL;
  535. hr = g_pISchedAgent -> Activate(L"foo2.job", IID_ITask, &pIUtemp);
  536. if (FAILED(hr))
  537. wprintf(L"FAILURE! Activate foo2.job returned hr = %x\n\n",hr);
  538. hr = pIUtemp -> QueryInterface(IID_ITask,(void **) &g_pITask);
  539. pIUtemp -> Release();
  540. pIUtemp = NULL;
  541. hr = g_pITask -> Terminate();
  542. if (FAILED(hr) && (hr != SCHED_E_TASK_NOT_RUNNING))
  543. wprintf(L"FAILURE! Foo2 termination returned = %x\n", hr);
  544. int k = 0;
  545. if (FAILED(hr) && (hr != SCHED_E_TASK_NOT_RUNNING))
  546. {
  547. wprintf(L"Foo2 would not die. Trying again.\n");
  548. do
  549. {
  550. wprintf(L"Sleeping for another second...hr was %x\n",hr);
  551. Sleep(1000); // Let job catch up
  552. k++;
  553. hr = g_pITask -> Terminate();
  554. } while ((k < 10) && (hr != S_OK));
  555. }
  556. if (k == 10)
  557. wprintf(L"Tried 10 times and just gave up.\n");
  558. hr = pITask -> Terminate();
  559. if (SUCCEEDED(hr))
  560. wprintf(L"FAILURE! Foo termination (not running) hr = %x\n\n",hr);
  561. //
  562. // GetMostRecentRunTime revisited
  563. //
  564. hr = g_pITask -> GetMostRecentRunTime(&tTime);
  565. if (FAILED(hr))
  566. wprintf(L"FAILURE! GetMostRecentRunTime returned hr = %x\n",hr);
  567. /* if (hr != SCHED_S_TASK_HAS_NOT_RUN)
  568. {
  569. // Dump Time Structure
  570. wprintf(L"%d/%d/%d %d:%d:%d.%d day %d\n",
  571. tTime.wDay, tTime.wMonth, tTime.wYear,
  572. tTime.wHour, tTime.wMinute, tTime.wSecond,
  573. tTime.wMilliseconds, tTime.wDayOfWeek);
  574. }
  575. */
  576. //
  577. // GetExitCode method
  578. // The return code is the task's last start error, not an error in
  579. // retrieving the exit code
  580. //
  581. DWORD dwExitCode;
  582. hr = pITask -> GetExitCode(&dwExitCode);
  583. hr = g_pITask -> GetExitCode(&dwExitCode);
  584. //
  585. // Clean up...
  586. //
  587. // wprintf(L"Now cleaning up...");
  588. pITask -> Release();
  589. // wprintf(L"pITask ");
  590. pIPF -> Release();
  591. // wprintf(L"pIPF ");
  592. pIPF2 -> Release();
  593. // wprintf(L"pIPF2 ");
  594. hr = g_pISchedAgent -> Delete(L"foo.job");
  595. if (FAILED(hr))
  596. wprintf(L"\nFAILURE! Cleaning up foo.job delete returned %x\n\n",hr);
  597. return S_OK;
  598. }
  599. //+-----------------------------------------------------------------
  600. //
  601. // function: TestITaskTrigger();
  602. //
  603. // synopsis: Tests ITaskTrigger, plus associated functionality
  604. // within ITask, as related to ITaskTrigger
  605. //
  606. // Arguments: none
  607. //
  608. // returns: HRESULT, usually S_OK, almost always discarded
  609. //
  610. // history: 11-5-96 camerone created
  611. //
  612. //-------------------------------------------------------------------
  613. HRESULT TestITaskTrigger()
  614. {
  615. HRESULT hr;
  616. //
  617. // Where we are
  618. //
  619. // wprintf(L"----------------------------------------------------\n");
  620. // wprintf(L"Testing ITaskTrigger and related ITask parts\n\n");
  621. //
  622. // Need to reload off of Disk
  623. //
  624. IUnknown *pIUtemp;
  625. g_pITask -> Release();
  626. g_pITask = NULL;
  627. hr = g_pISchedAgent -> Activate(L"foo2.job", IID_ITask, &pIUtemp);
  628. if (FAILED(hr))
  629. wprintf(L"FAILURE! Activate foo2.job returned hr = %x\n\n",hr);
  630. hr = pIUtemp -> QueryInterface(IID_ITask,(void **) &g_pITask);
  631. pIUtemp -> Release();
  632. pIUtemp = NULL;
  633. //
  634. // Allow persistant changes
  635. //
  636. IPersistFile *pIPF = NULL;
  637. hr = g_pITask -> QueryInterface(IID_IPersistFile, (void **) &pIPF);
  638. if (FAILED(hr))
  639. {
  640. wprintf(L"QueryInterface for IPersistFile failed %x\n",hr);
  641. return E_FAIL;
  642. }
  643. //
  644. // To test one, we need one.
  645. // CreateTrigger method
  646. //
  647. ITaskTrigger *pITTrig;
  648. WORD iTrig, iTrig2, iTrig3;
  649. hr = g_pITask -> CreateTrigger(&iTrig, &g_pITaskTrigger);
  650. if (FAILED(hr))
  651. wprintf(L"FAILURE! CreateTrigger returned trigger #%d, hr = %x\n",iTrig,hr);
  652. if (FAILED(hr))
  653. {
  654. wprintf(L"Failure to initialize a trigger\n");
  655. return E_FAIL;
  656. }
  657. hr = g_pITask -> CreateTrigger(&iTrig2, &pITTrig);
  658. if (FAILED(hr))
  659. wprintf(L"FAILURE! CreateTrigger returned trigger #%d, hr = %x\n",iTrig2,hr);
  660. pITTrig -> Release();
  661. pITTrig = NULL;
  662. hr = g_pITask -> CreateTrigger(&iTrig3, &pITTrig);
  663. if (FAILED(hr))
  664. wprintf(L"FAILURE! CreateTrigger returned trigger #%d, hr = %x\n",iTrig3,hr);
  665. if (pIPF -> IsDirty() == S_OK)
  666. {
  667. // wprintf(L"Persisting changes...\n");
  668. pIPF -> Save(NULL,FALSE);
  669. }
  670. //
  671. // Delete the last trigger
  672. //
  673. pITTrig -> Release();
  674. pITTrig = NULL;
  675. hr = g_pITask -> DeleteTrigger(iTrig3);
  676. if (FAILED(hr))
  677. wprintf(L"\nFAILURE! DeleteTrigger on %d returned hr = %x\n",iTrig3,hr);
  678. if (pIPF -> IsDirty() == S_OK)
  679. {
  680. // wprintf(L"Persisting changes...\n");
  681. pIPF -> Save(NULL,FALSE);
  682. }
  683. //
  684. // GetTriggerString
  685. // we will revisit this one later.
  686. //
  687. LPWSTR pwszTriggerString;
  688. hr = g_pITask -> GetTriggerString(iTrig, &pwszTriggerString);
  689. if (FAILED(hr))
  690. wprintf(L"\nFAILURE! GetTrigger string - %s, hr = %x\n\n",pwszTriggerString,hr);
  691. CoTaskMemFree(pwszTriggerString);
  692. //
  693. // GetMostRecentRunTimes again.
  694. //
  695. SYSTEMTIME tTime;
  696. hr = g_pITask -> GetMostRecentRunTime(&tTime);
  697. if (FAILED(hr))
  698. wprintf(L"FAILURE! GetMostRecentRunTime returned hr = %x\n",hr);
  699. /* if (hr != SCHED_S_TASK_HAS_NOT_RUN)
  700. {
  701. // Dump Time Structure
  702. wprintf(L"%d/%d/%d %d:%d:%d.%d day %d\n",
  703. tTime.wDay, tTime.wMonth, tTime.wYear,
  704. tTime.wHour, tTime.wMinute, tTime.wSecond,
  705. tTime.wMilliseconds, tTime.wDayOfWeek);
  706. }
  707. */
  708. //
  709. // GetTriggerCount Method
  710. //
  711. WORD wTrigCount;
  712. hr = g_pITask -> GetTriggerCount(&wTrigCount);
  713. if (FAILED(hr))
  714. wprintf(L"FAILURE! GetTriggerCount shows %d triggers, hr = %x\n\n",wTrigCount,hr);
  715. //
  716. // TaskTrigger GetTriggerString
  717. //
  718. hr = g_pITaskTrigger -> GetTriggerString(&pwszTriggerString);
  719. if (FAILED(hr))
  720. wprintf(L"FAILURE! GetTriggerString (TaskTrig) - %s, hr = %x\n\n",pwszTriggerString,hr);
  721. CoTaskMemFree(pwszTriggerString);
  722. //
  723. // GetTrigger
  724. //
  725. TASK_TRIGGER Trigger;
  726. Trigger.cbTriggerSize = sizeof(TASK_TRIGGER);
  727. hr = g_pITaskTrigger -> GetTrigger(&Trigger);
  728. if (FAILED(hr))
  729. {
  730. wprintf(L"FAILURE! GetTrigger returned hr = %x\nDumping structure:\n",hr);
  731. wprintf(L"From %d/%d/%d to %d/%d/%d\n",Trigger.wBeginDay,
  732. Trigger.wBeginMonth,Trigger.wBeginYear,
  733. Trigger.wEndDay,Trigger.wEndMonth,Trigger.wEndYear);
  734. wprintf(L"Starting at %d:%d, running for %d or at Interval %d\n",
  735. Trigger.wStartHour,Trigger.wStartMinute,
  736. Trigger.MinutesDuration,Trigger.MinutesInterval);
  737. wprintf(L"Flag mask %x, Trigger Type = %d\n\n",Trigger.rgFlags,
  738. Trigger.TriggerType);
  739. }
  740. //
  741. // SetTrigger
  742. //
  743. Trigger.wBeginDay = 7;
  744. Trigger.wBeginMonth = 10;
  745. Trigger.wBeginYear = 1996;
  746. Trigger.wEndDay = 21;
  747. Trigger.wEndMonth = 1;
  748. Trigger.wEndYear = 1997;
  749. Trigger.wStartHour = 15;
  750. Trigger.wStartMinute = 42;
  751. Trigger.MinutesDuration = 2;
  752. Trigger.TriggerType = TASK_TIME_TRIGGER_WEEKLY;
  753. Trigger.rgFlags = TASK_TRIGGER_FLAG_HAS_END_DATE;
  754. Trigger.Type.Weekly.WeeksInterval = 2;
  755. Trigger.Type.Weekly.rgfDaysOfTheWeek = TASK_SUNDAY | TASK_FRIDAY;
  756. hr = g_pITaskTrigger -> SetTrigger(&Trigger);
  757. if (FAILED(hr))
  758. {
  759. wprintf(L"FAILURE! Set trigger returned hr = %x\nSet To: \n",hr);
  760. wprintf(L"From %d/%d/%d to %d/%d/%d\n",Trigger.wBeginDay,
  761. Trigger.wBeginMonth,Trigger.wBeginYear,
  762. Trigger.wEndDay,Trigger.wEndMonth,Trigger.wEndYear);
  763. wprintf(L"Starting at %d:%d, running for %d or at Interval %d\n",
  764. Trigger.wStartHour,Trigger.wStartMinute,
  765. Trigger.MinutesDuration,Trigger.MinutesInterval);
  766. wprintf(L"Flag mask %x, Trigger Type = %d\n\n",Trigger.rgFlags,
  767. Trigger.TriggerType);
  768. }
  769. if (pIPF -> IsDirty() == S_OK)
  770. {
  771. // wprintf(L"Persisting changes...\n");
  772. pIPF -> Save(NULL,FALSE);
  773. }
  774. // Get Again
  775. hr = g_pITaskTrigger -> GetTrigger(&Trigger);
  776. if (FAILED(hr))
  777. {
  778. wprintf(L"FAILURE! GetTrigger returned hr = %x\nDumping structure:\n",hr);
  779. wprintf(L"From %d/%d/%d to %d/%d/%d\n",Trigger.wBeginDay,
  780. Trigger.wBeginMonth,Trigger.wBeginYear,
  781. Trigger.wEndDay,Trigger.wEndMonth,Trigger.wEndYear);
  782. wprintf(L"Starting at %d:%d, running for %d or at Interval %d\n",
  783. Trigger.wStartHour,Trigger.wStartMinute,
  784. Trigger.MinutesDuration,Trigger.MinutesInterval);
  785. wprintf(L"Flag mask %x, Trigger Type = %d\n\n",Trigger.rgFlags,
  786. Trigger.TriggerType);
  787. }
  788. //
  789. // Get Trigger String last time
  790. //
  791. hr = g_pITaskTrigger -> GetTriggerString(&pwszTriggerString);
  792. if (FAILED(hr))
  793. wprintf(L"FAILURE! GetTriggerString (TaskTrig) - %s, hr = %x\n\n",pwszTriggerString,hr);
  794. CoTaskMemFree(pwszTriggerString);
  795. //
  796. // Some Cleanup
  797. //
  798. g_pITaskTrigger -> Release();
  799. g_pITaskTrigger = NULL;
  800. //
  801. // Get the other trigger
  802. //
  803. hr = g_pITask -> GetTrigger(iTrig2,&g_pITaskTrigger);
  804. if (FAILED(hr))
  805. wprintf(L"GetTrigger on #%d returned hr = %x\n\n",iTrig2,hr);
  806. //
  807. // Dump this one
  808. //
  809. hr = g_pITaskTrigger -> GetTrigger(&Trigger);
  810. if (FAILED(hr))
  811. {
  812. wprintf(L"FAILURE! GetTrigger returned hr = %x\nDumping structure:\n",hr);
  813. wprintf(L"From %d/%d/%d to %d/%d/%d\n",Trigger.wBeginDay,
  814. Trigger.wBeginMonth,Trigger.wBeginYear,
  815. Trigger.wEndDay,Trigger.wEndMonth,Trigger.wEndYear);
  816. wprintf(L"Starting at %d:%d, running for %d or at Interval %d\n",
  817. Trigger.wStartHour,Trigger.wStartMinute,
  818. Trigger.MinutesDuration,Trigger.MinutesInterval);
  819. wprintf(L"Flag mask %x, Trigger Type = %d\n\n",Trigger.rgFlags,
  820. Trigger.TriggerType);
  821. }
  822. //
  823. // Get Trigger String last time
  824. //
  825. hr = g_pITaskTrigger -> GetTriggerString(&pwszTriggerString);
  826. if (FAILED(hr))
  827. wprintf(L"FAILURE! GetTriggerString (TaskTrig) - %s, hr = %x\n\n",pwszTriggerString,hr);
  828. CoTaskMemFree(pwszTriggerString);
  829. // More cleanup
  830. g_pITaskTrigger -> Release();
  831. g_pITaskTrigger = NULL;
  832. pIPF -> Release();
  833. pIPF = NULL;
  834. return S_OK;
  835. }
  836. //+-------------------------------------------------------------------
  837. //
  838. // function: TestIEnum
  839. //
  840. // Synopsis: Tests IEnumTasks methods
  841. //
  842. // Arguments: None
  843. //
  844. // returns: S_OK, possibly others. Basically, discarded.
  845. //
  846. // history: 11-5-96 camerone created
  847. //
  848. //---------------------------------------------------------------------
  849. HRESULT TestIEnum()
  850. {
  851. HRESULT hr;
  852. //
  853. // Where we are
  854. //
  855. // wprintf(L"------------------------------------------------------\n");
  856. // wprintf(L"Testing IEnumTasks\n");
  857. //
  858. // Get the pointer to the interface
  859. //
  860. hr = g_pISchedAgent -> Enum(&g_pIEnumTasks);
  861. if (FAILED(hr))
  862. wprintf(L"FAILURE! ISA Enum returned hr = %x\n\n",hr);
  863. //
  864. // Clean up the folder
  865. //
  866. LPWSTR *rgpwszNames;
  867. ULONG celt, celtGot;
  868. celt = 10;
  869. hr = g_pIEnumTasks -> Next(celt, &rgpwszNames, &celtGot);
  870. if (FAILED(hr))
  871. wprintf(L"FAILURE - Next returned %d results, hr = %x\n",celtGot, hr);
  872. for (ULONG i = 0; i < celtGot; i++)
  873. {
  874. // wprintf(L"%s\n",rgpwszNames[i]);
  875. g_pISchedAgent -> Delete(rgpwszNames[i]);
  876. CoTaskMemFree(rgpwszNames[i]);
  877. }
  878. CoTaskMemFree(rgpwszNames);
  879. return S_OK;
  880. }
  881. //+----------------------------------------------------------------------
  882. //
  883. // function: TestGRT()
  884. //
  885. // synopsis: Tests GetRunTimes
  886. //
  887. // Arguments: None
  888. //
  889. // Returns: S_OK. Throw it away!
  890. //
  891. // history: 11-15-96 camerone created
  892. //
  893. // Notes: Requires the global pointers to be init'd. Should
  894. // be the last thing called, too, since we need that empty
  895. // tasks folder.
  896. //
  897. //------------------------------------------------------------------------
  898. HRESULT TestGRT()
  899. {
  900. HRESULT hr = S_OK;
  901. IUnknown *pIU = NULL;
  902. ITask *pITask = NULL;
  903. IPersistFile *pIPF = NULL;
  904. ITaskTrigger *pITT1 = NULL, *pITT2 = NULL, *pITT3 = NULL;
  905. WORD wTrig1, wTrig2, wTrig3;
  906. // wprintf(L"-----------------------------------------------\n");
  907. // wprintf(L"TestGRT!\n\n");
  908. hr = g_pISchedAgent -> NewWorkItem(L"bar.job", CLSID_CTask, IID_ITask, &pIU);
  909. if (FAILED(hr))
  910. {
  911. wprintf(L"Failed to allocate New Task bar.job\n");
  912. return E_FAIL;
  913. }
  914. // wprintf(L"New Task (good - bar.job) returned hr = %x\n\n",hr);
  915. hr = pIU -> QueryInterface(IID_ITask, (void **) &pITask);
  916. if (FAILED(hr))
  917. {
  918. wprintf(L"QI for ITask failed %x\n",hr);
  919. return E_FAIL;
  920. }
  921. pIU -> Release();
  922. pIU = NULL;
  923. hr = pITask -> QueryInterface(IID_IPersistFile, (void **) &pIPF);
  924. if (FAILED(hr))
  925. {
  926. wprintf(L"QueryInterface for IPersistFile failed %x\n",hr);
  927. return E_FAIL;
  928. }
  929. //
  930. // Set up legitimate task parameters.
  931. //
  932. // wprintf(L"Setting Application name to calc.exe\n");
  933. hr = pITask -> SetApplicationName(L"calc.exe");
  934. if (FAILED(hr))
  935. {
  936. wprintf(L"SetApplicationName failed %x\n",hr);
  937. return E_FAIL;
  938. }
  939. // wprintf(L"Setting Account Information security\n");
  940. #ifndef _CHICAGO_
  941. hr = pITask -> SetAccountInformation(L"Administrator",L"");
  942. if (FAILED(hr))
  943. {
  944. wprintf(L"Failed to set account info %x\n",hr);
  945. return E_FAIL;
  946. }
  947. #endif
  948. DWORD dwFlags = 0;
  949. hr = pITask -> SetFlags(dwFlags);
  950. //
  951. // Great. We now have empty task "foo.job"
  952. // The plan is as follows:
  953. // 1. Create 3 triggers of the following types:
  954. // a. One at Logon
  955. // b. Two set to run Once, in the future
  956. // 2. Call GetRunTimes the following ways:
  957. // a. Asking for 1 run time, in the interval w/trigs
  958. // b. Asking for 2 run times, in the interval w/trigs
  959. // 3. Verify S_OK returned each time
  960. // 4. Call asking for 2 runtimes, when only one possible.
  961. // 5. Verify S_FALSE and pCount == 1 returned
  962. // 6. Call for one trigger, outside of the interval w/trigs
  963. // 7. Verify that SCHED_S_EVENT_TRIGGER returned
  964. // 8. Delete the "LOGON" Trigger
  965. // 9. Call for one trigger, outside possible run interval.
  966. // 10. Verify S_FALSE, pCount == 0 returned.
  967. // 11. Disable task
  968. // 12. Call for one trigger, in the good interval.
  969. // 13. Verify return of SCHED_S_TASK_DISABLED
  970. // 14. Enable Task
  971. // 15. Delete remaining two triggers
  972. // 16. Call for a single trigger, any time.
  973. // 17. Verify return of SCHED_S_NO_VALID_TRIGGERS
  974. // 18. Delete task.
  975. // Create 3 triggers
  976. hr = pITask -> CreateTrigger(&wTrig1, &pITT1);
  977. if (hr != S_OK)
  978. {
  979. wprintf(L"Failed to create first trigger\n");
  980. return E_FAIL;
  981. }
  982. hr = pITask -> CreateTrigger(&wTrig2, &pITT2);
  983. if (hr != S_OK)
  984. {
  985. wprintf(L"Failed to create second trigger\n");
  986. return E_FAIL;
  987. }
  988. hr = pITask -> CreateTrigger(&wTrig3, &pITT3);
  989. if (hr != S_OK)
  990. {
  991. wprintf(L"Failed to create third trigger\n");
  992. return E_FAIL;
  993. }
  994. if (pIPF -> IsDirty() == S_OK)
  995. {
  996. wprintf(L"Persisting changes...\n");
  997. pIPF -> Save(NULL,FALSE);
  998. }
  999. // Setup the three triggers
  1000. TASK_TRIGGER TT1, TT2, TT3;
  1001. TT1.cbTriggerSize = sizeof(TASK_TRIGGER);
  1002. TT2.cbTriggerSize = sizeof(TASK_TRIGGER);
  1003. TT3.cbTriggerSize = sizeof(TASK_TRIGGER);
  1004. TT1.Reserved1 = 0;
  1005. TT1.Reserved2 = 0;
  1006. TT1.wRandomMinutesInterval = 0;
  1007. TT1.wBeginYear = 1997;
  1008. TT1.wBeginMonth = 1;
  1009. TT1.wBeginDay = 1;
  1010. TT1.wStartHour = 10;
  1011. TT1.wStartMinute = 0;
  1012. TT1.MinutesDuration = 10;
  1013. TT1.rgFlags = TASK_TRIGGER_FLAG_KILL_AT_DURATION_END;
  1014. TT1.TriggerType = TASK_TIME_TRIGGER_ONCE;
  1015. TT1.wEndYear = 0;
  1016. TT1.wEndMonth = 0;
  1017. TT1.wEndDay = 0;
  1018. TT1.MinutesInterval = 0;
  1019. TT1.Type.Daily.DaysInterval = 1;
  1020. TT2.Reserved1 = 0;
  1021. TT2.Reserved2 = 0;
  1022. TT2.wRandomMinutesInterval = 0;
  1023. TT2.wBeginYear = 1997;
  1024. TT2.wBeginMonth = 1;
  1025. TT2.wBeginDay = 1;
  1026. TT2.wStartHour = 11;
  1027. TT2.wStartMinute = 0;
  1028. TT2.MinutesDuration = 10;
  1029. TT2.rgFlags = TASK_TRIGGER_FLAG_KILL_AT_DURATION_END;
  1030. TT2.TriggerType = TASK_TIME_TRIGGER_ONCE;
  1031. TT2.wEndYear = 0;
  1032. TT2.wEndMonth = 0;
  1033. TT2.wEndDay = 0;
  1034. TT2.MinutesInterval = 0;
  1035. TT2.Type.Daily.DaysInterval = 1;
  1036. TT3.Reserved1 = 0;
  1037. TT3.Reserved2 = 0;
  1038. TT3.wRandomMinutesInterval = 0;
  1039. TT3.wBeginYear = 1997;
  1040. TT3.wBeginMonth = 1;
  1041. TT3.wBeginDay = 1;
  1042. TT3.wStartHour = 11;
  1043. TT3.wStartMinute = 0;
  1044. TT3.MinutesDuration = 10;
  1045. TT3.rgFlags = TASK_TRIGGER_FLAG_KILL_AT_DURATION_END;
  1046. TT3.TriggerType = TASK_EVENT_TRIGGER_AT_LOGON;
  1047. TT3.wEndYear = 0;
  1048. TT3.wEndMonth = 0;
  1049. TT3.wEndDay = 0;
  1050. TT3.MinutesInterval = 0;
  1051. TT3.Type.Daily.DaysInterval = 1;
  1052. hr = pITT1 -> SetTrigger(&TT1);
  1053. if (hr != S_OK)
  1054. {
  1055. wprintf(L"Failed to set trigger 1 hr = %x\n",hr);
  1056. return E_FAIL;
  1057. }
  1058. hr = pITT2 -> SetTrigger(&TT2);
  1059. if (hr != S_OK)
  1060. {
  1061. wprintf(L"Failed to set trigger 2 hr = %x\n",hr);
  1062. return E_FAIL;
  1063. }
  1064. hr = pITT3 -> SetTrigger(&TT3);
  1065. if (hr != S_OK)
  1066. {
  1067. wprintf(L"Failed to set trigger 3 hr = %x\n",hr);
  1068. return E_FAIL;
  1069. }
  1070. // Persist.
  1071. if (pIPF -> IsDirty() == S_OK)
  1072. {
  1073. wprintf(L"Persisting changes...\n");
  1074. pIPF -> Save(NULL,FALSE);
  1075. }
  1076. // Call GetRunTimes, asking for one and two trigs in interval
  1077. SYSTEMTIME tStart, tEnd;
  1078. WORD wTrigCount;
  1079. SYSTEMTIME *rgstTaskTimes;
  1080. tStart.wDay = 1;
  1081. tStart.wMonth = 1;
  1082. tStart.wYear = 1997;
  1083. tStart.wHour = 9;
  1084. tStart.wMinute = 0;
  1085. tStart.wDayOfWeek = 0;
  1086. tStart.wSecond = 0;
  1087. tStart.wMilliseconds = 0;
  1088. tEnd.wDay = 1;
  1089. tEnd.wMonth = 1;
  1090. tEnd.wYear = 1997;
  1091. tEnd.wHour = 12;
  1092. tEnd.wMinute = 0;
  1093. tEnd.wDayOfWeek = 0;
  1094. tEnd.wSecond = 0;
  1095. tEnd.wMilliseconds = 0;
  1096. wTrigCount = 1;
  1097. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1098. wprintf(L"GetRunTimes(1) returned %x, should be S_OK. #Trig=%d\n",hr,wTrigCount);
  1099. CoTaskMemFree(rgstTaskTimes);
  1100. wTrigCount = 2;
  1101. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1102. wprintf(L"GetRunTimes(2) returned %x, should be S_OK. #Trig=%d\n",hr,wTrigCount);
  1103. CoTaskMemFree(rgstTaskTimes);
  1104. tStart.wHour = 10;
  1105. tStart.wMinute = 30;
  1106. // Ask for two, where there is only one.
  1107. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1108. wprintf(L"GetRunTimes ask two, get one returned %x, should be S_FALSE. #Trig=%d\n",hr,wTrigCount);
  1109. CoTaskMemFree(rgstTaskTimes);
  1110. // Call for two, outside of trigger area, get EVENT
  1111. wTrigCount = 2;
  1112. tStart.wHour = 11;
  1113. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1114. wprintf(L"GetRunTimes ask outside returned %x, should be SCHED_S_EVENT_TRIGGER #Trig=%d\n",hr,wTrigCount);
  1115. CoTaskMemFree(rgstTaskTimes);
  1116. // Delete trigger 3, remake the call.
  1117. pITT3 -> Release();
  1118. pITT3 = NULL;
  1119. wprintf(L"Now deleting the event trigger\n");
  1120. hr = pITask -> DeleteTrigger(wTrig3);
  1121. if (hr != S_OK)
  1122. {
  1123. wprintf(L"Deleting trigger 3 failed\n");
  1124. return E_FAIL;
  1125. }
  1126. WORD wCount;
  1127. hr = pITask -> GetTriggerCount(&wCount);
  1128. if (hr != S_OK)
  1129. {
  1130. wprintf(L"GetTriggerCount failed\n");
  1131. return(E_FAIL);
  1132. }
  1133. wprintf(L"There are now %d triggers\n",wCount);
  1134. wTrigCount = 2;
  1135. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1136. wprintf(L"GetRunTimes ask outside returned %x, should be S_FALSE #Trig=%d\n",hr,wTrigCount);
  1137. CoTaskMemFree(rgstTaskTimes);
  1138. // Disable task
  1139. dwFlags = TASK_FLAG_DISABLED;
  1140. hr = pITask -> SetFlags(dwFlags);
  1141. if (hr != S_OK)
  1142. {
  1143. wprintf(L"Couldn't set disabled flag\n");
  1144. return E_FAIL;
  1145. }
  1146. tStart.wHour = 9;
  1147. wTrigCount = 2;
  1148. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1149. wprintf(L"GetRunTimes disabled (2) returned %x, should be SCHED_S_TASK_DISABLED #Trig=%d\n",hr,wTrigCount);
  1150. CoTaskMemFree(rgstTaskTimes);
  1151. // Enable, remove other trigs
  1152. dwFlags ^= TASK_FLAG_DISABLED;
  1153. hr = pITask -> SetFlags(dwFlags);
  1154. if (hr != S_OK)
  1155. {
  1156. wprintf(L"Couldn't set disabled flag\n");
  1157. return E_FAIL;
  1158. }
  1159. pITT1 -> Release();
  1160. pITT1 = NULL;
  1161. pITT2 -> Release();
  1162. pITT2 = NULL;
  1163. hr = pITask -> DeleteTrigger(wTrig2);
  1164. if (hr != S_OK)
  1165. {
  1166. wprintf(L"Deleting trigger 2 failed\n");
  1167. return E_FAIL;
  1168. }
  1169. hr = pITask -> DeleteTrigger(wTrig1);
  1170. if (hr != S_OK)
  1171. {
  1172. wprintf(L"Deleting trigger 1 failed\n");
  1173. return E_FAIL;
  1174. }
  1175. // Persist.
  1176. if (pIPF -> IsDirty() == S_OK)
  1177. {
  1178. wprintf(L"Persisting changes...\n");
  1179. pIPF -> Save(NULL,FALSE);
  1180. }
  1181. // Now call for SCHED_S_NO_VALID_TRIGGERS
  1182. wTrigCount = 2;
  1183. hr = pITask -> GetRunTimes(&tStart, &tEnd, &wTrigCount, &rgstTaskTimes);
  1184. wprintf(L"GetRunTimes (2) no trigs returned %x, should be SCHED_S_TASK_NO_VALID_TRIGGERS #Trig=%d\n",hr,wTrigCount);
  1185. CoTaskMemFree(rgstTaskTimes);
  1186. // Cleanup
  1187. pITask -> Release();
  1188. pITask = NULL;
  1189. pIPF -> Release();
  1190. pIPF = NULL;
  1191. // Delete job
  1192. g_pISchedAgent -> Delete(L"bar.job");
  1193. return S_OK;
  1194. }