Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python _winreg woes

Tags:

python

winreg

I'm trying to access the windows registry (in Python) to query a key value using _winreg and I can't get it to work. The following line returns a WindowsError saying that the "system cannot find the specified file":

key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Autodesk\Maya\2012\Setup\InstallPath', 0, _winreg.KEY_ALL_ACCESS)

After hours of trying, it looks like Python cannot see beyond the "Maya" part of the path (it looks like the "2012\...etc..." sub-path is "invisible" or non-existent). Now I have the Registry Editor open and I guaranty there IS such a path in the HKLM. I'm on Windows 7 64bit. Any idea what I'm doing wrong? This is driving me nuts. Thanks...

like image 378
user1219144 Avatar asked Feb 19 '12 12:02

user1219144


2 Answers

You need to combine the access key with one of the 64bit access keys.

_winreg.KEY_WOW64_64KEY Indicates that an application on 64-bit Windows should operate on the 64-bit registry view.

_winreg.KEY_WOW64_32KEY Indicates that an application on 64-bit Windows should operate on the 32-bit registry view.

Try:

_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Autodesk\Maya\2012\Setup\InstallPath', 0, (_winreg.KEY_WOW64_64KEY + _winreg.KEY_ALL_ACCESS))
like image 54
Denis Avatar answered Nov 11 '22 13:11

Denis


Are you also using a 64-bit version of Python, or is it a 32-bit Python? (The latter is more common.) If you're using a 32-bit version of Python, the _winreg module will see the 32-bit registry by default, while regedit will show you the 64-bit one. You should be able to tell _winreg to open a different view; see the _winreg module docs on access rights, specifically the subsection on 64-bit specific flags and the MSDN article it references. Unfortunately it doesn't look like there's a way for a 32-bit process to access the 64-bit registry, but I may be missing something.

like image 30
Thomas Wouters Avatar answered Nov 11 '22 14:11

Thomas Wouters