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.

517 lines
14 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Scheduling Agent Unit Test
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  7. //
  8. // File: st.cxx
  9. //
  10. // Contents: sage API compatability test harness
  11. //
  12. // History: 26-Jun-96 EricB created
  13. //
  14. //-----------------------------------------------------------------------------
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include "..\..\inc\sage.hxx"
  18. // flags
  19. #define ST_DETECT 0x1
  20. #define ST_ENABLE 0x2
  21. #define ST_TERMINATE 0x4
  22. #define ST_ADDTASK 0x8
  23. #define ST_GETTASK 0x10
  24. #define ST_REMOVETASK 0x20
  25. // prototypes
  26. void Usage(int ExitCode);
  27. BOOL AddTask(HWND);
  28. BOOL GetTask(HWND, int);
  29. BOOL RemoveTask(HWND, int);
  30. BOOL SetSageInputs (int *IOnum, TaskInfo *pti);
  31. BOOL GetSageInputs (int IOnum, TaskInfo *pti);
  32. //
  33. // Function pointers.
  34. //
  35. typedef int (*PFCNVOID)(VOID);
  36. typedef int (*PFCNINT)(int);
  37. //+----------------------------------------------------------------------------
  38. //
  39. // Function: main
  40. //
  41. // Synopsis: entry point
  42. //
  43. //-----------------------------------------------------------------------------
  44. int _cdecl
  45. main(int argc, char ** argv)
  46. {
  47. if (argc < 2 || argc > 3)
  48. {
  49. Usage(EXIT_SUCCESS);
  50. }
  51. DWORD dwTest = 0;
  52. int iParam = 0;
  53. char szMsg[80] = "\n** Sage API test";
  54. LPSTR pszCmdLine = GetCommandLineA();
  55. // skip the program name
  56. CHAR * pszToken = strpbrk(pszCmdLine, TEXT(" \t"));
  57. // delimit the first token
  58. pszToken = strtok(pszToken, TEXT(" \t"));
  59. // parse command line
  60. do {
  61. switch (*pszToken)
  62. {
  63. case TEXT('/'):
  64. case TEXT('-'):
  65. pszToken++;
  66. switch(*pszToken)
  67. {
  68. case TEXT('d'):
  69. case TEXT('D'):
  70. dwTest = ST_DETECT;
  71. strcat(szMsg, "; System_Agent_Detect");
  72. break;
  73. case TEXT('e'):
  74. case TEXT('E'):
  75. dwTest = ST_ENABLE;
  76. pszToken++;
  77. TCHAR * ptszFunction;
  78. ptszFunction = strtok(NULL, TEXT(" \t"));
  79. if (ptszFunction == NULL || *ptszFunction == TEXT('/') ||
  80. *ptszFunction == TEXT('-'))
  81. {
  82. Usage(EXIT_FAILURE);
  83. }
  84. iParam = (WORD)strtol(ptszFunction, NULL, 10);
  85. sprintf(szMsg + strlen(szMsg), "; System_Agent_Enable(%i)",
  86. iParam);
  87. break;
  88. case TEXT('t'):
  89. case TEXT('T'):
  90. dwTest = ST_TERMINATE;
  91. strcat(szMsg, "; System_Agent_Terminate");
  92. break;
  93. case TEXT('a'):
  94. case TEXT('A'):
  95. dwTest = ST_ADDTASK;
  96. strcat(szMsg, "; AddTask");
  97. break;
  98. case TEXT('g'):
  99. case TEXT('G'):
  100. dwTest = ST_GETTASK;
  101. pszToken++;
  102. ptszFunction = strtok(NULL, TEXT(" \t"));
  103. if (ptszFunction == NULL || *ptszFunction == TEXT('/') ||
  104. *ptszFunction == TEXT('-'))
  105. {
  106. Usage(EXIT_FAILURE);
  107. }
  108. iParam = (WORD)strtol(ptszFunction, NULL, 10);
  109. sprintf(szMsg + strlen(szMsg), "; GetTask(%i)",
  110. iParam);
  111. break;
  112. case TEXT('r'):
  113. case TEXT('R'):
  114. dwTest = ST_REMOVETASK;
  115. pszToken++;
  116. ptszFunction = strtok(NULL, TEXT(" \t"));
  117. if (ptszFunction == NULL || *ptszFunction == TEXT('/') ||
  118. *ptszFunction == TEXT('-'))
  119. {
  120. Usage(EXIT_FAILURE);
  121. }
  122. iParam = (WORD)strtol(ptszFunction, NULL, 10);
  123. sprintf(szMsg + strlen(szMsg), "; RemoveTask(%i)",
  124. iParam);
  125. break;
  126. case TEXT('h'):
  127. case TEXT('H'):
  128. case TEXT('?'):
  129. Usage(EXIT_SUCCESS);
  130. default:
  131. Usage(EXIT_FAILURE);
  132. }
  133. break;
  134. default:
  135. // not a switch character (/ or -)
  136. Usage(EXIT_FAILURE);
  137. }
  138. pszToken = strtok(NULL, TEXT(" \t"));
  139. } while (pszToken);
  140. strcat(szMsg, "\n\n");
  141. printf(szMsg);
  142. HWND hAgent = NULL;
  143. HINSTANCE hSageLib = NULL;
  144. if (dwTest == ST_DETECT || dwTest == ST_ENABLE || dwTest == ST_TERMINATE)
  145. {
  146. hSageLib = LoadLibrary("SAGE.DLL");
  147. if (hSageLib == NULL)
  148. {
  149. printf("Load of sage.dll failed with error %d\n", GetLastError());
  150. goto Err;
  151. }
  152. }
  153. else
  154. {
  155. hAgent = FindWindow ("SAGEWINDOWCLASS", "SYSTEM AGENT COM WINDOW");
  156. if (hAgent == NULL)
  157. {
  158. printf("16 bit API test: Scheduling Agent not running!\n");
  159. goto Err;
  160. }
  161. }
  162. int iRet;
  163. PFCNVOID pfnDetect, pfnTerminate;
  164. PFCNINT pfnEnable;
  165. switch (dwTest)
  166. {
  167. case ST_DETECT:
  168. pfnDetect = (PFCNVOID)GetProcAddress(hSageLib, "System_Agent_Detect");
  169. if (pfnDetect == NULL)
  170. {
  171. printf("GetProcAddress failed with error %d\n", GetLastError());
  172. goto Err;
  173. }
  174. iRet = pfnDetect();
  175. printf("System_Agent_Detect returned %d\n", iRet);
  176. break;
  177. case ST_ENABLE:
  178. pfnEnable = (PFCNINT)GetProcAddress(hSageLib, "System_Agent_Enable");
  179. if (pfnEnable == NULL)
  180. {
  181. printf("GetProcAddress failed with error %d\n", GetLastError());
  182. goto Err;
  183. }
  184. iRet = pfnEnable(iParam);
  185. printf("System_Agent_Enable returned %d\n", iRet);
  186. break;
  187. case ST_TERMINATE:
  188. pfnTerminate = (PFCNVOID)GetProcAddress(hSageLib,
  189. "System_Agent_Terminate");
  190. if (pfnTerminate == NULL)
  191. {
  192. printf("GetProcAddress failed with error %d\n", GetLastError());
  193. goto Err;
  194. }
  195. iRet = pfnTerminate();
  196. printf("System_Agent_Terminate returned %d\n", iRet);
  197. break;
  198. case ST_ADDTASK:
  199. if (!AddTask(hAgent))
  200. {
  201. goto Err;
  202. }
  203. break;
  204. case ST_GETTASK:
  205. if (!GetTask(hAgent, iParam))
  206. {
  207. goto Err;
  208. }
  209. break;
  210. case ST_REMOVETASK:
  211. if (!RemoveTask(hAgent, iParam))
  212. {
  213. goto Err;
  214. }
  215. break;
  216. default:
  217. Usage(EXIT_FAILURE);
  218. }
  219. printf("\n** Test successfully completed! **\n");
  220. return(EXIT_SUCCESS);
  221. Err:
  222. printf("\n** Test failed.\n");
  223. return(EXIT_FAILURE);
  224. }
  225. char *pszREGIOpath = "Software\\Microsoft\\Plus!\\System Agent";
  226. char *pszREGIOkey = "IOKey%lu";
  227. #define cbRESOURCE 256
  228. #define nKeySageMAX 100 // Don't try over 100 keys
  229. #define SAGE_ADDTASK (WM_USER + 9)
  230. #define SAGE_REMOVETASK (WM_USER + 10)
  231. #define SAGE_GETTASK (WM_USER + 11)
  232. //+----------------------------------------------------------------------------
  233. //
  234. // Function: AddTask
  235. //
  236. //-----------------------------------------------------------------------------
  237. BOOL AddTask(HWND hAgent)
  238. {
  239. TaskInfo ti = {0};
  240. ti.StructureSize = sizeof(TaskInfo);
  241. ti.BeginTime.wYear = (WORD)-1;
  242. ti.BeginTime.wMonth = (WORD)-1;
  243. ti.BeginTime.wDay = (WORD)-1;
  244. ti.BeginTime.wDayOfWeek = (WORD)-1;
  245. ti.BeginTime.wDay = (WORD)-1;
  246. ti.BeginTime.wHour = 12;
  247. ti.BeginTime.wMinute = 10;
  248. ti.EndTime.wYear = (WORD)-1;
  249. ti.EndTime.wMonth = (WORD)-1;
  250. ti.EndTime.wDay = (WORD)-1;
  251. ti.EndTime.wDayOfWeek = (WORD)-1;
  252. ti.EndTime.wDay = (WORD)-1;
  253. ti.EndTime.wHour = 13;
  254. ti.EndTime.wMinute = 50;
  255. ti.Time_Granularity = 900; // seconds = 15 minutes.
  256. ti.StopAfterTime = 600; // seconds = 10 minutes.
  257. lstrcpy(ti.CommandLine, "sol");
  258. lstrcpy(ti.Comment, "this is a test of the 16 bit Sage support.");
  259. GetStartupInfo(&ti.StartupInfo);
  260. ti.StartupInfo.cb = sizeof(STARTUPINFO);
  261. ti.CreatorId = (unsigned long)GetCurrentProcess();
  262. int IOnum;
  263. if (!SetSageInputs(&IOnum, &ti))
  264. return FALSE;
  265. LRESULT lRet =
  266. SendMessage(hAgent, SAGE_ADDTASK, (WPARAM)IOnum, (LPARAM)ti.CreatorId);
  267. SetSageInputs(&IOnum, NULL); // Delete the regkey
  268. if (lRet > 0)
  269. {
  270. printf("AddTask added a task with Task_Identifier of %u.\n", lRet);
  271. }
  272. else
  273. {
  274. printf("AddTask failed to add a task.\n");
  275. return FALSE;
  276. }
  277. return TRUE;
  278. }
  279. //+----------------------------------------------------------------------------
  280. //
  281. // Function: GetTask
  282. //
  283. //-----------------------------------------------------------------------------
  284. BOOL GetTask(HWND hAgent, int iTask)
  285. {
  286. char szValue[cbRESOURCE];
  287. int IOnum;
  288. HKEY hk;
  289. DWORD dwType;
  290. if (RegCreateKey(HKEY_LOCAL_MACHINE, pszREGIOpath, &hk) != ERROR_SUCCESS)
  291. return FALSE;
  292. for (IOnum = 0; IOnum < nKeySageMAX; IOnum++)
  293. {
  294. wsprintf(szValue, pszREGIOkey, (unsigned long)IOnum);
  295. if (RegQueryValueEx(hk, szValue, NULL, &dwType, NULL, NULL)
  296. != ERROR_SUCCESS)
  297. {
  298. break;
  299. }
  300. }
  301. RegCloseKey(hk);
  302. LRESULT lRet =
  303. SendMessage(hAgent, SAGE_GETTASK, (WPARAM)IOnum, (LPARAM)iTask);
  304. TaskInfo ti;
  305. if (lRet > 0)
  306. {
  307. printf("GetTask returned a task with Task_Identifier of %u.\n", lRet);
  308. }
  309. else
  310. {
  311. printf("GetTask failed to return a task.\n");
  312. return FALSE;
  313. }
  314. if (!GetSageInputs(IOnum, &ti))
  315. {
  316. printf("Unable to read the task from the registry!\n");
  317. return FALSE;
  318. }
  319. printf("The task data has been left in the registry at:\n"
  320. "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Plus!\\System Agent: "
  321. "IOKey%lu\n", (unsigned long)IOnum);
  322. return TRUE;
  323. }
  324. //+----------------------------------------------------------------------------
  325. //
  326. // Function: RemoveTask
  327. //
  328. //-----------------------------------------------------------------------------
  329. BOOL RemoveTask(HWND hAgent, int iTask)
  330. {
  331. LRESULT lRet =
  332. SendMessage(hAgent, SAGE_REMOVETASK, 0, (LPARAM)iTask);
  333. if (lRet > 0)
  334. {
  335. printf("RemoveTask deleted a task with Task_Identifier of %u.\n", iTask);
  336. }
  337. else
  338. {
  339. printf("RemoveTask failed to delete a task.\n");
  340. return FALSE;
  341. }
  342. return TRUE;
  343. }
  344. /*** SetSageInputs - Create or remove a regkey for IPC with SAGE
  345. *
  346. * If called with a non-NULL {TaskInfo*}, this function will create a new
  347. * registry value for IPC with SAGE, returning in {*IOnum} an index to that
  348. * new registry value.
  349. *
  350. * If called with a NULL {TaskInfo*}, this function will remove the registry
  351. * key indicated by {*IOnum}.
  352. *
  353. * In both cases, TRUE is returned upon success.
  354. *
  355. */
  356. BOOL
  357. SetSageInputs(int *IOnum, TaskInfo *pti)
  358. {
  359. HKEY hk;
  360. DWORD cb;
  361. DWORD dwType;
  362. char value[cbRESOURCE];
  363. DWORD rc;
  364. BOOL fDone = FALSE;
  365. if (RegCreateKey(HKEY_LOCAL_MACHINE, pszREGIOpath, &hk) != ERROR_SUCCESS)
  366. return FALSE;
  367. if (pti == NULL) // Delete the previously-used key?
  368. {
  369. wsprintf(value, pszREGIOkey, (unsigned long)(*IOnum));
  370. RegDeleteValue(hk, value);
  371. }
  372. else // Or create a new key?
  373. {
  374. for (*IOnum = 0; (*IOnum) < nKeySageMAX; (*IOnum)++)
  375. {
  376. wsprintf(value, pszREGIOkey, (unsigned long)(*IOnum));
  377. rc = RegQueryValueEx(hk, value, NULL, &dwType, NULL, NULL);
  378. if (rc != ERROR_SUCCESS)
  379. break;
  380. }
  381. if ((*IOnum) >= nKeySageMAX) // Never found a spot for a free key?
  382. {
  383. RegCloseKey (hk);
  384. return FALSE;
  385. }
  386. cb = sizeof(*pti);
  387. rc = RegSetValueEx(hk, value, NULL, REG_BINARY, (const BYTE *)pti, cb);
  388. if (rc != ERROR_SUCCESS) // If we couldn't write to the registry
  389. { // then something bad is happening.
  390. RegDeleteKey(hk, value);
  391. RegCloseKey(hk);
  392. return FALSE;
  393. }
  394. RegCloseKey (hk);
  395. }
  396. return TRUE;
  397. }
  398. BOOL
  399. GetSageInputs(int IOnum, TaskInfo *pti)
  400. {
  401. char value[cbRESOURCE];
  402. HKEY hk;
  403. DWORD cb;
  404. DWORD dwType;
  405. LONG rc;
  406. if (RegOpenKey(HKEY_LOCAL_MACHINE, pszREGIOpath, &hk) != ERROR_SUCCESS)
  407. return FALSE;
  408. wsprintf(value, pszREGIOkey, (unsigned long)IOnum);
  409. cb = sizeof(*pti);
  410. rc = RegQueryValueEx(hk, value, NULL, &dwType, (LPBYTE)pti, &cb);
  411. return (rc == ERROR_SUCCESS) ? TRUE : FALSE;
  412. }
  413. //+----------------------------------------------------------------------------
  414. //
  415. // Function: Usage
  416. //
  417. //-----------------------------------------------------------------------------
  418. void
  419. Usage(int ExitCode)
  420. {
  421. printf("\nST: sage API compatibility test harness\n\n");
  422. if (ExitCode == EXIT_FAILURE)
  423. {
  424. printf("ERROR: incorrect command line!\n\n");
  425. }
  426. printf("Usage: st [/d] | [/e <#>] | [/t] | [/a] | [/g <#>] | [/r <#>]\n");
  427. printf(" /d - System_Agent_Detect\n");
  428. printf(" returns the Scheduling Agent version if running\n");
  429. printf(" /e - System_Agent_Enable\n");
  430. printf(" # can be one of 1, 2, or 3. If # is 3, then returns 0, 1 or 2\n");
  431. printf(" /t - System_Agent_Terminate\n");
  432. printf(" cause the service to exit.\n");
  433. printf(" /a - AddTask\n");
  434. printf(" uses the 16 bit method to add a pre-canned task.\n");
  435. printf(" /g - GetTask\n");
  436. printf(" uses the 16 bit method to get task #.\n");
  437. printf(" /r - RemoveTask\n");
  438. printf(" uses the 16 bit method to remove task #.\n");
  439. exit(ExitCode);
  440. }