Winreg.OpenKey throws filenotfound error for existing registry keys

I am having difficulty reading the registry key created by my software. However, with the same code, I can read other keys.

installdir = winreg.OpenKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MediaPlayer\\Player\\Extensions\\Types" ) #this works perfect #installdir1 = winreg.OpenKey( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS" ) #this gives Filenotfound error # list values owned by this registry key try: i = 0 while 1: name, value, type = winreg.EnumValue(installdir, i) print (repr(name)) i += 1 except WindowsError: print ("Bot donf") value, type = winreg.QueryValueEx(installdir, "10") print("user is", repr(value)) value, type = winreg.QueryValueEx(winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS"), "v2") print("user is", repr(value)) 

Tracking shows

  Traceback (most recent call last): File "D:/python_scripts/myclass.py", line 32, in <module> value, type = winreg.QueryValueEx(winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS"), "v2") FileNotFoundError: [WinError 2] The system cannot find the file specified 

However, a regressive Windows request may receive a given value.

 #reg query HKLM\SOFTWARE\MySoftware\MyEvent\IS /v v2 HKEY_LOCAL_MACHINE\SOFTWARE\MySoftware\MyEvent\IS v2 REG_DWORD 0x12 

Any help would be greatly appreciated

+5
source share
1 answer

There are 2 types of registry. There is a 32-bit registry representation and a 64-bit registry representation. By default, and in most cases, 32-bit applications will see only a 32-bit representation of the registry, and 64-bit applications will see only a 64-bit representation of the registry.

You can access another view using the access flags KEY_WOW64_64KEY or KEY_WOW64_32KEY.

If you use 32-bit python and your key is part of the 64-bit registry, you should use something like this to open your key:

 winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS", access=winreg.KEY_READ | winreg.KEY_WOW64_64KEY) 

If you are using 64-bit python, and your key is part of the 32-bit registry, you should use something like this to open your key:

 winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS", access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY) 

If you know that the key is always part of the same view, adding the correct KEY_WOW64_* access flag ensures that it works regardless of your python architecture.

In the most general case, if you have a python architecture variable and you don’t know in advance what representation the key will be in, you can try to find the key in your current view and try a different view. It might look something like this:

 try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS") except FileNotFoundError: import platform bitness = platform.architecture()[0] if bitness == '32bit': other_view_flag = winreg.KEY_WOW64_64KEY elif bitness == '64bit': other_view_flag = winreg.KEY_WOW64_32KEY try: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\MySoftware\\MyEvent\\IS", access=winreg.KEY_READ | other_view_flag) except FileNotFoundError: ''' We really could not find the key in both views. ''' 

For more information, visit Access to an alternate registry view .

+8
source

All Articles