Skip to content

Commit

Permalink
reg: Recursively export all subkeys and data.
Browse files Browse the repository at this point in the history
Signed-off-by: Hugh McMaster <[email protected]>
Signed-off-by: Alexandre Julliard <[email protected]>
  • Loading branch information
hughmcmaster authored and julliard committed Dec 4, 2017
1 parent 4918010 commit 72db61d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
33 changes: 31 additions & 2 deletions programs/reg/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,18 @@ static void export_key_name(HANDLE hFile, WCHAR *name)
heap_free(buf);
}

#define MAX_SUBKEY_LEN 257

static int export_registry_data(HANDLE hFile, HKEY key, WCHAR *path)
{
LONG rc;
DWORD max_value_len = 256, value_len;
DWORD max_data_bytes = 2048, data_size;
DWORD i, type;
WCHAR *value_name;
DWORD subkey_len;
DWORD i, type, path_len;
WCHAR *value_name, *subkey_name, *subkey_path;
BYTE *data;
HKEY subkey;

export_key_name(hFile, path);

Expand Down Expand Up @@ -280,6 +284,31 @@ static int export_registry_data(HANDLE hFile, HKEY key, WCHAR *path)

heap_free(data);
heap_free(value_name);

subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR));

path_len = lstrlenW(path);

i = 0;
for (;;)
{
subkey_len = MAX_SUBKEY_LEN;
rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL);
if (rc == ERROR_SUCCESS)
{
subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len);
if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey))
{
export_registry_data(hFile, subkey, subkey_path);
RegCloseKey(subkey);
}
heap_free(subkey_path);
i++;
}
else break;
}

heap_free(subkey_name);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion programs/reg/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ static void output_value(const WCHAR *value_name, DWORD type, BYTE *data, DWORD
output_string(newlineW);
}

static WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len)
{
WCHAR *subkey_path;
static const WCHAR fmt[] = {'%','s','\\','%','s',0};
Expand Down
1 change: 1 addition & 0 deletions programs/reg/reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void output_writeconsole(const WCHAR *str, DWORD wlen);
void WINAPIV output_message(unsigned int id, ...);
BOOL ask_confirm(unsigned int msgid, WCHAR *reg_info);
HKEY path_get_rootkey(const WCHAR *path);
WCHAR *build_subkey_path(WCHAR *path, DWORD path_len, WCHAR *subkey_name, DWORD subkey_len);
BOOL parse_registry_key(const WCHAR *key, HKEY *root, WCHAR **path, WCHAR **long_key);

/* import.c */
Expand Down
4 changes: 2 additions & 2 deletions programs/reg/tests/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -4489,7 +4489,7 @@ static void test_export(void)

run_reg_exe("reg export HKEY_CURRENT_USER\\" KEY_BASE " file.reg", &r);
ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r);
ok(compare_export("file.reg", complex_test, TODO_REG_COMPARE), "compare_export() failed\n");
ok(compare_export("file.reg", complex_test, 0), "compare_export() failed\n");

err = delete_tree(HKEY_CURRENT_USER, KEY_BASE);
ok(err == ERROR_SUCCESS, "delete_tree() failed: %d\n", err);
Expand All @@ -4503,7 +4503,7 @@ static void test_export(void)

run_reg_exe("reg export HKEY_CURRENT_USER\\" KEY_BASE " file.reg", &r);
ok(r == REG_EXIT_SUCCESS, "got exit code %d, expected 0\n", r);
ok(compare_export("file.reg", key_order_test, TODO_REG_COMPARE), "compare_export() failed\n");
ok(compare_export("file.reg", key_order_test, 0), "compare_export() failed\n");

delete_key(hkey, "Subkey1");
delete_key(hkey, "Subkey2");
Expand Down

0 comments on commit 72db61d

Please sign in to comment.