winreg.OpenKey 为现有的注册表项抛出 filenotfound 错误 [英] winreg.OpenKey throws filenotfound error for existing registry keys

查看:131
本文介绍了winreg.OpenKey 为现有的注册表项抛出 filenotfound 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读取由我的软件创建的注册表项时遇到困难.但是,使用相同的代码,我可以读取其他键.

I am facing difficulties in reading a registry key created by my software. However with the same code, I am able to 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))

追溯展示

 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

但是 Windows reg 查询能够检索值集.

However Windows reg query is able to retrieve the value set.

#reg query HKLM\SOFTWARE\MySoftware\MyEvent\IS /v v2

HKEY_LOCAL_MACHINE\SOFTWARE\MySoftware\MyEvent\IS
v2    REG_DWORD    0x12

任何帮助将不胜感激

推荐答案

注册表有 2 个视图.有 32 位注册表视图和 64 位注册表视图.默认情况下,在大多数情况下,32 位应用程序只会看到 32 位注册表视图,而 64 位应用程序只会看到 64 位注册表视图.

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

可以使用 KEY_WOW64_64KEY 或 KEY_WOW64_32KEY 访问标志访问另一个视图.

The other view can be accessed by using the KEY_WOW64_64KEY or the KEY_WOW64_32KEY access flags.

如果您运行的是 32 位 python 并且您的密钥是 64 位注册表视图的一部分,您应该使用如下方式打开您的密钥:

If you are running 32-bit python and your key is part of the 64-bit registry view, 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)

如果您运行的是 64 位 python 并且您的密钥是 32 位注册表视图的一部分,您应该使用如下方式打开您的密钥:

If you are running 64-bit python and your key is part of the 32-bit registry view, 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)

如果您知道密钥始终是同一视图的一部分,则添加正确的 KEY_WOW64_* 访问标志将确保无论您的 Python 架构是什么,它都能正常工作.

If you know the key is always part of the same view, adding the proper KEY_WOW64_* access flag will ensure that it works no matter what your python architecture is.

在最通用的情况下,如果您有可变的 Python 架构并且您事先不知道键将在哪个视图中,您可以尝试在当前视图中查找键,然后尝试其他视图.它可能看起来像这样:

In the most generic case, if you have variable python architecture and you do not know in advance in which view the key will be, you can try finding the key in your current view and try the other view next. It could 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, check out Accessing an Alternate Registry View.

这篇关于winreg.OpenKey 为现有的注册表项抛出 filenotfound 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆