Leaked source code of windows server 2003
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.

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