0ed731d41f5f87df15b22d8b27dd92699f9b5b44
[reactos.git] / rosapps / tests / hivetest / hivetest.c
1 #include <stdarg.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 #include <windows.h>
6 #define NTOS_MODE_USER
7 #include <ndk/ntndk.h>
8
9 HANDLE OutputHandle;
10 HANDLE InputHandle;
11
12 void dprintf(char* fmt, ...)
13 {
14 va_list args;
15 char buffer[255];
16
17 va_start(args,fmt);
18 vsprintf(buffer,fmt,args);
19 WriteConsoleA(OutputHandle, buffer, strlen(buffer), NULL, NULL);
20 va_end(args);
21 }
22
23 void do_enumeratekey(PWSTR Name)
24 {
25 ULONG Index,Length,i;
26 KEY_BASIC_INFORMATION KeyInformation[5];
27 NTSTATUS Status;
28 OBJECT_ATTRIBUTES ObjectAttributes;
29 HANDLE hKey1;
30 UNICODE_STRING KeyName;
31
32 RtlInitUnicodeString(&KeyName, Name);
33 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE
34 , NULL, NULL);
35 Status=NtOpenKey( &hKey1, MAXIMUM_ALLOWED, &ObjectAttributes);
36 dprintf("NtEnumerateKey : \n");
37 Index=0;
38 while(Status == STATUS_SUCCESS)
39 {
40 Status=NtEnumerateKey(hKey1,Index++,KeyBasicInformation
41 ,&KeyInformation[0], sizeof(KeyInformation)
42 ,&Length);
43 if(Status== STATUS_SUCCESS)
44 {
45 dprintf("\tSubKey Name = ");
46 for (i=0;i<KeyInformation[0].NameLength/2;i++)
47 dprintf("%C",KeyInformation[0].Name[i]);
48 dprintf("\n");
49 }
50 }
51 NtClose(hKey1);
52 }
53
54
55 void CreateKeyTest(void)
56 {
57 HKEY hKey;
58 OBJECT_ATTRIBUTES ObjectAttributes;
59 NTSTATUS Status;
60 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\testkey");
61
62 dprintf("Create key '\\Registry\\Machine\\Software\\testkey':\n");
63
64 InitializeObjectAttributes(&ObjectAttributes,
65 &KeyName,
66 OBJ_CASE_INSENSITIVE,
67 NULL,
68 NULL);
69 dprintf("NtCreateKey:\n");
70 Status = NtCreateKey(&hKey,
71 KEY_ALL_ACCESS,
72 &ObjectAttributes,
73 0,
74 NULL,
75 REG_OPTION_NON_VOLATILE,
76 NULL);
77 dprintf(" Status = %lx\n",Status);
78 if (NT_SUCCESS(Status))
79 {
80 NtClose(hKey);
81 }
82 }
83
84
85 void DeleteKeyTest(void)
86 {
87 OBJECT_ATTRIBUTES ObjectAttributes;
88 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\testkey");
89 HKEY hKey;
90 NTSTATUS Status;
91
92 dprintf("Delete key '\\Registry\\Machine\\Software\\testkey':\n");
93
94 InitializeObjectAttributes(&ObjectAttributes,
95 &KeyName,
96 OBJ_CASE_INSENSITIVE,
97 NULL,
98 NULL);
99 dprintf("NtOpenKey:\n");
100 Status = NtOpenKey(&hKey,
101 KEY_ALL_ACCESS,
102 &ObjectAttributes);
103 dprintf(" Status = %lx\n",Status);
104 if (!NT_SUCCESS(Status))
105 return;
106
107 dprintf("NtDeleteKey:\n");
108 Status = NtDeleteKey(hKey);
109 dprintf(" Status = %lx\n",Status);
110 NtClose(hKey);
111 }
112
113
114 void EnumerateKeyTest(void)
115 {
116 HKEY hKey = NULL;
117 OBJECT_ATTRIBUTES ObjectAttributes;
118 NTSTATUS Status;
119 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software");
120 ULONG Index;
121 ULONG Length;
122 ULONG i;
123 KEY_BASIC_INFORMATION KeyInformation[5];
124
125 dprintf("Enumerate key '\\Registry\\Machine\\Software':\n");
126
127 InitializeObjectAttributes(&ObjectAttributes,
128 &KeyName,
129 OBJ_CASE_INSENSITIVE,
130 NULL,
131 NULL);
132 dprintf("NtOpenKey:\n");
133 Status = NtOpenKey(&hKey,
134 KEY_ALL_ACCESS,
135 &ObjectAttributes);
136 dprintf(" Status = %lx\n", Status);
137 if (!NT_SUCCESS(Status))
138 return;
139
140 dprintf("NtQueryKey:\n");
141 Status = NtQueryKey(hKey,
142 KeyBasicInformation,
143 &KeyInformation[0],
144 sizeof(KeyInformation),
145 &Length);
146 dprintf(" Status = %lx\n", Status);
147 if (NT_SUCCESS(Status))
148 {
149 dprintf("\tKey Name = ");
150 for (i = 0; i < KeyInformation[0].NameLength / 2; i++)
151 dprintf("%C", KeyInformation[0].Name[i]);
152 dprintf("\n");
153 }
154
155 dprintf("NtEnumerateKey:\n");
156 Index=0;
157 while(NT_SUCCESS(Status))
158 {
159 Status = NtEnumerateKey(hKey,
160 Index,
161 KeyBasicInformation,
162 &KeyInformation[0],
163 sizeof(KeyInformation),
164 &Length);
165 if (NT_SUCCESS(Status))
166 {
167 dprintf("\tSubKey Name = ");
168 for (i = 0; i < KeyInformation[0].NameLength / 2; i++)
169 dprintf("%C", KeyInformation[0].Name[i]);
170 dprintf("\n");
171 }
172 Index++;
173 }
174
175 dprintf("NtClose:\n");
176 Status = NtClose(hKey);
177 dprintf(" Status = %lx\n", Status);
178 }
179
180
181 void SetValueTest1(void)
182 {
183 HKEY hKey;
184 OBJECT_ATTRIBUTES ObjectAttributes;
185 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\testkey");
186 UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"TestValue");
187 NTSTATUS Status;
188
189 dprintf("Create key '\\Registry\\Machine\\Software\\testkey':\n");
190
191 InitializeObjectAttributes(&ObjectAttributes,
192 &KeyName,
193 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
194 NULL,
195 NULL);
196 dprintf("NtCreateKey:\n");
197 Status = NtCreateKey(&hKey,
198 KEY_ALL_ACCESS,
199 &ObjectAttributes,
200 0,
201 NULL,
202 REG_OPTION_NON_VOLATILE,
203 NULL);
204 dprintf(" Status = %lx\n",Status);
205 if (!NT_SUCCESS(Status))
206 return;
207
208
209 dprintf("NtSetValueKey:\n");
210 Status = NtSetValueKey(hKey,
211 &ValueName,
212 0,
213 REG_SZ,
214 (PVOID)L"TestString",
215 24);
216 dprintf(" Status = %lx\n",Status);
217
218 NtClose(hKey);
219 }
220
221
222 void SetValueTest2(void)
223 {
224 HKEY hKey;
225 OBJECT_ATTRIBUTES ObjectAttributes;
226 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\testkey");
227 UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"TestValue");
228 NTSTATUS Status;
229
230 dprintf("Create key '\\Registry\\Machine\\Software\\testkey':\n");
231
232 InitializeObjectAttributes(&ObjectAttributes,
233 &KeyName,
234 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
235 NULL,
236 NULL);
237 dprintf("NtCreateKey:\n");
238 Status = NtCreateKey(&hKey,
239 KEY_ALL_ACCESS,
240 &ObjectAttributes,
241 0,
242 NULL,
243 REG_OPTION_NON_VOLATILE,
244 NULL);
245 dprintf(" Status = %lx\n",Status);
246 if (!NT_SUCCESS(Status))
247 return;
248
249 dprintf("NtSetValueKey:\n");
250 Status = NtSetValueKey(hKey,
251 &ValueName,
252 0,
253 REG_DWORD,
254 (PVOID)"reac",
255 4);
256 dprintf(" Status = %lx\n",Status);
257
258 NtClose(hKey);
259 }
260
261
262 void DeleteValueTest(void)
263 {
264 OBJECT_ATTRIBUTES ObjectAttributes;
265 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\testkey");
266 UNICODE_STRING ValueName = RTL_CONSTANT_STRING(L"TestValue");
267 HKEY KeyHandle;
268 NTSTATUS Status;
269
270 dprintf("Open key '\\Registry\\Machine\\Software\\testkey':\n");
271
272 InitializeObjectAttributes(&ObjectAttributes,
273 &KeyName,
274 OBJ_CASE_INSENSITIVE,
275 NULL,
276 NULL);
277 Status=NtOpenKey(&KeyHandle,
278 MAXIMUM_ALLOWED,
279 &ObjectAttributes);
280 dprintf(" Status = %lx\n", Status);
281 if (!NT_SUCCESS(Status))
282 return;
283
284 dprintf("Delete value:\n");
285
286
287 Status = NtDeleteValueKey(KeyHandle,
288 &ValueName);
289 dprintf(" Status = %lx\n", Status);
290
291 dprintf("Close key:\n");
292 Status = NtClose(KeyHandle);
293 dprintf(" Status = %lx\n", Status);
294 }
295
296
297 void EnumerateValueTest(void)
298 {
299 KEY_VALUE_FULL_INFORMATION KeyValueInformation[5];
300 OBJECT_ATTRIBUTES ObjectAttributes;
301 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\testkey");
302 ULONG Index,Length,i;
303 HKEY hKey = NULL;
304 NTSTATUS Status;
305
306 dprintf("Open key '\\Registry\\Machine\\Software\\testkey':\n");
307
308 InitializeObjectAttributes(&ObjectAttributes,
309 &KeyName,
310 OBJ_CASE_INSENSITIVE,
311 NULL,
312 NULL);
313 Status=NtOpenKey(&hKey,
314 MAXIMUM_ALLOWED,
315 &ObjectAttributes);
316 dprintf(" Status = %lx\n", Status);
317 if (!NT_SUCCESS(Status))
318 return;
319
320 dprintf("Enumerate values:\n");
321 Index = 0;
322 while (Status == STATUS_SUCCESS)
323 {
324 Status = NtEnumerateValueKey(hKey,
325 Index++,
326 KeyValueFullInformation,
327 &KeyValueInformation[0],
328 sizeof(KeyValueInformation),
329 &Length);
330 if (Status == STATUS_SUCCESS)
331 {
332 dprintf(" Value:DO=%d, DL=%d, NL=%d, Name = ",
333 KeyValueInformation[0].DataOffset,
334 KeyValueInformation[0].DataLength,
335 KeyValueInformation[0].NameLength);
336 for (i = 0; i < KeyValueInformation[0].NameLength / 2; i++)
337 dprintf("%C", KeyValueInformation[0].Name[i]);
338 dprintf(", Type = %d\n", KeyValueInformation[0].Type);
339
340 if (KeyValueInformation[0].Type == REG_SZ)
341 dprintf(" Value = %S\n",
342 ((char*)&KeyValueInformation[0]+KeyValueInformation[0].DataOffset));
343
344 if (KeyValueInformation[0].Type == REG_DWORD)
345 dprintf(" Value = %X\n",
346 *((DWORD*)((char*)&KeyValueInformation[0]+KeyValueInformation[0].DataOffset)));
347 }
348 }
349
350 dprintf("NtClose:\n");
351 Status = NtClose(hKey);
352 dprintf(" Status = %lx\n", Status);
353 }
354
355
356
357
358 void test1(void)
359 {
360 HKEY hKey = NULL, hKey1;
361 OBJECT_ATTRIBUTES ObjectAttributes;
362 NTSTATUS Status;
363 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software");
364 ULONG Index,Length,i;
365 KEY_BASIC_INFORMATION KeyInformation[5];
366
367 #if 0
368 dprintf("NtOpenKey \\Registry : ");
369 #endif
370 dprintf("NtOpenKey \\Registry\\Machine\\Software : ");
371 InitializeObjectAttributes(&ObjectAttributes,
372 &KeyName,
373 OBJ_CASE_INSENSITIVE,
374 NULL,
375 NULL);
376 Status=NtOpenKey( &hKey1, MAXIMUM_ALLOWED, &ObjectAttributes);
377 dprintf("\t\t\t\tStatus =%x\n",Status);
378 if(Status==0)
379 {
380 dprintf("NtQueryKey : ");
381 Status=NtQueryKey(hKey1,KeyBasicInformation
382 ,&KeyInformation[0], sizeof(KeyInformation)
383 ,&Length);
384 dprintf("\t\t\t\t\tStatus =%x\n",Status);
385 if (Status == STATUS_SUCCESS)
386 {
387 dprintf("\tKey Name = ");
388 for (i=0;i<KeyInformation[0].NameLength/2;i++)
389 dprintf("%C",KeyInformation[0].Name[i]);
390 dprintf("\n");
391 }
392 dprintf("NtEnumerateKey : \n");
393 Index=0;
394 while(Status == STATUS_SUCCESS)
395 {
396 Status=NtEnumerateKey(hKey1,Index++,KeyBasicInformation
397 ,&KeyInformation[0], sizeof(KeyInformation)
398 ,&Length);
399 if(Status== STATUS_SUCCESS)
400 {
401 dprintf("\tSubKey Name = ");
402 for (i=0;i<KeyInformation[0].NameLength/2;i++)
403 dprintf("%C",KeyInformation[0].Name[i]);
404 dprintf("\n");
405 }
406 }
407 dprintf("NtClose : ");
408 Status = NtClose( hKey1 );
409 dprintf("\t\t\t\t\tStatus =%x\n",Status);
410 }
411 NtClose(hKey);
412 }
413
414
415 void test3(void)
416 {
417 HKEY hKey;
418 OBJECT_ATTRIBUTES ObjectAttributes;
419 UNICODE_STRING KeyName;
420 NTSTATUS Status;
421 char Buffer[10];
422 DWORD Result;
423 dprintf("NtCreateKey non volatile: \n");
424 dprintf(" \\Registry\\Machine\\Software\\test3reactos: ");
425 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine\\Software\\test3reactos");
426 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE
427 , NULL, NULL);
428 Status = NtCreateKey ( &hKey, KEY_ALL_ACCESS , &ObjectAttributes
429 ,0,NULL,REG_OPTION_NON_VOLATILE,NULL);
430 dprintf("\t\tStatus=%x\n",Status);
431 NtClose(hKey);
432
433 dprintf("delete \\Registry\\Machine\\software\\test3reactos ?");
434 ReadConsoleA(InputHandle, Buffer, 3, &Result, NULL) ;
435 if (Buffer[0] != 'y' && Buffer[0] != 'Y') return;
436
437 dprintf("delete \\Registry\\Machine\\software\\test3reactos ?");
438 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine\\Software\\test3reactos");
439 InitializeObjectAttributes(&ObjectAttributes,
440 &KeyName,
441 OBJ_CASE_INSENSITIVE,
442 NULL,
443 NULL);
444 dprintf("NtOpenKey : ");
445 Status=NtOpenKey( &hKey, KEY_ALL_ACCESS, &ObjectAttributes);
446 dprintf("\t\t\t\tStatus =%x\n",Status);
447 dprintf("NtDeleteKey : ");
448 Status=NtDeleteKey(hKey);
449 dprintf("\t\t\t\tStatus =%x\n",Status);
450 NtClose(hKey);
451 }
452
453 void test4(void)
454 {
455 HKEY hKey = NULL,hKey1;
456 DWORD dwDisposition;
457 DWORD dwError;
458 DWORD RegDataType, RegDataSize;
459 BOOL GlobalFifoEnable;
460 HKEY hPortKey;
461 DWORD RegDisposition;
462 WCHAR szClass[260];
463 DWORD cchClass;
464 DWORD cSubKeys;
465 DWORD cchMaxSubkey;
466 DWORD cchMaxClass;
467 DWORD cValues;
468 DWORD cchMaxValueName;
469 DWORD cbMaxValueData;
470 DWORD cbSecurityDescriptor;
471 FILETIME ftLastWriteTime;
472 SYSTEMTIME LastWriteTime;
473
474 dprintf ("RegOpenKeyExW HKLM\\System\\Setup: ");
475 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
476 L"System\\Setup",
477 0,
478 KEY_ALL_ACCESS,
479 &hKey1);
480 dprintf("\t\tdwError =%x\n",dwError);
481 if (dwError == ERROR_SUCCESS)
482 {
483 dprintf("RegQueryInfoKeyW: ");
484 cchClass=260;
485 dwError = RegQueryInfoKeyW(hKey1
486 , szClass, &cchClass, NULL, &cSubKeys
487 , &cchMaxSubkey, &cchMaxClass, &cValues, &cchMaxValueName
488 , &cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime);
489 dprintf ("\t\t\t\tdwError %x\n", dwError);
490 FileTimeToSystemTime(&ftLastWriteTime,&LastWriteTime);
491 dprintf ("\tnb of subkeys=%d,last write : %d/%d/%d %d:%02.2d'%02.2d''%03.3d\n",cSubKeys
492 ,LastWriteTime.wMonth
493 ,LastWriteTime.wDay
494 ,LastWriteTime.wYear
495 ,LastWriteTime.wHour
496 ,LastWriteTime.wMinute
497 ,LastWriteTime.wSecond
498 ,LastWriteTime.wMilliseconds
499 );
500 }
501
502
503 dprintf ("RegOpenKeyExW: ");
504 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
505 L"System\\ControlSet001\\Services\\Serial",
506 0,
507 KEY_ALL_ACCESS,
508 &hKey);
509 dprintf ("\t\t\t\t\tdwError %x\n", dwError);
510 RegDataSize = sizeof(GlobalFifoEnable);
511 if (dwError == ERROR_SUCCESS)
512 {
513 dprintf ("RegQueryValueExW: ");
514 dwError = RegQueryValueExW(hKey,
515 L"ForceFifoEnable",
516 NULL,
517 &RegDataType,
518 (PBYTE)&GlobalFifoEnable,
519 &RegDataSize);
520 dprintf("\t\t\t\tdwError =%x\n",dwError);
521 if (dwError == 0)
522 {
523 dprintf("\tValue:DT=%d, DS=%d, Value=%d\n"
524 ,RegDataType
525 ,RegDataSize
526 ,GlobalFifoEnable);
527 }
528 }
529 dprintf ("RegCreateKeyExW: ");
530 dwError = RegCreateKeyExW(hKey,
531 L"Parameters\\Serial001",
532 0,
533 NULL,
534 0,
535 KEY_ALL_ACCESS,
536 NULL,
537 &hPortKey,
538 &RegDisposition
539 );
540 dprintf ("\t\t\t\tdwError %x\n", dwError);
541
542 dprintf ("RegCreateKeyExW: ");
543 dwError = RegCreateKeyExW (HKEY_LOCAL_MACHINE,
544 L"Software\\test4reactos\\test",
545 0,
546 NULL,
547 REG_OPTION_NON_VOLATILE,
548 KEY_ALL_ACCESS,
549 NULL,
550 &hKey,
551 &dwDisposition);
552
553 dprintf ("\t\t\t\tdwError %x ", dwError);
554 dprintf ("dwDisposition %x\n", dwDisposition);
555 if (dwError == ERROR_SUCCESS)
556 {
557 dprintf ("RegSetValueExW: ");
558 dwError = RegSetValueExW (hKey,
559 L"TestValue",
560 0,
561 REG_SZ,
562 (BYTE*)L"TestString",
563 20);
564
565 dprintf ("\t\t\t\tdwError %x\n", dwError);
566 dprintf ("RegCloseKey: ");
567 dwError = RegCloseKey (hKey);
568 dprintf ("\t\t\t\t\tdwError %x\n", dwError);
569 }
570 dprintf ("\n\n");
571
572 hKey = NULL;
573
574 dprintf ("RegCreateKeyExW: ");
575 dwError = RegCreateKeyExW (HKEY_LOCAL_MACHINE,
576 L"software\\Test",
577 0,
578 NULL,
579 REG_OPTION_VOLATILE,
580 KEY_ALL_ACCESS,
581 NULL,
582 &hKey,
583 &dwDisposition);
584
585 dprintf ("\t\t\t\tdwError %x ", dwError);
586 dprintf ("dwDisposition %x\n", dwDisposition);
587
588
589 if (dwError == ERROR_SUCCESS)
590 {
591 dprintf("RegQueryInfoKeyW: ");
592 cchClass=260;
593 dwError = RegQueryInfoKeyW(hKey
594 , szClass, &cchClass, NULL, &cSubKeys
595 , &cchMaxSubkey, &cchMaxClass, &cValues, &cchMaxValueName
596 , &cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime);
597 dprintf ("\t\t\t\tdwError %x\n", dwError);
598 FileTimeToSystemTime(&ftLastWriteTime,&LastWriteTime);
599 dprintf ("\tnb of subkeys=%d,last write : %d/%d/%d %d:%02.2d'%02.2d''%03.3d\n",cSubKeys
600 ,LastWriteTime.wMonth
601 ,LastWriteTime.wDay
602 ,LastWriteTime.wYear
603 ,LastWriteTime.wHour
604 ,LastWriteTime.wMinute
605 ,LastWriteTime.wSecond
606 ,LastWriteTime.wMilliseconds
607 );
608 dprintf ("RegCloseKey: ");
609 dwError = RegCloseKey (hKey);
610 dprintf ("\t\t\t\t\tdwError %x\n", dwError);
611 }
612 dprintf ("\nTests done...\n");
613 }
614
615 void test5(void)
616 {
617 HKEY hKey;
618 OBJECT_ATTRIBUTES ObjectAttributes;
619 UNICODE_STRING KeyName;
620 NTSTATUS Status;
621
622 dprintf("NtOpenKey : \n");
623 dprintf(" \\Registry\\Machine\\Software\\reactos : ");
624 RtlRosInitUnicodeStringFromLiteral(&KeyName,L"\\Registry\\Machine\\Software\\reactos");
625 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE
626 , NULL, NULL);
627 Status=NtOpenKey( &hKey, KEY_ALL_ACCESS, &ObjectAttributes);
628 dprintf("\t\tStatus=%x\n",Status);
629 dprintf("NtFlushKey : \n");
630 Status = NtFlushKey(hKey);
631 dprintf("\t\tStatus=%x\n",Status);
632 dprintf("NtCloseKey : \n");
633 Status=NtClose(hKey);
634 dprintf("\t\tStatus=%x\n",Status);
635 }
636
637 /* registry link create test */
638 void test6(void)
639 {
640 HKEY hKey;
641 OBJECT_ATTRIBUTES ObjectAttributes;
642 UNICODE_STRING KeyName,ValueName;
643 NTSTATUS Status;
644 KEY_VALUE_FULL_INFORMATION KeyValueInformation[5];
645 ULONG Length,i;
646
647 dprintf("Create target key\n");
648 dprintf(" Key: \\Registry\\Machine\\SOFTWARE\\Reactos\n");
649 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine\\SOFTWARE\\Reactos");
650 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE
651 , NULL, NULL);
652 Status = NtCreateKey(&hKey, KEY_ALL_ACCESS , &ObjectAttributes
653 ,0,NULL, REG_OPTION_VOLATILE,NULL);
654 dprintf(" NtCreateKey() called (Status %lx)\n",Status);
655 if (!NT_SUCCESS(Status))
656 return;
657
658 dprintf("Create target value\n");
659 dprintf(" Value: TestValue = 'Test String'\n");
660 RtlRosInitUnicodeStringFromLiteral(&ValueName, L"TestValue");
661 Status=NtSetValueKey(hKey,&ValueName,0,REG_SZ,(PVOID)L"TestString",22);
662 dprintf(" NtSetValueKey() called (Status %lx)\n",Status);
663 if (!NT_SUCCESS(Status))
664 return;
665
666 dprintf("Close target key\n");
667 NtClose(hKey);
668
669
670 dprintf("Create link key\n");
671 dprintf(" Key: \\Registry\\Machine\\SOFTWARE\\Test\n");
672 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine\\SOFTWARE\\Test");
673 InitializeObjectAttributes(&ObjectAttributes,
674 &KeyName,
675 OBJ_CASE_INSENSITIVE | OBJ_OPENLINK,
676 NULL,
677 NULL);
678 Status = NtCreateKey(&hKey,
679 KEY_ALL_ACCESS | KEY_CREATE_LINK,
680 &ObjectAttributes,
681 0,
682 NULL,
683 REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
684 NULL);
685 dprintf(" NtCreateKey() called (Status %lx)\n",Status);
686 if (!NT_SUCCESS(Status))
687 return;
688
689 dprintf("Create link value\n");
690 dprintf(" Value: SymbolicLinkValue = '\\Registry\\Machine\\SOFTWARE\\Reactos'\n");
691 RtlRosInitUnicodeStringFromLiteral(&ValueName, L"SymbolicLinkValue");
692 Status=NtSetValueKey(hKey,&ValueName,0,REG_LINK,(PVOID)L"\\Registry\\Machine\\SOFTWARE\\Reactos",68);
693 dprintf(" NtSetValueKey() called (Status %lx)\n",Status);
694 if (!NT_SUCCESS(Status))
695 {
696 dprintf("Creating link value failed! Test failed!\n");
697 NtClose(hKey);
698 return;
699 }
700
701 dprintf("Close link key\n");
702 NtClose(hKey);
703
704 dprintf("Open link key\n");
705 dprintf(" Key: \\Registry\\Machine\\SOFTWARE\\Test\n");
706 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine\\SOFTWARE\\Test");
707 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_OPENIF
708 , NULL, NULL);
709 Status = NtCreateKey(&hKey, KEY_ALL_ACCESS , &ObjectAttributes
710 ,0,NULL, REG_OPTION_VOLATILE, NULL);
711 dprintf(" NtCreateKey() called (Status %lx)\n",Status);
712 if (!NT_SUCCESS(Status))
713 return;
714
715 dprintf("Query value\n");
716 dprintf(" Value: TestValue\n");
717 RtlRosInitUnicodeStringFromLiteral(&ValueName, L"TestValue");
718 Status=NtQueryValueKey(hKey,
719 &ValueName,
720 KeyValueFullInformation,
721 &KeyValueInformation[0],
722 sizeof(KeyValueInformation),
723 &Length);
724 dprintf(" NtQueryValueKey() called (Status %lx)\n",Status);
725 if (Status == STATUS_SUCCESS)
726 {
727 dprintf(" Value: Type %d DataLength %d NameLength %d Name '",
728 KeyValueInformation[0].Type,
729 KeyValueInformation[0].DataLength,
730 KeyValueInformation[0].NameLength);
731 for (i=0; i < KeyValueInformation[0].NameLength / sizeof(WCHAR); i++)
732 dprintf("%C",KeyValueInformation[0].Name[i]);
733 dprintf("'\n");
734 if (KeyValueInformation[0].Type == REG_SZ)
735 dprintf(" Value '%S'\n",
736 KeyValueInformation[0].Name+1
737 +KeyValueInformation[0].NameLength/2);
738 }
739
740 dprintf("Close link key\n");
741 NtClose(hKey);
742
743 dprintf("Test successful!\n");
744 }
745
746 /* registry link delete test */
747 void test7(void)
748 {
749 HKEY hKey;
750 OBJECT_ATTRIBUTES ObjectAttributes;
751 UNICODE_STRING KeyName,ValueName;
752 NTSTATUS Status;
753
754 dprintf("Open link key\n");
755 dprintf(" Key: \\Registry\\Machine\\SOFTWARE\\Test\n");
756 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine\\SOFTWARE\\Test");
757 InitializeObjectAttributes(&ObjectAttributes,
758 &KeyName,
759 OBJ_CASE_INSENSITIVE | OBJ_OPENIF | OBJ_OPENLINK,
760 NULL,
761 NULL);
762 Status = NtCreateKey(&hKey,
763 KEY_ALL_ACCESS,
764 &ObjectAttributes,
765 0,
766 NULL,
767 REG_OPTION_VOLATILE | REG_OPTION_OPEN_LINK,
768 NULL);
769 dprintf(" NtCreateKey() called (Status %lx)\n",Status);
770 if (!NT_SUCCESS(Status))
771 {
772 dprintf("Could not open the link key. Please run the link create test first!\n");
773 return;
774 }
775
776 dprintf("Delete link value\n");
777 RtlRosInitUnicodeStringFromLiteral(&ValueName, L"SymbolicLinkValue");
778 Status = NtDeleteValueKey(hKey,
779 &ValueName);
780 dprintf(" NtDeleteValueKey() called (Status %lx)\n",Status);
781
782 dprintf("Delete link key\n");
783 Status=NtDeleteKey(hKey);
784 dprintf(" NtDeleteKey() called (Status %lx)\n",Status);
785
786 dprintf("Close link key\n");
787 NtClose(hKey);
788 }
789
790
791 void test8(void)
792 {
793 OBJECT_ATTRIBUTES ObjectAttributes;
794 UNICODE_STRING KeyName;
795 NTSTATUS Status;
796 LONG dwError;
797 TOKEN_PRIVILEGES NewPrivileges;
798 HANDLE Token,hKey;
799 LUID Luid;
800 BOOLEAN bRes;
801 Status=NtOpenProcessToken(GetCurrentProcess()
802 ,TOKEN_ADJUST_PRIVILEGES,&Token);
803 // ,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&Token);
804 dprintf("\t\t\t\tStatus =%x\n",Status);
805 // bRes=LookupPrivilegeValueA(NULL,SE_RESTORE_NAME,&Luid);
806 // dprintf("\t\t\t\tbRes =%x\n",bRes);
807 NewPrivileges.PrivilegeCount = 1;
808 NewPrivileges.Privileges[0].Luid = Luid;
809 // NewPrivileges.Privileges[0].Luid.u.LowPart=18;
810 // NewPrivileges.Privileges[0].Luid.u.HighPart=0;
811 NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
812
813 // Status = NtAdjustPrivilegesToken(
814 bRes = AdjustTokenPrivileges(
815 Token,
816 FALSE,
817 &NewPrivileges,
818 0,
819 NULL,
820 NULL
821 );
822 dprintf("\t\t\t\tbRes =%x\n",bRes);
823
824 // Status=NtClose(Token);
825 // dprintf("\t\t\t\tStatus =%x\n",Status);
826
827
828 RtlRosInitUnicodeStringFromLiteral(&KeyName,L"test5");
829 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE
830 , NULL, NULL);
831 Status = NtLoadKey(HKEY_LOCAL_MACHINE,&ObjectAttributes);
832 dprintf("\t\t\t\tStatus =%x\n",Status);
833 dwError=RegLoadKey(HKEY_LOCAL_MACHINE,"def"
834 ,"test5");
835 dprintf("\t\t\t\tdwError =%x\n",dwError);
836
837 dprintf("NtOpenKey \\Registry\\Machine : ");
838 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine");
839 InitializeObjectAttributes(&ObjectAttributes,
840 &KeyName,
841 OBJ_CASE_INSENSITIVE,
842 NULL,
843 NULL);
844 Status=NtOpenKey( &hKey, MAXIMUM_ALLOWED, &ObjectAttributes);
845 dprintf("\t\t\tStatus =%x\n",Status);
846 RtlRosInitUnicodeStringFromLiteral(&KeyName,L"test5");
847 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE
848 , NULL, NULL);
849 Status = NtLoadKey(hKey,&ObjectAttributes);
850 dprintf("\t\t\t\tStatus =%x\n",Status);
851 }
852
853 void test9(void)
854 {
855 HKEY hKey = NULL, hKey1;
856 OBJECT_ATTRIBUTES ObjectAttributes;
857 NTSTATUS Status;
858 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry");
859 ULONG Index,Length,i;
860 KEY_BASIC_INFORMATION KeyInformation[5];
861 KEY_VALUE_FULL_INFORMATION KeyValueInformation[5];
862
863 dprintf("NtOpenKey \\Registry : ");
864 InitializeObjectAttributes(&ObjectAttributes,
865 &KeyName,
866 OBJ_CASE_INSENSITIVE,
867 NULL,
868 NULL);
869 Status=NtOpenKey( &hKey1, MAXIMUM_ALLOWED, &ObjectAttributes);
870 dprintf("\t\t\t\tStatus =%x\n",Status);
871 if (Status == 0) {
872 dprintf("NtQueryKey : ");
873 Status = NtQueryKey(hKey1, KeyBasicInformation, &KeyInformation[0], sizeof(KeyInformation), &Length);
874 dprintf("\t\t\t\t\tStatus =%x\n",Status);
875 if (Status == STATUS_SUCCESS) {
876 dprintf("\tKey Name = ");
877 for (i=0;i<KeyInformation[0].NameLength/2;i++)
878 dprintf("%C",KeyInformation[0].Name[i]);
879 dprintf("\n");
880 }
881
882 dprintf("NtEnumerateKey : \n");
883 Index = 0;
884 while (Status == STATUS_SUCCESS) {
885 Status = NtEnumerateKey(hKey1,Index++,KeyBasicInformation,&KeyInformation[0], sizeof(KeyInformation),&Length);
886 if (Status == STATUS_SUCCESS) {
887 dprintf("\tSubKey Name = ");
888 for (i = 0; i < KeyInformation[0].NameLength / 2; i++)
889 dprintf("%C",KeyInformation[0].Name[i]);
890 dprintf("\n");
891 }
892 }
893 dprintf("NtClose : ");
894 Status = NtClose( hKey1 );
895 dprintf("\t\t\t\t\tStatus =%x\n",Status);
896 }
897 NtClose(hKey); // RobD - hKey unused so-far, should this have been hKey1 ???
898
899 dprintf("NtOpenKey \\Registry\\Machine : ");
900 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"\\Registry\\Machine");
901 InitializeObjectAttributes(&ObjectAttributes,
902 &KeyName,
903 OBJ_CASE_INSENSITIVE,
904 NULL,
905 NULL);
906 Status = NtOpenKey(&hKey1, MAXIMUM_ALLOWED, &ObjectAttributes);
907 dprintf("\t\t\tStatus =%x\n",Status);
908
909 //Status of c0000001 opening \Registry\Machine\System\CurrentControlSet\Services\Tcpip\Linkage
910
911 // dprintf("NtOpenKey System\\CurrentControlSet\\Services\\Tcpip : ");
912 // RtlRosInitUnicodeStringFromLiteral(&KeyName, L"System\\CurrentControlSet\\Services\\Tcpip");
913 #if 1
914 dprintf("NtOpenKey System\\ControlSet001\\Services\\Tcpip\\Parameters : ");
915 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"System\\ControlSet001\\Services\\Tcpip\\Parameters");
916 #else
917 dprintf("NtOpenKey System\\CurrentControlSet\\Services\\Tcpip : ");
918 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"System\\CurrentControlSet\\Services\\Tcpip");
919 #endif
920 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE, hKey1 , NULL);
921 Status = NtOpenKey(&hKey, KEY_READ , &ObjectAttributes);
922 dprintf("\t\t\tStatus =%x\n",Status);
923 if (Status == 0) {
924 dprintf("NtQueryValueKey : ");
925 RtlRosInitUnicodeStringFromLiteral(&KeyName, L"NameServer");
926 Status = NtQueryValueKey(hKey, &KeyName, KeyValueFullInformation, &KeyValueInformation[0], sizeof(KeyValueInformation), &Length);
927 dprintf("\t\t\t\tStatus =%x\n",Status);
928 if (Status == STATUS_SUCCESS) {
929 dprintf("\tValue:DO=%d, DL=%d, NL=%d, Name = "
930 ,KeyValueInformation[0].DataOffset
931 ,KeyValueInformation[0].DataLength
932 ,KeyValueInformation[0].NameLength);
933 for (i = 0; i < 10 && i < KeyValueInformation[0].NameLength / 2; i++)
934 dprintf("%C", KeyValueInformation[0].Name[i]);
935 dprintf("\n");
936 dprintf("\t\tType = %d\n", KeyValueInformation[0].Type);
937 if (KeyValueInformation[0].Type == REG_SZ)
938 //dprintf("\t\tValue = %S\n", KeyValueInformation[0].Name + 1 + KeyValueInformation[0].NameLength / 2);
939 dprintf("\t\tValue = %S\n", KeyValueInformation[0].Name + KeyValueInformation[0].NameLength / 2);
940 }
941 dprintf("NtEnumerateValueKey : \n");
942 Index = 0;
943 while (Status == STATUS_SUCCESS) {
944 Status = NtEnumerateValueKey(hKey, Index++, KeyValueFullInformation, &KeyValueInformation[0], sizeof(KeyValueInformation), &Length);
945 if (Status == STATUS_SUCCESS) {
946 dprintf("\tValue:DO=%d, DL=%d, NL=%d, Name = "
947 ,KeyValueInformation[0].DataOffset
948 ,KeyValueInformation[0].DataLength
949 ,KeyValueInformation[0].NameLength);
950 for (i = 0; i < KeyValueInformation[0].NameLength / 2; i++)
951 dprintf("%C", KeyValueInformation[0].Name[i]);
952 dprintf(", Type = %d\n", KeyValueInformation[0].Type);
953 if (KeyValueInformation[0].Type == REG_SZ)
954 dprintf("\t\tValue = %S\n", ((char*)&KeyValueInformation[0]+KeyValueInformation[0].DataOffset));
955 if (KeyValueInformation[0].Type == REG_DWORD)
956 dprintf("\t\tValue = %X\n", *((DWORD*)((char*)&KeyValueInformation[0]+KeyValueInformation[0].DataOffset)));
957 }
958 }
959 dprintf("NtClose : ");
960 Status = NtClose(hKey);
961 dprintf("\t\t\t\t\tStatus =%x\n", Status);
962 }
963 NtClose(hKey1);
964 }
965
966
967 int main(int argc, char* argv[])
968 {
969 char Buffer[10];
970 DWORD Result;
971
972 AllocConsole();
973 InputHandle = GetStdHandle(STD_INPUT_HANDLE);
974 OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
975 while(1)
976 {
977 dprintf("choose test :\n");
978 dprintf(" 0 = Exit\n");
979 dprintf(" 1 = Create key\n");
980 dprintf(" 2 = Delete key\n");
981 dprintf(" 3 = Enumerate key\n");
982 dprintf(" 4 = Set value (REG_SZ)\n");
983 dprintf(" 5 = Set value (REG_DWORD)\n");
984 dprintf(" 6 = Delete value\n");
985 dprintf(" 7 = Enumerate value\n");
986 ReadConsoleA(InputHandle, Buffer, 3, &Result, NULL) ;
987 switch (Buffer[0])
988 {
989 case '0':
990 return(0);
991
992 case '1':
993 CreateKeyTest();
994 break;
995
996 case '2':
997 DeleteKeyTest();
998 break;
999
1000 case '3':
1001 EnumerateKeyTest();
1002 break;
1003
1004 case '4':
1005 SetValueTest1();
1006 break;
1007
1008 case '5':
1009 SetValueTest2();
1010 break;
1011
1012 case '6':
1013 DeleteValueTest();
1014 break;
1015
1016 case '7':
1017 EnumerateValueTest();
1018 break;
1019 }
1020 }
1021 return(0);
1022 }
1023