Skip to content

Commit

Permalink
win32/uniregistrygen.cc: Open keys with READ if we can't READ/WRITE.
Browse files Browse the repository at this point in the history
Windows Vista tries to trip us up again, by giving us permission denied
to open HKEY_LOCAL_MACHINE/Software (and probably other keys) with
KEYS_READ | KEYS_WRITE permissions.  But, it works with KEYS_READ.

Rework the iterative subkey opening strategy to try
KEYS_READ | KEYS_WRITE, and fall back to KEYS_READ if this fails.
  • Loading branch information
lkosewsk committed Sep 29, 2010
1 parent cbff382 commit c2d06f6
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions win32/uniregistrygen.cc
Expand Up @@ -11,6 +11,15 @@
WV_LINK(UniRegistryGen);


static LONG openregkey(HKEY hLastKey, WvStringParm subkey,
REGSAM samDesired, HKEY *phNextKey, bool create)
{
if (create)
return RegCreateKeyEx(hLastKey, subkey, 0, NULL, 0, samDesired,
NULL, phNextKey, NULL);
return RegOpenKeyEx(hLastKey, subkey, 0, samDesired, phNextKey);
}

// returns a handle to the key specified by key, or, if key specifies a value,
// a handle to the key containing that value (and setting isValue = true)
static HKEY follow_path(HKEY from, const UniConfKey &key,
Expand All @@ -27,16 +36,15 @@ static HKEY follow_path(HKEY from, const UniConfKey &key,
{
WvString subkey = key.segment(i).printable();
HKEY hNextKey;

if (create)
{
result = RegCreateKeyEx(hLastKey, subkey, 0, NULL, 0, samDesired,
NULL, &hNextKey, NULL);
}
else
{
result = RegOpenKeyEx(hLastKey, subkey, 0, samDesired, &hNextKey);
}

result = openregkey(hLastKey, subkey, samDesired, &hNextKey, create);
// In Windows Vista, you can't seemingly get KEY_WRITE permissions on
// HKEY_LOCAL_MACHINE/Software (and likely others) if running from
// within certain processes (presumably ones that set some magic
// incantation). We don't care, of course, since we don't intend to
// write this key, and this wonderful fix fixes all.
if (result == ERROR_ACCESS_DENIED)
result = openregkey(hLastKey, subkey, KEY_READ, &hNextKey, create);

if ((result == ERROR_FILE_NOT_FOUND) && (i == n-1))
{
Expand Down

0 comments on commit c2d06f6

Please sign in to comment.