Scanning through Windows registry...
Mike Driscoll
kyosohma at gmail.com
Wed May 7 10:47:49 EDT 2008
On May 7, 9:19 am, Tim Golden <m... at timgolden.me.uk> wrote:
> Mike Driscoll wrote:
> > On May 7, 4:45 am, Tim Golden <m... at timgolden.me.uk> wrote:
> >> In a spirit of being helpful... :)
>
> [... snip registry walker ...]
>
> > This is pretty cool stuff, Tim. Of course, it would also seriously
> > screw up some programs if you decided to replace the wrong phrase.
> > Just a word of warning to the OP: be sure to make a backup of the
> > registry before doing something like this. Trying to fix a messed up
> > registry from the command line is not a fun way to spend the
> > afternoon.
>
> Good point, Mike. In a spirit of being even more helpful... the _winreg
> module includes the functions SaveKey and LoadKey so as a crude
> backup mechanism, you could do this:
>
> <code snippet>
> import time
> import _winreg
> import win32api
> import win32security
>
> #
> # You need to have SeBackupPrivilege enabled for this to work
> #
> priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
> hToken = win32security.OpenProcessToken (win32api.GetCurrentProcess (), priv_flags)
> privilege_id = win32security.LookupPrivilegeValue (None, "SeBackupPrivilege")
> win32security.AdjustTokenPrivileges (hToken, 0, [(privilege_id, win32security.SE_PRIVILEGE_ENABLED)])
>
> root = _winreg.OpenKey (_winreg.HKEY_LOCAL_MACHINE, r"Software\Python\TEST")
> #
> # This will fail if the file already exists so make it uniqueish
> #
> _winreg.SaveKey (root, "c:/temp/HKLM_Software_Python-%s.reg" % time.strftime ("%Y%m%d-%H%M%S"))
>
> </code snippet>
>
> Note that the file which this creates is *not* the same as a .reg file which the File > Export menu
> option provides from within the registry editor. (In fact, I shouldn't really have called it .reg; call
> it something else). It can be reloaded by the _winreg.LoadKey function -- which I notice is incorrectly
> named RegLoadKey in the docs after the underlying API call. The docs note that SeRestorePrivilege is
> required for this action, the counterpart to SeBackupPrivilege.
>
> TJG
Wow! Talk about going above and beyond the call of tutoring! I still
don't know how you found out how to do all these neat Windows tricks,
but I'll be filing them away for future reference.
Mike
More information about the Python-list
mailing list