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.

468 lines
13 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. treg.c
  5. Abstract:
  6. Test for cluster registry API
  7. Author:
  8. John Vert (jvert) 15-Mar-1996
  9. Revision History:
  10. --*/
  11. #include "windows.h"
  12. #include "cluster.h"
  13. #include "stdio.h"
  14. #include "stdlib.h"
  15. #include "conio.h"
  16. #define INDENT_LEVEL 4
  17. WCHAR Value1Data[] = L"This is data for value 1";
  18. WCHAR Value2Data[] = L"This is data for value 2";
  19. WCHAR Value3Data[] = L"This is data for value 3";
  20. VOID
  21. DumpKeyWorker(
  22. IN HKEY Key,
  23. IN DWORD Indent
  24. );
  25. VOID
  26. DumpValues(
  27. IN HKEY Key,
  28. IN DWORD Indent
  29. );
  30. int
  31. _cdecl
  32. main (argc, argv)
  33. int argc;
  34. char *argv[];
  35. {
  36. HCLUSTER Cluster;
  37. HRESOURCE Resource;
  38. HGROUP Group;
  39. HNODE Node;
  40. HKEY ClusterRoot;
  41. HKEY TestKey;
  42. HCLUSENUM ResEnum;
  43. DWORD ClusterCountBefore, ClusterCountAfter;
  44. DWORD i;
  45. DWORD Status;
  46. WCHAR NameBuf[50];
  47. DWORD NameLen;
  48. DWORD Type;
  49. DWORD Disposition;
  50. //
  51. // Dump out registry structure for current cluster.
  52. //
  53. Cluster = OpenCluster(NULL);
  54. if (Cluster == NULL) {
  55. fprintf(stderr, "OpenCluster(NULL) failed %d\n",GetLastError());
  56. return(0);
  57. }
  58. ClusterRoot = GetClusterKey(Cluster, KEY_READ);
  59. if (ClusterRoot == NULL) {
  60. fprintf(stderr, "GetClusterKey failed %d\n", GetLastError());
  61. return(0);
  62. }
  63. printf("CLUSTERROOT\n");
  64. DumpKeyWorker(ClusterRoot, 4);
  65. //
  66. // Dump by object
  67. //
  68. printf("\n\nENUMERATING OBJECTS\n");
  69. ResEnum = ClusterOpenEnum(Cluster, CLUSTER_ENUM_ALL);
  70. if (ResEnum == NULL) {
  71. fprintf(stderr, "ClusterOpenEnum failed %d\n",GetLastError());
  72. return(0);
  73. }
  74. ClusterCountBefore = ClusterGetEnumCount(ResEnum);
  75. for(i=0; ; i++) {
  76. NameLen = sizeof(NameBuf)/sizeof(WCHAR);
  77. Status = ClusterEnum(ResEnum,
  78. i,
  79. &Type,
  80. NameBuf,
  81. &NameLen);
  82. if (Status == ERROR_NO_MORE_ITEMS) {
  83. break;
  84. } else if (Status != ERROR_SUCCESS) {
  85. fprintf(stderr, "ClusterEnum %d returned error %d\n",i,Status);
  86. return(0);
  87. }
  88. switch (Type) {
  89. case CLUSTER_ENUM_NODE:
  90. printf("NODE %ws\n",NameBuf);
  91. break;
  92. case CLUSTER_ENUM_RESTYPE:
  93. printf("RESOURCETYPE %ws\n",NameBuf);
  94. break;
  95. case CLUSTER_ENUM_RESOURCE:
  96. printf("RESOURCE %ws\n",NameBuf);
  97. Resource = OpenClusterResource(Cluster, NameBuf);
  98. if (Resource == NULL) {
  99. fprintf(stderr, "OpenClusterResource returned error %d\n",GetLastError());
  100. break;
  101. }
  102. ClusterRoot = GetClusterResourceKey(Resource,
  103. KEY_READ);
  104. if (ClusterRoot == NULL) {
  105. fprintf(stderr, "GetClusterResourceKey returned error %d\n",GetLastError());
  106. break;
  107. }
  108. CloseClusterResource(Resource);
  109. DumpKeyWorker(ClusterRoot, 4);
  110. break;
  111. case CLUSTER_ENUM_GROUP:
  112. printf("GROUP %ws\n",NameBuf);
  113. Group = OpenClusterGroup(Cluster, NameBuf);
  114. if (Group == NULL) {
  115. fprintf(stderr, "OpenClusterGroup returned error %d\n",GetLastError());
  116. break;
  117. }
  118. ClusterRoot = GetClusterGroupKey(Group,
  119. KEY_READ);
  120. if (ClusterRoot == NULL) {
  121. fprintf(stderr, "GetClusterResourceKey returned error %d\n",GetLastError());
  122. break;
  123. }
  124. CloseClusterGroup(Group);
  125. DumpKeyWorker(ClusterRoot, 4);
  126. break;
  127. default:
  128. fprintf(stderr, "ClusterEnum returned unknown type %d\n",Type);
  129. break;
  130. }
  131. }
  132. if (Status == ERROR_NO_MORE_ITEMS) {
  133. printf("\nCluster count: %d\n", i);
  134. ClusterCountAfter = ClusterGetEnumCount(ResEnum);
  135. if (ClusterCountBefore != ClusterCountAfter)
  136. fprintf(stderr, "\nReported cluster count was %d before enumeration, and %d afterward\n", ClusterCountBefore, ClusterCountAfter);
  137. else if (i != ClusterCountBefore)
  138. fprintf(stderr, "\nReported cluster count: %d\n", ClusterCountBefore);
  139. }
  140. ClusterCloseEnum(ResEnum);
  141. //
  142. // Test the create/delete apis
  143. //
  144. printf("\nTesting Creation APIs\n");
  145. ClusterRoot = GetClusterKey(Cluster, KEY_READ | KEY_WRITE);
  146. if (ClusterRoot == NULL) {
  147. fprintf(stderr, "GetClusterKey failed %d\n", GetLastError());
  148. return(0);
  149. }
  150. Status = ClusterRegCreateKey(ClusterRoot,
  151. L"TestKey",
  152. 0,
  153. KEY_READ | KEY_WRITE,
  154. NULL,
  155. &TestKey,
  156. &Disposition);
  157. if (Status != ERROR_SUCCESS) {
  158. fprintf(stderr, "ClusterRegCreateKey failed %d\n", Status);
  159. return(0);
  160. }
  161. if (Disposition == REG_CREATED_NEW_KEY) {
  162. printf("TestKey successfully created\n");
  163. } else if (Disposition == REG_OPENED_EXISTING_KEY) {
  164. printf("TestKey successfully opened\n");
  165. } else {
  166. fprintf(stderr,"CreateKey of TestKey returned unknown Disposition %d\n", Disposition);
  167. }
  168. Status = ClusterRegSetValue(TestKey,
  169. L"Value1",
  170. REG_SZ,
  171. (CONST BYTE*)Value1Data,
  172. (lstrlenW(Value1Data)+1)*sizeof(WCHAR));
  173. if (Status != ERROR_SUCCESS) {
  174. fprintf(stderr, "SetValue for Value1 failed %d\n", Status);
  175. }
  176. Status = ClusterRegSetValue(TestKey,
  177. L"Value2",
  178. REG_SZ,
  179. (CONST BYTE*)Value2Data,
  180. (lstrlenW(Value2Data)+1)*sizeof(WCHAR));
  181. if (Status != ERROR_SUCCESS) {
  182. fprintf(stderr, "SetValue for Value2 failed %d\n", Status);
  183. }
  184. Status = ClusterRegSetValue(TestKey,
  185. L"Value3",
  186. REG_SZ,
  187. (CONST BYTE*)Value3Data,
  188. (lstrlenW(Value3Data)+1)*sizeof(WCHAR));
  189. if (Status != ERROR_SUCCESS) {
  190. fprintf(stderr, "SetValue for Value3 failed %d\n", Status);
  191. }
  192. printf("Press a key to delete values\n");
  193. _getch();
  194. printf("Deleting values...\n");
  195. Status = ClusterRegDeleteValue(TestKey, L"Value1");
  196. if (Status != ERROR_SUCCESS) {
  197. fprintf(stderr, "DeleteValue for Value1 failed %d\n", Status);
  198. }
  199. Status = ClusterRegDeleteValue(TestKey, L"Value2");
  200. if (Status != ERROR_SUCCESS) {
  201. fprintf(stderr, "DeleteValue for Value2 failed %d\n", Status);
  202. }
  203. Status = ClusterRegDeleteValue(TestKey, L"Value3");
  204. if (Status != ERROR_SUCCESS) {
  205. fprintf(stderr, "DeleteValue for Value3 failed %d\n", Status);
  206. }
  207. printf("Press a key to delete TestKey\n");
  208. _getch();
  209. printf("Deleting TestKey");
  210. ClusterRegCloseKey(TestKey);
  211. Status = ClusterRegDeleteKey(ClusterRoot, L"TestKey");
  212. if (Status != ERROR_SUCCESS) {
  213. fprintf(stderr, "DeleteKey failed %d\n", Status);
  214. }
  215. ClusterRegCloseKey(ClusterRoot);
  216. CloseCluster(Cluster);
  217. }
  218. VOID
  219. DumpKeyWorker(
  220. IN HKEY Key,
  221. IN DWORD Indent
  222. )
  223. /*++
  224. Routine Description:
  225. Recursively dumps out the specified cluster registry key
  226. Arguments:
  227. Key - Supplies the root of the subtree to dump
  228. Indent - Supplies the current indent level
  229. Return Value:
  230. None.
  231. --*/
  232. {
  233. DWORD i;
  234. DWORD j;
  235. HKEY Subkey;
  236. DWORD CurrentLength=80;
  237. DWORD Length;
  238. LPWSTR Buffer;
  239. FILETIME FileTime;
  240. LONG Status;
  241. //
  242. // Enumerate our values
  243. //
  244. DumpValues(Key, Indent);
  245. //
  246. // Enumerate the subkeys and dump each one.
  247. //
  248. Buffer = malloc(CurrentLength*sizeof(WCHAR));
  249. if (Buffer == NULL) {
  250. fprintf(stderr, "DumpKeyWorker: out of memory\n");
  251. return;
  252. }
  253. for (i=0; ; i++) {
  254. retry:
  255. Length = CurrentLength;
  256. Status = ClusterRegEnumKey(Key,
  257. i,
  258. Buffer,
  259. &Length,
  260. &FileTime);
  261. if (Status == ERROR_MORE_DATA) {
  262. CurrentLength = Length+1;
  263. free(Buffer);
  264. Buffer = malloc(CurrentLength*sizeof(WCHAR));
  265. if (Buffer == NULL) {
  266. fprintf(stderr, "DumpKeyWorker: out of memory\n");
  267. }
  268. goto retry;
  269. } else if (Status == ERROR_NO_MORE_ITEMS) {
  270. break;
  271. } else if (Status != ERROR_SUCCESS) {
  272. fprintf(stderr, "DumpKeyWorker, ClusterRegEnumKey returned %d\n",Status);
  273. return;
  274. }
  275. //
  276. // print out name
  277. //
  278. for (j=0;j<Indent;j++) {
  279. printf(" ");
  280. }
  281. printf("%ws\n",Buffer);
  282. //
  283. // Open the key and call ourself recursively
  284. //
  285. Status = ClusterRegOpenKey(Key,
  286. Buffer,
  287. KEY_READ,
  288. &Subkey);
  289. if (Status != ERROR_SUCCESS) {
  290. fprintf(stderr, "DumpKeyWorker, ClusterRegOpenKey returned %d\n",Status);
  291. return;
  292. }
  293. DumpKeyWorker(Subkey, Indent+INDENT_LEVEL);
  294. Status = ClusterRegCloseKey(Subkey);
  295. if (Status != ERROR_SUCCESS) {
  296. fprintf(stderr, "DumpKeyWorker, ClusterRegCloseKey returned %d\n",Status);
  297. }
  298. }
  299. free(Buffer);
  300. }
  301. VOID
  302. DumpValues(
  303. IN HKEY Key,
  304. IN DWORD Indent
  305. )
  306. /*++
  307. Routine Description:
  308. Dumps the values of the specified key.
  309. Arguments:
  310. Key - Supplies the key to dump.
  311. Indent - Supplies the indentation level to use.
  312. Return Value:
  313. None.
  314. --*/
  315. {
  316. DWORD i;
  317. DWORD j;
  318. HKEY Subkey;
  319. DWORD CurrentNameLength=80;
  320. DWORD NameLength;
  321. LPWSTR Name;
  322. DWORD CurrentDataLength=80;
  323. DWORD DataLength;
  324. PUCHAR Data ;
  325. LONG Status;
  326. DWORD Type;
  327. //
  328. // Enumerate the values and dump each one.
  329. //
  330. Name = malloc(CurrentNameLength*sizeof(WCHAR));
  331. if (Name==NULL) {
  332. fprintf(stderr, "DumpValues: out of memory\n");
  333. return;
  334. }
  335. Data = malloc(CurrentDataLength);
  336. if (Name==NULL) {
  337. fprintf(stderr, "DumpValues: out of memory\n");
  338. return;
  339. }
  340. for (i=0; ;i++) {
  341. retry:
  342. NameLength = CurrentNameLength;
  343. DataLength = CurrentDataLength;
  344. Status = ClusterRegEnumValue(Key,
  345. i,
  346. Name,
  347. &NameLength,
  348. &Type,
  349. Data,
  350. &DataLength);
  351. if (Status == ERROR_MORE_DATA) {
  352. if (NameLength+1 > CurrentNameLength) {
  353. CurrentNameLength = NameLength+1;
  354. free(Name);
  355. Name = malloc(CurrentNameLength);
  356. if (Name == NULL) {
  357. fprintf(stderr, "DumpValues: out of memory\n");
  358. return;
  359. }
  360. }
  361. if (DataLength > CurrentDataLength) {
  362. CurrentDataLength = DataLength;
  363. free(Data);
  364. Data = malloc(CurrentDataLength);
  365. if (Data == NULL) {
  366. fprintf(stderr, "DumpValues: out of memory\n");
  367. return;
  368. }
  369. }
  370. goto retry;
  371. } else if (Status == ERROR_NO_MORE_ITEMS) {
  372. return;
  373. } else if (Status != ERROR_SUCCESS) {
  374. fprintf(stderr, "DumpValues: out of memory\n");
  375. return;
  376. }
  377. //
  378. // print out value
  379. //
  380. for (j=0;j<Indent;j++) {
  381. printf(" ");
  382. }
  383. printf("%ws = ",Name);
  384. switch (Type) {
  385. case REG_SZ:
  386. printf("REG_SZ %ws\n",Data);
  387. break;
  388. case REG_DWORD:
  389. printf("REG_DWORD 0x%08lx\n",Data);
  390. break;
  391. case REG_BINARY:
  392. printf("REG_BINARY 0x%08lx\n",Data);
  393. break;
  394. default:
  395. printf("UNKNOWN TYPE %d\n",Type);
  396. break;
  397. }
  398. }
  399. free(Name);
  400. free(Data);
  401. }