【问题标题】:Python _winreg key path incorrectPython _winreg 密钥路径不正确
【发布时间】:2015-05-17 13:27:43
【问题描述】:

当我尝试从该键读取值时,不会返回该键的正确值,而是得到不同的键路径值?

import _winreg as wreg
key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")
print(wreg.EnumValue(key, 0))

还有输出:

('SunJavaUpdateSched', u'"C:\\Program Files (x86)\\Common Files\\Java\\Java Update\\jusched.exe"', 1)

但是这个值不是我使用的密钥的一部分吗?这个值不在这个键上,我应该得到一个不同的值。 我搜索了 RegEdit 上不正确值的值所在的位置,它位于

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run

当我使用命令提示符时

REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

我得到了正确的输出......

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
IgfxTray    REG_SZ    "C:\Windows\system32\igfxtray.exe"
HotKeysCmds    REG_SZ    "C:\Windows\system32\hkcmd.exe"
Persistence    REG_SZ    "C:\Windows\system32\igfxpers.exe"

然后我会尝试在 python 上使用 os.popen...

import os
buff = os.popen("REG QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")
print(buff.read())

还有输出

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
SunJavaUpdateSched    REG_SZ    "C:\Program Files (x86)\Common Files\Java\Java Update\jusched.exe"

为什么这些不同?如何使用_winreg 获得正确的值?

【问题讨论】:

  • 可能与 Alternate Registry Views 有关,因为您在 64 位版本的操作系统上运行 32 位 Python。这在_winregdocumentation 中有所提及。
  • 你是如何设置这个值的?
  • @vks 这些键值是自动安装的,我只想找回它们。
  • 你可以试试wreg.OpenKeyEx
  • @vks 试过了,结果一样

标签: python registry winreg


【解决方案1】:

在 WOW64 上,32 位应用程序查看的注册表树与 64 位应用程序查看的注册表树不同。注册表反射在两个视图之间复制特定的注册表键和值。

您应该禁用注册表reflection

_winreg.DisableReflectionKey()
# Do stuff ...
# ...
# ...
_winreg.EnableReflectionKey()

【讨论】:

  • 我尝试将.DisableReflectionKey()key 的参数一起使用,但没有成功。我还用参数 '.HKEY_LOCAL_MACHINE 进行了尝试,但它也不起作用,它们都返回了 ('SunJavaUpdateSched', ...,1),这是我假设的 32 位值。
  • 尝试使用这两个参数后 _winreg.QueryReflectionKey(...) 会返回什么?
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多