-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Am 2014-11-04 21:15, schrieb Jonathan Vollebregt:
+typedef struct +{
- HKEY key;
- const WCHAR *short_name;
- const WCHAR *long_name;
+} hkey_rel;
Style suggestion: you can make this an anonymous structure:
struct { HKEY key; const WCHAR *short_name; const WCHAR *long_name; } root_rels[] = { {HKEY_LOCAL_MACHINE, short_hklm, long_hklm}, {HKEY_CURRENT_USER, short_hkcu, long_hkcu}, {HKEY_CLASSES_ROOT, short_hkcr, long_hkcr}, {HKEY_USERS, short_hku, long_hku}, {HKEY_CURRENT_CONFIG, short_hkcc, long_hkcc}, };
Unless of course you need the structure definition elsewhere.
+static LSTATUS path_get_rootkey(const WCHAR *path, HKEY *out)
Since you're only returning two possible values (ERROR_SUCCESS or ERROR_BADKEY) you can make this return a HKEY *.
+static LSTATUS path_get_key(const WCHAR *path, HKEY *out) +{
- HKEY k;
- LONG err = path_get_rootkey(path, &k);
- if (err != ERROR_SUCCESS)
return err;
- path = strchrW(path, '\');
- if (path)
path++;
- err = RegOpenKeyW(k, path, &k);
- if (err != ERROR_SUCCESS)
return err;
- *out = k;
- return ERROR_SUCCESS;
}
What do you think about this:
static LSTATUS path_get_key(const WCHAR *path, HKEY *out) { *out = path_get_rootkey(path); path = strchrW(path, '\'); if (path) path++; return RegOpenKeyW(*out, path, out); }
If my understanding of RegOpenKeyW is right this will give you ERROR_INVALID_HANDLE (winnt+) or ERROR_BADKEY (win9x) if the root key is invalid, ERROR_PATH_NOT_FOUND if the path is invalid, and ERROR_SUCCESS otherwise. So pretty much the behavior of your code in 1/3rd of the lines of code.
I think path_open would be a better name for this function.
You could also consider an additional BOOL create parameter to call RegCreateKeyW instead of RegOpenKeyW so you can reuse this code in reg_add.
- err = path_get_key(key_name, &subkey);
- if (err != ERROR_SUCCESS) {
reg_message(STRING_INVALID_KEY);
return 1;
- }
- p++;
- root = get_rootkey(key_name);
- if (!root)
- {
reg_message(STRING_INVALID_KEY);
}reg_print_error(err); return 1;
@@ -306,21 +341,19 @@ static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty, /* Delete subtree only if no /v* option is given */ if (!value_name && !value_empty && !value_all) {
if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
HKEY root;
err = path_get_rootkey(key_name, &root);
err = RegDeleteTreeW(root, strchrW(key_name, '\\') + 1);
My reading comprehension may be lacking again, but can't you just call RegDeleteTree(subkey, NULL)? You're also assuming the caller isn't trying to nuke HKEY_LOCAL_MACHINE :-) .