-----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 :-) .
-----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQIcBAEBAgAGBQJUWUmSAAoJEN0/YqbEcdMwjmQP/iuYzlfloARtQOxq6zCyHAsT bBFIHVwZoiB3/e4UvDcnRvcdJGvTM/LNu2LBczsFhOEfidSGsMYbbPAm2bEP5cJE FAcy2bMTpiaPJx957dbimhEQuSreRx1OOdzH6jK74g+d/WJ3fHy0RHh9QXY0Rpe+ JLsPDOpau2gDs6HoKNvF9yVPM32sa/BWJGtLqGFhQngmzIye9WmBEVMX+c6w9q9N gxc7uiDBYXMSi4D2rGE5HwQJq3AM6InTOKkyAkDZv6EObvUeZ0JQKtFlC6tW3z0x aXTzs9RjvzS2er5ULPyxYV3QxBynRzI80VoWq5p3Z4M34dIaSsJeuWlhTWiIktzl U+M7G1Ukk3owmmXtYC0nBOIBq2IEzRzL28nXwbz3h8EUp1kYzwsZ9VpP6NitOP+6 E2yywBEDIrJL22RNSt79GLKlnLKpBNitWq5xCoWdXE625pBDSEH4cKlE5Afwi7j7 /jNGjxRv4Rre6+PcgrPLq3JSaEDHAs+tKuBvkJWgCKCc45KkaGiiwS5Tp32odqdA Q75F+8Epolx00aA948PwJ827VMtMT6U6z79ypluI5tgHuldhS73pmg9PvLtVfYG6 TlexSFUC1eq0FJWwS8yxY2HO20ZbQpOjpmrxR0mu2ACcFfdH8Ut6dspB5g2ttrGU XgXrPCumoV8HxXzwDcMu =B2eH -----END PGP SIGNATURE-----