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.

386 lines
11 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Job Schedule Application Unit Test
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  7. //
  8. // File: schtest.cxx
  9. //
  10. // Contents: schedule object test harness
  11. //
  12. // History: 14-Nov-95 EricB created
  13. //
  14. //-----------------------------------------------------------------------------
  15. #include "headers.hxx"
  16. #pragma hdrstop
  17. #include <mstask.h>
  18. #include <sch_cls.hxx>
  19. DECLARE_INFOLEVEL(Sched);
  20. //const WORD JOB_MILLISECONDS_PER_MINUTE = 60 * 1000;
  21. //+----------------------------------------------------------------------------
  22. // Macro: TEST_HR
  23. // Purpose: tests the HRESULT for error, takes "action" if found
  24. //-----------------------------------------------------------------------------
  25. #define TEST_HR(hr, str, action) \
  26. if (FAILED(hr)) \
  27. { \
  28. fprintf(stderr, #str " failed with error %lx\n", hr); \
  29. action; \
  30. }
  31. HINSTANCE g_hInstance;
  32. // flags
  33. #define ST_PRINT_RUNS 0x1
  34. #define ST_CREATE 0x2
  35. #define ST_NEWATJOB 0x4
  36. #define ST_TARGET 0x8
  37. // prototypes
  38. void Usage(int ExitCode);
  39. HRESULT PrintRunTimes(ISchedulingAgent * pSched);
  40. HRESULT CreateJob(ISchedulingAgent * pSched, TCHAR * ptszFileName);
  41. HRESULT CreateAtJob(ISchedulingAgent * pSched);
  42. //+----------------------------------------------------------------------------
  43. //
  44. // Function: main
  45. //
  46. // Synopsis: entry point
  47. //
  48. //-----------------------------------------------------------------------------
  49. int _cdecl
  50. main(int argc, char ** argv)
  51. {
  52. if (argc < 2)
  53. {
  54. Usage(EXIT_SUCCESS);
  55. }
  56. DWORD dwFlags = 0;
  57. char szMsg[80] = "\n** Schedule object test";
  58. LPTSTR ptszCmdLine = GetCommandLine();
  59. TCHAR * ptszFileName, * ptszTarget;
  60. // skip the program name
  61. TCHAR * ptszToken = _tcspbrk(ptszCmdLine, TEXT(" \t"));
  62. // delimit the first token
  63. ptszToken = _tcstok(ptszToken, TEXT(" \t"));
  64. // parse command line
  65. do {
  66. switch (*ptszToken)
  67. {
  68. case TEXT('/'):
  69. case TEXT('-'):
  70. ptszToken = _tcsinc(ptszToken);
  71. switch(*ptszToken)
  72. {
  73. #if !defined(_CHICAGO_) // no AT command support on Chicago
  74. case TEXT('a'):
  75. case TEXT('A'):
  76. dwFlags |= ST_NEWATJOB;
  77. strcat(szMsg, "; new AT job");
  78. break;
  79. #endif // #if !defined(_CHICAGO_)
  80. case TEXT('r'):
  81. case TEXT('R'):
  82. dwFlags |= ST_PRINT_RUNS;
  83. strcat(szMsg, "; print runs");
  84. break;
  85. case TEXT('c'):
  86. case TEXT('C'):
  87. dwFlags |= ST_CREATE;
  88. ptszToken = _tcsinc(ptszToken);
  89. ptszFileName = _tcstok(NULL, TEXT(" \t"));
  90. if (ptszFileName == NULL || *ptszFileName == TEXT('/') ||
  91. *ptszFileName == TEXT('-'))
  92. {
  93. Usage(EXIT_FAILURE);
  94. }
  95. sprintf(szMsg + strlen(szMsg), "; create %S", ptszFileName);
  96. break;
  97. case TEXT('t'):
  98. case TEXT('T'):
  99. dwFlags |= ST_TARGET;
  100. ptszToken = _tcsinc(ptszToken);
  101. ptszTarget = _tcstok(NULL, TEXT(" \t"));
  102. if (ptszTarget == NULL || *ptszTarget == TEXT('/') ||
  103. *ptszTarget == TEXT('-') ||
  104. lstrlen(ptszTarget) > MAX_COMPUTERNAME_LENGTH)
  105. {
  106. Usage(EXIT_FAILURE);
  107. }
  108. sprintf(szMsg + strlen(szMsg), "; target %S", ptszTarget);
  109. break;
  110. case TEXT('h'):
  111. case TEXT('H'):
  112. case TEXT('?'):
  113. Usage(EXIT_SUCCESS);
  114. default:
  115. Usage(EXIT_FAILURE);
  116. }
  117. break;
  118. default:
  119. Usage(EXIT_FAILURE);
  120. }
  121. ptszToken = _tcstok(NULL, TEXT(" \t"));
  122. } while (ptszToken);
  123. strcat(szMsg, "\n");
  124. printf(szMsg);
  125. HRESULT hr;
  126. hr = OleInitialize(NULL);
  127. TEST_HR(hr, "OleInitialize", return -1);
  128. ISchedulingAgent * pSched;
  129. hr = CoCreateInstance(CLSID_CSchedulingAgent, NULL, CLSCTX_INPROC_SERVER,
  130. IID_ISchedulingAgent, (void **)&pSched);
  131. TEST_HR(hr, "CoCreateInstance(CLSID_CSchedulingAgent)", goto Err0);
  132. if (dwFlags & ST_TARGET)
  133. {
  134. WCHAR wszTarget[MAX_COMPUTERNAME_LENGTH + 4] = L"\\\\";
  135. #if defined(UNICODE)
  136. lstrcat(wszTarget, ptszTarget);
  137. #else
  138. MultiByteToWideChar(CP_ACP, 0, ptszTarget, -1, wszTarget + 2,
  139. MAX_COMPUTERNAME_LENGTH + 2);
  140. #endif
  141. hr = pSched->SetTargetComputer(wszTarget);
  142. if (FAILED(hr))
  143. {
  144. if (hr == SCHED_E_SERVICE_NOT_INSTALLED)
  145. {
  146. printf("\nScheduler not installed on \\\\%S\n", ptszTarget);
  147. pSched->Release();
  148. OleUninitialize();
  149. printf("\n** Test could not be run **\n");
  150. return(EXIT_SUCCESS);
  151. }
  152. fprintf(stderr,
  153. "pSched->SetTargetComputer failed with error %lx\n",
  154. hr);
  155. goto Err1;
  156. }
  157. }
  158. if (dwFlags & ST_PRINT_RUNS)
  159. {
  160. hr = PrintRunTimes(pSched);
  161. if (FAILED(hr))
  162. {
  163. goto Err1;
  164. }
  165. }
  166. if (dwFlags & ST_CREATE)
  167. {
  168. hr = CreateJob(pSched, ptszFileName);
  169. if (FAILED(hr))
  170. {
  171. goto Err1;
  172. }
  173. }
  174. if (dwFlags & ST_NEWATJOB)
  175. {
  176. hr = CreateAtJob(pSched);
  177. if (FAILED(hr))
  178. {
  179. goto Err1;
  180. }
  181. }
  182. pSched->Release();
  183. OleUninitialize();
  184. printf("\n** Test successfully completed! **\n");
  185. return(EXIT_SUCCESS);
  186. Err1:
  187. pSched->Release();
  188. Err0:
  189. OleUninitialize();
  190. printf("** Test failed.\n");
  191. return(EXIT_FAILURE);
  192. }
  193. //+----------------------------------------------------------------------------
  194. //
  195. // Function: CreateJob
  196. //
  197. // Synopsis: creates the named job in the jobs folder
  198. //
  199. //-----------------------------------------------------------------------------
  200. HRESULT
  201. CreateJob(ISchedulingAgent * pSched, TCHAR * ptszFileName)
  202. {
  203. ITask * pJob;
  204. #if defined(UNICODE)
  205. HRESULT hr = pSched->NewJob(ptszFileName, IID_ITask, (IUnknown **)&pJob);
  206. #else // convert from ANSI to UNICODE
  207. WCHAR wszName[SCH_BIGBUF_LEN];
  208. MultiByteToWideChar(CP_ACP, 0, ptszFileName, -1, wszName, SCH_BIGBUF_LEN);
  209. HRESULT hr = pSched->NewJob(wszName, IID_ITask, (IUnknown **)&pJob);
  210. #endif
  211. TEST_HR(hr, "pSched->NewJob", return hr);
  212. pJob->Release();
  213. printf("\nJob %S successfully created!\n", ptszFileName);
  214. return hr;
  215. }
  216. //+----------------------------------------------------------------------------
  217. //
  218. // Function: CreateJob
  219. //
  220. // Synopsis: creates an AT job in the AT jobs folder
  221. //
  222. //-----------------------------------------------------------------------------
  223. HRESULT
  224. CreateAtJob(ISchedulingAgent * pSched)
  225. {
  226. HRESULT hr = E_NOTIMPL;
  227. #if !defined(_CHICAGO_) // no AT support on Chicago
  228. AT_INFO At;
  229. At.Flags = 0;
  230. At.JobTime = 655 * JOB_MILLISECONDS_PER_MINUTE; // run at 10:50
  231. At.DaysOfWeek = 0x1f; // weekdays
  232. At.DaysOfMonth = 0x00001000; // on the twelfth
  233. At.Command = TEXT("sol.exe");
  234. DWORD dwID;
  235. hr = ((CSchedule *)pSched)->AddAtJob(At, &dwID);
  236. TEST_HR(hr, "pSched->AddAtJob", return hr);
  237. printf("\nAT Job 'at%d.job' successfully created!\n", dwID);
  238. #endif // #if !defined(_CHICAGO_)
  239. return hr;
  240. }
  241. //+----------------------------------------------------------------------------
  242. //
  243. // Function: PrintRunTimes
  244. //
  245. // Synopsis: print out the next cRuns run times
  246. //
  247. //-----------------------------------------------------------------------------
  248. HRESULT
  249. PrintRunTimes(ISchedulingAgent * pSched)
  250. {
  251. SYSTEMTIME stNow;
  252. GetLocalTime(&stNow);
  253. LPSYSTEMTIME pstRuns;
  254. HRESULT hr;
  255. IEnumJobs * pEnumJobs;
  256. hr = pSched->Enum(&pEnumJobs);
  257. TEST_HR(hr, "pSched->Enum", return hr);
  258. ITask * pJob;
  259. SYSTEMTIME stEnd = stNow;
  260. stNow.wHour = stNow.wMinute = 0;
  261. stEnd.wHour = 23;
  262. stEnd.wMinute = 59;
  263. LPWSTR * rgpwszJobNames;
  264. ULONG cFetched;
  265. do
  266. {
  267. //hr = pEnumJobs->Skip(1);
  268. //TEST_HR(hr, "pEnumJobs->Skip", pEnumJobs->Release(); return hr);
  269. hr = pEnumJobs->Next(3, &rgpwszJobNames, &cFetched);
  270. TEST_HR(hr, "pEnumJobs->Next", break);
  271. ITask * pJob;
  272. for (ULONG i = 0; i < cFetched; i++)
  273. {
  274. hr = pSched->Activate(rgpwszJobNames[i], IID_ITask,
  275. (IUnknown **)&pJob);
  276. if (FAILED(hr))
  277. {
  278. fprintf(stderr, "pSched->Activate failed with error 0x%x\n",
  279. hr);
  280. break;
  281. }
  282. WORD cRuns = 0;
  283. LPSYSTEMTIME pstRuns;
  284. hr = pJob->GetRunTimes(&stNow, &stEnd, &cRuns, &pstRuns);
  285. TEST_HR(hr, "pJob->GetRunTimes", break);
  286. if (cRuns == 0)
  287. {
  288. hr = S_OK; // hr had been set to S_FALSE
  289. printf("There are no runs scheduled for %S.\n",
  290. rgpwszJobNames[i]);
  291. }
  292. else
  293. {
  294. printf("Runs scheduled for %S:\n", rgpwszJobNames[i]);
  295. for (WORD i = 0; i < cRuns; i++)
  296. {
  297. printf("%02d/%02d/%4d at %02d:%02d\n",
  298. pstRuns[i].wMonth, pstRuns[i].wDay,
  299. pstRuns[i].wYear, pstRuns[i].wHour,
  300. pstRuns[i].wMinute);
  301. //CTimeNode * ptn = new CTimeNode(&(pstRuns[i]));
  302. //if (ptn == NULL)
  303. //{
  304. // ERR_OUT("new CTimeNode", E_OUTOFMEMORY);
  305. // hr = E_OUTOFMEMORY;
  306. // break;
  307. //}
  308. }
  309. CoTaskMemFree(pstRuns);
  310. }
  311. pJob->Release();
  312. }
  313. for (i = 0; i < cFetched; i++)
  314. {
  315. CoTaskMemFree(rgpwszJobNames[i]);
  316. }
  317. if (cFetched != 0)
  318. {
  319. CoTaskMemFree(rgpwszJobNames);
  320. }
  321. }
  322. while (hr == S_OK);
  323. pEnumJobs->Release();
  324. return (hr == S_FALSE) ? S_OK : hr;
  325. }
  326. //+----------------------------------------------------------------------------
  327. //
  328. // Function: Usage
  329. //
  330. //-----------------------------------------------------------------------------
  331. void
  332. Usage(int ExitCode)
  333. {
  334. printf("\nSCHTEST: Schedule object test harness\n\n");
  335. if (ExitCode == EXIT_FAILURE)
  336. {
  337. printf("ERROR: incorrect command line!\n\n");
  338. }
  339. #if defined(_CHICAGO_) // no AT command support on Chicago
  340. printf("Usage: schtest [/t <computer>] [/r] [/c <jobname>]\n");
  341. printf(" /t <computer name> - target another computer (no leading slashes)\n");
  342. #else // not on Win9x
  343. printf("Usage: schtest [/t <computer>] [/a] [/r] [/c <jobname>] \n");
  344. printf(" /t <computer name> - target another computer (no leading slashes)\n");
  345. printf(" /a - create a new AT job in the AT jobs folder\n");
  346. #endif
  347. printf(" /r - print out today's run times for all job\n");
  348. printf(" /c <job name> - create a new job object in the jobs folder\n");
  349. exit(ExitCode);
  350. }