Scanning through Windows registry...

Tim Golden mail at timgolden.me.uk
Wed May 7 03:58:33 EDT 2008


Unknown Hero wrote:
> Tim Golden wrote:

[... snip long example ...]

> Correct me if I'm wrong (which I just might be), but doesn't the above
> code go through the keys behind HKEY_LOCAL_MACHINE\Software\Timsoft\ ?
> 
> Is it possible to use an empty value in:
> 
>   hTimSoft = _winreg.OpenKey (HKLM, r"Software\TimSoft")
> 
> like:
> 
>   hTimSoft = _winreg.OpenKey (HKLM, "")
> 
> so it would go all subkeys behind the "root" (in this case,
> HKEY_LOCAL_MACHINE)?

Yes. (Well, you can try it and find out for yourself!). As I said, this
was code to illustrate some other poster's difficulty. I assume that
you're capable of twisting it to suit your own purposes :)

> The code is supposed to work even if I don't know all possible subkeys
> under HKEY_LOCAL_MACHINE. Creating dozens or hundreds of handles like
> the above is a bit... unappealing.
> 
> Can I also use HKLM in the place of hTimSoft when I want to determine
> the amount of subkeys it has, like you did over here:

Yes. You can put any key handle you like in place of my
hTimSoft of the predefined _winreg.HKEY_LOCAL_MACHINE. They're
just keys. I assume you'll want some kind of recursive function which
combines calls to EnumKeys and EnumValues. 

> Anyway, the above code seems interesting. I'll try a few tweaks here
> and there and see what happens.

I wasn't sure just how experienced you were with programming; although you
said you were new to Python I thought you might have experience elsewhere
which would help you out here. If you're really stuck, post back and I (or someone
else) can try to knock up a more pertinent example. The main thing is for you
to get hold of the _winreg docs -- which I know you mentioned before -- and to
grasp what the concepts are and how the API calls work. It's a low-level module,
simply exposing the raw API calls, which is why there are several wrapper modules
around it, but it's worth getting to understand what's going on under the covers.

TJG




More information about the Python-list mailing list