python - winreg.OpenKey throws filenotfound error for existing registry keys -
i facing difficulties in reading registry key created software. same code, able 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 registry key try: = 0 while 1: name, value, type = winreg.enumvalue(installdir, i) print (repr(name)) += 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))
traceback 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] system cannot find file specified
however windows reg query able retrieve value set.
#reg query hklm\software\mysoftware\myevent\is /v v2 hkey_local_machine\software\mysoftware\myevent\is v2 reg_dword 0x12
any highly appreciated
there 2 views of registry. there 32-bit registry view , 64-bit registry view. default , in cases, 32-bit applications see the 32-bit registry view , 64-bit applications see 64-bit registry view.
the other view can accessed using key_wow64_64key or key_wow64_32key access flags.
if running 32-bit python , key part of 64-bit registry view, should use open key:
winreg.openkey(winreg.hkey_local_machine, "software\\mysoftware\\myevent\\is", access=winreg.key_read | winreg.key_wow64_64key)
if running 64-bit python , key part of 32-bit registry view, should use open key:
winreg.openkey(winreg.hkey_local_machine, "software\\mysoftware\\myevent\\is", access=winreg.key_read | winreg.key_wow64_32key)
if know key part of same view, adding proper key_wow64_*
access flag ensure works no matter python architecture is.
in generic case, if have variable python architecture , not know in advance in view key be, can try finding key in current view , try other view next. 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: ''' not find key in both views. '''
for more information, check out accessing alternate registry view.
Comments
Post a Comment