Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Kerpal
Jul 20, 2003

Well that's weird.
Does anyone have experience with the _winreg module? I'm trying to write a script that deletes a registry key in HKLM\Software\Classes\Installer\Products, however the _winreg DeleteKey method cannot delete keys with subkeys, which gives a WindowsError exception "Access is denied" error 5. The only solutions I could think of was to create a function that recursively deletes every subkey in the input key before deleting the primary key. I'm trying to do this remotely, so I have to be able to connect to a remote registry. Another solution was to call the reg command like:

reg delete "\\\\%s1\\HKLM\Software\Classes\Installer\Products\%s2"

Where %s1 is the computer and %s2 is the product key I'm trying to delete. Running this command produces a different error, "The procedure number is out of range." Running the same command on the local machine worked fine.

Adbot
ADBOT LOVES YOU

Kerpal
Jul 20, 2003

Well that's weird.
Sweet, thanks Gripper! That works exactly like I wanted it to. The only thing I changed was the key_str argument, instead I'm using:

Python code:
HKLM = _winreg.HKEY_LOCAL_MACHINE
key_str = _winreg.ConnectRegistry(r'\\computer_name', HKLM)
to connect to the remote machine. The code traversed all the keys and removed them, exactly like I need. Your enumerate keys function looked very similar to what I was trying, which started as something like:

Python code:
def delete_subkeys(key, delete_list):
    for i in delete_list:
        product_key = i[0]
        c = 0
        skey = _winreg.OpenKey(key, product_key)
        subkeys = []
        while True:
            try:
                sub_key = _winreg.EnumKey(skey, c)
                s = '%s\\%s' % (product_key, sub_key)
                subkeys.append(s)
                c += 1
            except WindowsError:
                break
I'm trying to understand how you're traversing each key, because to me that's the real trick here. I guess it works because you're using enum_keys as a recursive function to traverse each subkey until the WindowsError exception is caught? Thanks for the help.

Kerpal fucked around with this message at 21:13 on Oct 4, 2012

  • Locked thread