Unknown Hero wrote:
So basically I am looking for these things:
1) Read one subkey from HKEY_LOCAL_MACHINE at a time (I think
QueryValueEx() is needed here)
2) Check if said subkey contains some predetermined string (like 'foo'
here)
3) If the above applies, change the value into another predetermined
string (like 'moo' here)
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
--
http://mail.python.org/mailman/listinfo/python-list