update registry test program.
[reactos.git] / reactos / apps / tests / regdump / regdump.c
1 /* $Id: regdump.c,v 1.2 2002/11/24 19:13:40 robd Exp $
2 *
3 * ReactOS regedit
4 *
5 * regdump.c
6 *
7 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <windows.h>
25 #include <tchar.h>
26 #include <stdio.h>
27 #include "regdump.h"
28
29
30 #define MAX_REGKEY_NAME_LEN 500
31 #define MAX_REG_DATA_SIZE 500
32 #define REG_FILE_HEX_LINE_LEN 76
33
34 #ifdef UNICODE
35 //#define dprintf _tprintf
36 #define dprintf printf
37 #else
38 #define dprintf printf
39 #endif
40
41 /*
42 char dprintf_buffer[1024];
43
44 void dprintf(char* fmt, ...)
45 {
46 int msg_len;
47 va_list args;
48
49 va_start(args, fmt);
50 vsprintf(dprintf_buffer, fmt, args);
51 //_vstprintf(dprintf_buffer, fmt, args);
52 msg_len = strlen(dprintf_buffer)
53 WriteConsoleA(OutputHandle, dprintf_buffer, msg_len, NULL, NULL);
54 va_end(args);
55 }
56 */
57 BOOL _DumpRegKey(TCHAR* KeyPath, HKEY hKey)
58 {
59 if (hKey != NULL) {
60 HKEY hNewKey;
61 LONG errCode;
62
63 //_tprintf(_T("_DumpRegKey() - Calling RegOpenKeyEx(%x)\n"), hKey);
64 //
65 // TODO: this is freezing ROS in bochs hard
66 errCode = RegOpenKeyEx(hKey, NULL, 0, KEY_READ, &hNewKey);
67 //
68 //errCode = RegOpenKeyExA(hKey, NULL, 0, KEY_READ, &hNewKey);
69
70 //_tprintf(_T("RegOpenKeyEx returned %x\n"), errCode);
71
72 if (errCode == ERROR_SUCCESS) {
73 TCHAR Name[MAX_REGKEY_NAME_LEN];
74 DWORD cName = MAX_REGKEY_NAME_LEN;
75 FILETIME LastWriteTime;
76 DWORD dwIndex = 0L;
77 TCHAR* pKeyName = &KeyPath[_tcslen(KeyPath)];
78
79 //_tprintf(_T("Calling RegEnumKeyEx(%x)\n"), hNewKey);
80
81 while (RegEnumKeyEx(hNewKey, dwIndex, Name, &cName, NULL, NULL, NULL, &LastWriteTime) == ERROR_SUCCESS) {
82 HKEY hSubKey;
83 DWORD dwCount = 0L;
84 //int len;
85 _tcscat(KeyPath, _T("\\"));
86 _tcscat(KeyPath, Name);
87 _tprintf(_T("[%s]\n"), KeyPath);
88 #if 1
89 //_tprintf(_T("Calling RegOpenKeyEx\n"));
90
91 errCode = RegOpenKeyEx(hNewKey, Name, 0, KEY_READ, &hSubKey);
92 if (errCode == ERROR_SUCCESS) {
93 #if 1
94 BYTE Data[MAX_REG_DATA_SIZE];
95 DWORD cbData = MAX_REG_DATA_SIZE;
96 TCHAR ValueName[MAX_REGKEY_NAME_LEN];
97 DWORD cValueName = MAX_REGKEY_NAME_LEN;
98 DWORD Type;
99
100 //_tprintf(_T("Calling RegEnumValue\n"));
101
102 while (RegEnumValue(hSubKey, dwCount, ValueName, &cValueName, NULL, &Type, Data, &cbData) == ERROR_SUCCESS) {
103 //dprintf("\t%S (%d) %d data bytes\n", ValueName, Type, cbData);
104 _tprintf(_T("\t%s = "), ValueName);
105 ////
106 switch (Type) {
107 case REG_EXPAND_SZ:
108 _tprintf(_T("expand:"));
109 case REG_SZ:
110 _tprintf(_T("\"%s\"\n"), Data);
111 break;
112 case REG_DWORD:
113 _tprintf(_T("dword:%08lx\n"), *((DWORD *)Data));
114 break;
115 default:
116 _tprintf(_T("warning - unsupported registry format '%ld', ") \
117 _T("treat as binary\n"), Type);
118 _tprintf(_T("key name: \"%s\"\n"), Name);
119 _tprintf(_T("value name:\"%s\"\n\n"), ValueName);
120 /* falls through */
121 case REG_MULTI_SZ:
122 /* falls through */
123 case REG_BINARY:
124 {
125 DWORD i1;
126 TCHAR *hex_prefix;
127 TCHAR buf[20];
128 int cur_pos;
129
130 if (Type == REG_BINARY) {
131 hex_prefix = _T("hex:");
132 } else {
133 hex_prefix = buf;
134 _stprintf(buf, _T("hex(%ld):"), Type);
135 }
136
137 /* position of where the next character will be printed */
138 /* NOTE: yes, _tcslen("hex:") is used even for hex(x): */
139 cur_pos = _tcslen(_T("\"\"=")) + _tcslen(_T("hex:")) + _tcslen(ValueName);
140
141 //dprintf(hex_prefix);
142 //_tprintf(hex_prefix);
143
144 for (i1 = 0; i1 < cbData; i1++) {
145 _tprintf(_T("%02x"), (unsigned int)(Data)[i1]);
146 if (i1 + 1 < cbData) {
147 _tprintf(_T(","));
148 }
149 cur_pos += 3;
150 /* wrap the line */
151 if (cur_pos > REG_FILE_HEX_LINE_LEN) {
152 _tprintf(_T("\\\n "));
153 cur_pos = 2;
154 }
155 }
156 _tprintf(_T("\n"));
157 break;
158 }
159 }
160 ////
161 cValueName = MAX_REGKEY_NAME_LEN;
162 cbData = MAX_REG_DATA_SIZE;
163 ++dwCount;
164 }
165 #endif
166 #if 1
167 _DumpRegKey(KeyPath, hSubKey);
168 #endif
169 RegCloseKey(hSubKey);
170 }
171 #endif
172 cName = MAX_REGKEY_NAME_LEN;
173 *pKeyName = _T('\0');
174 ++dwIndex;
175 }
176 RegCloseKey(hNewKey);
177 } else {
178 _tprintf(_T("_DumpRegKey(...) RegOpenKeyEx() failed.\n\n"));
179 }
180 } else {
181 _tprintf(_T("_DumpRegKey(...) - NULL key parameter passed.\n\n"));
182 }
183 return TRUE;
184 }
185
186 BOOL DumpRegKey(TCHAR* KeyPath, HKEY hKey)
187 {
188 _tprintf(_T("\n[%s]\n"), KeyPath);
189 //_tprintf(_T("Calling _DumpRegKey(..., %x))\n", hKey);
190 return _DumpRegKey(KeyPath, hKey);
191 }
192
193 void RegKeyPrint(int which)
194 {
195 TCHAR szKeyPath[1000];
196
197 switch (which) {
198 case '1':
199 _tcscpy(szKeyPath, _T("HKEY_CLASSES_ROOT"));
200 DumpRegKey(szKeyPath, HKEY_CLASSES_ROOT);
201 break;
202 case '2':
203 _tcscpy(szKeyPath, _T("HKEY_CURRENT_USER"));
204 DumpRegKey(szKeyPath, HKEY_CURRENT_USER);
205 break;
206 case '3':
207 _tcscpy(szKeyPath, _T("HKEY_LOCAL_MACHINE"));
208 DumpRegKey(szKeyPath, HKEY_LOCAL_MACHINE);
209 break;
210 case '4':
211 _tcscpy(szKeyPath, _T("HKEY_USERS"));
212 DumpRegKey(szKeyPath, HKEY_USERS);
213 break;
214 case '5':
215 _tcscpy(szKeyPath, _T("HKEY_CURRENT_CONFIG"));
216 DumpRegKey(szKeyPath, HKEY_CURRENT_CONFIG);
217 break;
218 case '6':
219 // DumpRegKey(szKeyPath, HKEY_CLASSES_ROOT);
220 // DumpRegKey(szKeyPath, HKEY_CURRENT_USER);
221 // DumpRegKey(szKeyPath, HKEY_LOCAL_MACHINE);
222 // DumpRegKey(szKeyPath, HKEY_USERS);
223 // DumpRegKey(szKeyPath, HKEY_CURRENT_CONFIG);
224 _tprintf(_T("unimplemented...\n"));
225 break;
226 }
227
228 }
229
230 const char* default_cmd_line1 = "/E HKLM_EXPORT.TXT HKEY_LOCAL_MACHINE";
231 const char* default_cmd_line2 = "TEST_IMPORT.TXT";
232 const char* default_cmd_line3 = "/D HKEY_LOCAL_MACHINE\\SYSTEM";
233 const char* default_cmd_line4 = "/D HKEY_LOCAL_MACHINE\\SOFTWARE";
234 const char* default_cmd_line5 = "/D HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes";
235 const char* default_cmd_line6 = "/E HKCR_EXPORT.TXT HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes";
236 const char* default_cmd_line7 = "/D HKEY_LOCAL_MACHINE\\SOFTWARE";
237 const char* default_cmd_line8 = "/D HKEY_LOCAL_MACHINE\\SOFTWARE";
238 const char* default_cmd_line9 = "/D HKEY_LOCAL_MACHINE\\SOFTWARE";
239
240 void show_menu(void)
241 {
242 _tprintf(_T("\nchoose test :\n"));
243 _tprintf(_T(" 0 = Exit\n"));
244 _tprintf(_T(" 1 = HKEY_CLASSES_ROOT\n"));
245 _tprintf(_T(" 2 = HKEY_CURRENT_USER\n"));
246 _tprintf(_T(" 3 = HKEY_LOCAL_MACHINE\n"));
247 _tprintf(_T(" 4 = HKEY_USERS\n"));
248 _tprintf(_T(" 5 = HKEY_CURRENT_CONFIG\n"));
249 _tprintf(_T(" 6 = REGISTRY ROOT\n"));
250 printf(" 7 = %s\n", default_cmd_line1);
251 printf(" 8 = %s\n", default_cmd_line2);
252 printf(" 9 = %s\n", default_cmd_line3);
253 printf(" A = %s\n", default_cmd_line4);
254 printf(" B = %s\n", default_cmd_line5);
255 printf(" C = %s\n", default_cmd_line6);
256 printf(" D = %s\n", default_cmd_line7);
257 printf(" E = %s\n", default_cmd_line8);
258 printf(" F = %s\n", default_cmd_line9);
259 /*
260 _tprintf(_T(" 7 = %s\n"), default_cmd_line1);
261 _tprintf(_T(" 8 = %s\n"), default_cmd_line2);
262 _tprintf(_T(" 9 = %s\n"), default_cmd_line3);
263 _tprintf(_T(" A = %s\n"), default_cmd_line4);
264 _tprintf(_T(" B = %s\n"), default_cmd_line5);
265 _tprintf(_T(" C = %s\n"), default_cmd_line6);
266 _tprintf(_T(" D = %s\n"), default_cmd_line7);
267 _tprintf(_T(" E = %s\n"), default_cmd_line8);
268 _tprintf(_T(" F = %s\n"), default_cmd_line9);
269 */
270 }
271
272 int regdump(int argc, char* argv[])
273 {
274 char Buffer[500];
275
276 if (argc > 1) {
277 // if (0 == _tcsstr(argv[1], _T("HKLM"))) {
278 if (strstr(argv[1], "help")) {
279
280 } else if (strstr(argv[1], "HKCR")) {
281 RegKeyPrint('1');
282 } else if (strstr(argv[1], "HKCU")) {
283 RegKeyPrint('2');
284 } else if (strstr(argv[1], "HKLM")) {
285 RegKeyPrint('3');
286 } else if (strstr(argv[1], "HKU")) {
287 RegKeyPrint('4');
288 } else if (strstr(argv[1], "HKCC")) {
289 RegKeyPrint('5');
290 } else if (strstr(argv[1], "HKRR")) {
291 RegKeyPrint('6');
292 } else {
293 dprintf("started with argc = %d, argv[1] = %s (unknown?)\n", argc, argv[1]);
294 }
295 return 0;
296 }
297 show_menu();
298 while (1) {
299 GetInput(Buffer, sizeof(Buffer));
300 switch (toupper(Buffer[0])) {
301 case '0':
302 return(0);
303 case '1':
304 case '2':
305 case '3':
306 case '4':
307 case '5':
308 case '6':
309 RegKeyPrint(Buffer[0]/* - '0'*/);
310 break;
311 case '7':
312 strcpy(Buffer, default_cmd_line1);
313 goto doit;
314 case '8':
315 strcpy(Buffer, default_cmd_line2);
316 goto doit;
317 case '9':
318 strcpy(Buffer, default_cmd_line3);
319 goto doit;
320 case 'A':
321 strcpy(Buffer, default_cmd_line4);
322 goto doit;
323 case 'B':
324 strcpy(Buffer, default_cmd_line5);
325 goto doit;
326 case 'C':
327 strcpy(Buffer, default_cmd_line6);
328 goto doit;
329 case 'D':
330 strcpy(Buffer, default_cmd_line7);
331 goto doit;
332 case 'E':
333 strcpy(Buffer, default_cmd_line8);
334 goto doit;
335 case 'F':
336 strcpy(Buffer, default_cmd_line9);
337 goto doit;
338 default: doit:
339 if (!ProcessCmdLine(Buffer)) {
340 dprintf("invalid input.\n");
341 show_menu();
342 } else {
343 dprintf("done.\n");
344 }
345 break;
346 }
347 }
348 return 0;
349 }