[python-win32] Python and registry access with UAC enabled

Tim Roberts timr at probo.com
Tue Apr 26 19:52:34 CEST 2011


Michael ODonnell wrote:
> I have recently migrated to windows 7 from Windows XP. My organization enables UAC (level 3) at the group policy level and therefore I cannot disable it. I would like to know where there is a method to make registry changes using a python script without disabling UAC. For example, I can read registry hives and keys but I cannot write to them:

To be more specific, you cannot write to HKEY_LOCAL_MACHINE.  Most
scripts should be using HKEY_CURRENT_USER, which is writable.

> import _winreg
>
> # Successful read
> RegConn = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
> keyStr = r'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\1033'
> key = _winreg.OpenKey(RegConn, keyStr, 0, _winreg.KEY_READ | _winreg.KEY_WOW64_32KEY)
> print _winreg.QueryValueEx(key, "Version")[0]
>
> # Unsuccessful write
>
> x = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
> y = _winreg.OpenKey(x, r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", 0, _winreg.KEY_ALL_ACCESS)
> _winreg.SetValueEx(y, "path", 0, _winreg.REG_EXPAND_SZ,path + ";C:\\test")
> _winreg.CloseKey(y)
> _winreg.CloseKey(x)

You can certainly write to HKEY_CURRENT_USER\Environment.

Also, you don't really need KEY_ALL_ACCESS.  All you need is
KEY_SET_VALUE.  I don't think that will be allowed either, but it's
better to ask for only the permissions you need.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list