Winreg

R Pasco pascor22234 at gmail.com
Thu Jul 30 15:14:12 EDT 2020


 I can't find instructions for the proper way to reply to 'python list'. Is
it simply a matter of keeping the message title identical to the original
message and emailing again to python-list at python.org ? I'll reply both ways
to test this.

Yes, it's the 64/32-bit views that got me stuck. I think I've got it mostly
figured out. The key HKEY_LOCAL_MACHINE:SOFTWARE requires KEY_WOW64_64KEY
to be 'OR'ed. Winreg gives no indication of this requirement, but the
Windows doc "Redirected, Shared, and Reflected Keys Under WOW64" does
specifically mention  HKEY_CURRENT_USER :SOFTWARE as needing this added
access.

By the way, I'm running python X64, so the program is a "64 bit"
application. Strangely, the KEY_WOW64_32KEY access rights param also works
identically.

To sum it up the pertinent code, so far, is:
======================================
    # Be able to test creating both WOW64 and non-WOW64 key paths.
    if 1 :
        hive = winreg.HKEY_LOCAL_MACHINE
        hivename = 'HKEY_LOCAL_MACHINE'
    else :
        hive = winreg.HKEY_CURRENT_USER
        hivename = 'HKEY_CURRENT_USER'
    #
    parent_keypath = 'Software'   # Both hives happen to have this top
level key.
    newsubkey = 'AAA New Leaf Key, Process PID={}' .format( os.getpid() )

    # Note keys created in HKEY_LOCAL_MACHINE::SOFTWARE will be viewed in
RegEdit
    #   at HKEY_LOCAL_MACHINE:SOFTWARE\*Wow6432Node*\{newsubkey}.
CreateKeyEx
    #   access rights must be set to [winreg.KEY_ALL_ACCESS |
winreg.KEY_WOW64_64KEY]
    #     -OR-
    #   [winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_32KEY] (??? Why does this
also work ?)
    #
    newsubkeyPath = os.path.join( parent_keypath, newsubkey )

    valuename = 'NewString'
    value = 'New String Value'

    with winreg.ConnectRegistry( None, hive ) as hivehndl :

        with winreg.OpenKeyEx( hivehndl, parent_keypath ) as
parentpath_hndl :

            with winreg.CreateKeyEx( parentpath_hndl, newsubkey, 0,
                # winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_64KEY ) as
newkeyhndl :
                winreg.KEY_ALL_ACCESS | winreg.KEY_WOW64_32KEY ) as
newkeyhndl :

                winreg.SetValueEx( newkeyhndl, valuename, 0, winreg.REG_SZ,
value )

    #end
======================================

Thanks for all the help.
Ray Pasco


More information about the Python-list mailing list