93ac16216622c42a5457154c69a9da5aed0a37ff
[reactos.git] / apps / tests / regdump / regdump.c
1 #include <stdarg.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <windows.h>
5 #include <tchar.h>
6 #include <ddk/ntddk.h>
7
8 HANDLE OutputHandle;
9 HANDLE InputHandle;
10
11 void dprintf(char* fmt, ...)
12 {
13    va_list args;
14    char buffer[255];
15
16    va_start(args,fmt);
17    vsprintf(buffer,fmt,args);
18    WriteConsoleA(OutputHandle, buffer, strlen(buffer), NULL, NULL);
19    va_end(args);
20 }
21
22
23 #define MAX_NAME_LEN    500
24 /*
25 BOOL DumpRegKey(TCHAR* KeyPath, HKEY hKey)
26
27     TCHAR keyPath[1000];
28     int keyPathLen = 0;
29
30     keyPath[0] = _T('\0');
31
32         dprintf("\n[%s]\n", KeyPath);
33
34     if (hKey != NULL) {
35         HKEY hNewKey;
36         LONG errCode = RegOpenKeyEx(hKey, keyPath, 0, KEY_READ, &hNewKey);
37         if (errCode == ERROR_SUCCESS) {
38             TCHAR Name[MAX_NAME_LEN];
39             DWORD cName = MAX_NAME_LEN;
40             FILETIME LastWriteTime;
41             DWORD dwIndex = 0L;
42             while (RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, NULL, NULL, NULL, &LastWriteTime) == ERROR_SUCCESS) {
43                                 HKEY hSubKey;
44                 DWORD dwCount = 0L;
45
46                                 dprintf("\n[%s\\%s]\n", KeyPath, Name);
47
48                 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_READ, &hSubKey);
49                 if (errCode == ERROR_SUCCESS) {
50                     TCHAR SubName[MAX_NAME_LEN];
51                     DWORD cSubName = MAX_NAME_LEN;
52 //                    if (RegEnumKeyEx(hSubKey, 0, SubName, &cSubName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
53                     while (RegEnumKeyEx(hSubKey, dwCount, SubName, &cSubName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
54                         dprintf("\t%s (%d)\n", SubName, dwCount);
55                                                 cSubName = MAX_NAME_LEN;
56                         ++dwCount;
57                     }
58                 }
59                 RegCloseKey(hSubKey);
60
61                 //AddEntryToTree(hwndTV, pnmtv->itemNew.hItem, Name, NULL, dwCount);
62                 cName = MAX_NAME_LEN;
63                 ++dwIndex;
64             }
65             RegCloseKey(hNewKey);
66         }
67     } else {
68     }
69         dprintf("\n");
70     return TRUE;
71
72  */
73
74 BOOL _DumpRegKey(TCHAR* KeyPath, HKEY hKey)
75
76     if (hKey != NULL) {
77         HKEY hNewKey;
78         LONG errCode = RegOpenKeyEx(hKey, NULL, 0, KEY_READ, &hNewKey);
79         if (errCode == ERROR_SUCCESS) {
80             TCHAR Name[MAX_NAME_LEN];
81             DWORD cName = MAX_NAME_LEN;
82             FILETIME LastWriteTime;
83             DWORD dwIndex = 0L;
84                         TCHAR* pKeyName = &KeyPath[_tcslen(KeyPath)];
85             while (RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, NULL, NULL, NULL, &LastWriteTime) == ERROR_SUCCESS) {
86                                 HKEY hSubKey;
87                 DWORD dwCount = 0L;
88                                 _tcscat(KeyPath, _T("\\"));
89                                 _tcscat(KeyPath, Name);
90                                 dprintf("\n[%s]\n", KeyPath);
91                 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_READ, &hSubKey);
92                 if (errCode == ERROR_SUCCESS) {
93 #if 1
94                     _DumpRegKey(KeyPath, hSubKey);
95 #else
96                     TCHAR SubName[MAX_NAME_LEN];
97                     DWORD cSubName = MAX_NAME_LEN;
98 //                    if (RegEnumKeyEx(hSubKey, 0, SubName, &cSubName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
99                     while (RegEnumKeyEx(hSubKey, dwCount, SubName, &cSubName, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) {
100                         dprintf("\t%s (%d)\n", SubName, dwCount);
101                                                 cSubName = MAX_NAME_LEN;
102                         ++dwCount;
103                     }
104 #endif
105                 }
106                 RegCloseKey(hSubKey);
107                 cName = MAX_NAME_LEN;
108                                 *pKeyName = _T('\0');
109                                 ++dwIndex;
110             }
111             RegCloseKey(hNewKey);
112         }
113     } else {
114     }
115     return TRUE;
116
117
118 BOOL DumpRegKey(TCHAR* KeyPath, HKEY hKey)
119
120         dprintf("\n[%s]\n", KeyPath);
121     return _DumpRegKey(KeyPath, hKey);
122 }
123
124 void RegKeyPrint(int which)
125 {
126         TCHAR szKeyPath[1000];
127
128         switch (which) {
129         case '1':
130                         strcpy(szKeyPath, _T("HKEY_CLASSES_ROOT"));
131             DumpRegKey(szKeyPath, HKEY_CLASSES_ROOT);
132             break;
133         case '2':
134                         strcpy(szKeyPath, _T("HKEY_CURRENT_USER"));
135             DumpRegKey(szKeyPath, HKEY_CURRENT_USER);
136             break;
137         case '3':
138                         strcpy(szKeyPath, _T("HKEY_LOCAL_MACHINE"));
139             DumpRegKey(szKeyPath, HKEY_LOCAL_MACHINE);
140             break;
141         case '4':
142                         strcpy(szKeyPath, _T("HKEY_USERS"));
143             DumpRegKey(szKeyPath, HKEY_USERS);
144             break;
145         case '5':
146                         strcpy(szKeyPath, _T("HKEY_CURRENT_CONFIG"));
147             DumpRegKey(szKeyPath, HKEY_CURRENT_CONFIG);
148             break;
149         case '6':
150 //            DumpRegKey(szKeyPath, HKEY_CLASSES_ROOT);
151 //            DumpRegKey(szKeyPath, HKEY_CURRENT_USER);
152 //            DumpRegKey(szKeyPath, HKEY_LOCAL_MACHINE);
153 //            DumpRegKey(szKeyPath, HKEY_USERS);
154 //            DumpRegKey(szKeyPath, HKEY_CURRENT_CONFIG);
155             dprintf("unimplemented...\n");
156             break;
157                 }
158
159 }
160
161 int main(int argc, char* argv[])
162 {
163     char Buffer[10];
164         TCHAR szKeyPath[1000];
165     DWORD Result;
166
167     AllocConsole();
168     InputHandle = GetStdHandle(STD_INPUT_HANDLE);
169     OutputHandle =  GetStdHandle(STD_OUTPUT_HANDLE);
170
171         if (argc > 1) {
172
173 //              if (0 == _tcsstr(argv[1], _T("HKLM"))) {
174
175                 if (strstr(argv[1], _T("help"))) {
176
177                 } else if (strstr(argv[1], _T("HKCR"))) {
178                         RegKeyPrint('1');
179                 } else if (strstr(argv[1], _T("HKCU"))) {
180                         RegKeyPrint('2');
181                 } else if (strstr(argv[1], _T("HKLM"))) {
182                         RegKeyPrint('3');
183                 } else if (strstr(argv[1], _T("HKU"))) {
184                         RegKeyPrint('4');
185                 } else if (strstr(argv[1], _T("HKCC"))) {
186                         RegKeyPrint('5');
187                 } else if (strstr(argv[1], _T("HKRR"))) {
188                         RegKeyPrint('6');
189                 } else {
190             dprintf("started with argc = %d, argv[1] = %s (unknown?)\n", argc, argv[1]);
191                 }
192
193         } else while (1) {
194         dprintf("choose test :\n");
195         dprintf("  0 = Exit\n");
196         dprintf("  1 = HKEY_CLASSES_ROOT\n");
197         dprintf("  2 = HKEY_CURRENT_USER\n");
198         dprintf("  3 = HKEY_LOCAL_MACHINE\n");
199         dprintf("  4 = HKEY_USERS\n");
200         dprintf("  5 = HKEY_CURRENT_CONFIG\n");
201         dprintf("  6 = REGISTRY ROOT\n");
202         ReadConsoleA(InputHandle, Buffer, 3, &Result, NULL) ;
203         switch (Buffer[0]) {
204         case '0':
205             return(0);
206         case '1':
207         case '2':
208         case '3':
209         case '4':
210         case '5':
211         case '6':
212                         RegKeyPrint(Buffer[0]/* - '0'*/);
213             break;
214                 default:
215                         dprintf("invalid input.\n");
216                         break;
217                 }
218         }
219     return 0;
220 }
221 /*
222 [HKEY_LOCAL_MACHINE]
223
224 [HKEY_LOCAL_MACHINE\HARDWARE]
225
226 [HKEY_LOCAL_MACHINE\HARDWARE\ACPI]
227
228 [HKEY_LOCAL_MACHINE\HARDWARE\ACPI\DSDT]
229
230 [HKEY_LOCAL_MACHINE\HARDWARE\ACPI\DSDT\VIA694]
231
232 [HKEY_LOCAL_MACHINE\HARDWARE\ACPI\DSDT\VIA694\AWRDACPI]
233
234  */