Scanning through Windows registry...

Unknown Hero unknown_hero007 at hotmail.com
Wed May 7 01:31:49 EDT 2008


Tim Golden wrote:

> Well, I attach a kind of explanatory Noddy example I wrote a few years ago
> for someone on the python-win32 list. I think, glancing over it, that it includes
> what you need to know, although not necessarily in the right order. I'm happy to
> explain if things aren't clear:
>
> <code>
> import _winreg
>
> HKLM = _winreg.HKEY_LOCAL_MACHINE
>
> #
> # Set up a registry subtree under HKLM\Software
> # which will look like this:
> #
>
> #
> # TimSoft
> # |
> # +-- App1
> # |
> # +-- App2
> # |
> # +-- App3
> #
>
> #
> # The [TimSoft] key has a default (ie unnamed) value
> # while the Appx keys each have two values:
> #   [Registered] - a string Y/N value
> #   [Version] - a DWORD value
> #
>
> hSoftware = _winreg.OpenKey (HKLM, "Software")
> hTimSoft = _winreg.CreateKey (hSoftware, "TimSoft")
>
> _winreg.SetValueEx (hTimSoft, None, 0, _winreg.REG_SZ, "All Tim's Software")
>
> hApp1 = _winreg.CreateKey (hTimSoft, "App1")
> _winreg.SetValueEx (hApp1, "Version", 0, _winreg.REG_DWORD, 101)
> _winreg.SetValueEx (hApp1, "Registered", 0, _winreg.REG_SZ, "Y")
>
> hApp2 = _winreg.CreateKey (hTimSoft, "App2")
> _winreg.SetValueEx (hApp2, "Version", 0, _winreg.REG_DWORD, 202)
> _winreg.SetValueEx (hApp2, "Registered", 0, _winreg.REG_SZ, "N")
>
> hApp3 = _winreg.CreateKey (hTimSoft, "App3")
> _winreg.SetValueEx (hApp3, "Version", 0, _winreg.REG_DWORD, 303)
> _winreg.SetValueEx (hApp3, "Registered", 0, _winreg.REG_SZ, "Y")
>
> #
> # NB - no need to do an explicit "write": the Registry uses
> #  some sort of caching which eventually catches up with itself,
> #  so unless you plan to turn the machine off soon, don't
> #  bother with FlushKey or anything like that.
> #
>
> #
> # Now we start again, as though we were just querying
> #
>
> hTimSoft = _winreg.OpenKey (HKLM, r"Software\TimSoft")
> n_sub_keys, n_values, last_modified = _winreg.QueryInfoKey (hTimSoft)
> print n_sub_keys, "sub keys", n_values, "values", last_modified, "nanoseconds since 1600!"
>
> #
> # Pick up the default value: really should try to
> #  interpret the default_type to determine if it's
> #  a number or a string or whatever, but...
> #
> default_value, default_type = _winreg.QueryValueEx (hTimSoft, None)
> print "Default value:", default_value
>
> #
> # Now, in this case I know (because I created them) that
> #  the TimSoft key has three subkeys, each of which has
> #  two values. But if I didn't...
> #
> print
> for i in range (n_sub_keys):
>   subkey_name = _winreg.EnumKey (hTimSoft, i)
>   print subkey_name
>
> #
> # Alternatively, if I hadn't done the QueryInfoKey above...
> #
> i = 0
> print
> while 1:
>   try:
>     subkey_name = _winreg.EnumKey (hTimSoft, i)
>   except EnvironmentError:
>     break
>   else:
>     print subkey_name
>     i += 1
>
> #
> # Now, let's use the last key as an example
> #  and pick out its values.
> #
> print
> print subkey_name
> hAppKey = _winreg.OpenKey (hTimSoft, subkey_name)
> i = 0
> while 1:
>   try:
>     name, value, type = _winreg.EnumValue (hAppKey, i)
>     print name, value, type
>   except EnvironmentError:
>     break
>   else:
>     print "  %s => %s (type %s)" % (name, value, type)
>     i += 1
>
> </code>
>
> TJG

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)?


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:

  n_sub_keys, n_values, last_modified = _winreg.QueryInfoKey
(hTimSoft)


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

Thank you, Tim.



More information about the Python-list mailing list