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