我正在尝试获取所有已安装的Windows计算机程序,因此我读出了注册表。
但不知何故python两次读取32位程序(即使我给他另一个注册表项)
以下是代码剪辑:
def get_programs(registry):
reg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
programList = []
key = OpenKey(reg, registry)
print(QueryInfoKey(key))
for i in range(0, QueryInfoKey(key)[0]):
programList.append(EnumKey(key, i))
CloseKey(key)
CloseKey(reg)
return programList
我这样称呼这个函数:
registry32bit = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
registry64bit = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
programs32bit = get_programs(registry32bit)
programs64bit = get_programs(registry64bit)
为什么python打开并读出相同的注册表(32位)两次并返回完全相同的列表?
答案 0 :(得分:1)
这似乎有用,并在下面的评论中使用@eryksun建议,只是让重定向发生而不是显式引用Wow6432Node
注册表项。中心思想是在打开卸载子项时指定KEY_WOW64_32KEY
或KEY_WOW64_64KEY
标志,让魔法发生。
注意:我还 Pythonized get_programs()
函数中的代码。在我看来,这使它更短,更具可读性。
import sys
from _winreg import *
# Assure registry handle objects with context manager protocol implemented.
if sys.version_info.major*10 + sys.version_info.minor < 26:
raise AssertionError('At least Python 2.6 is required.')
def get_programs(subkey, regBitView):
with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as hive:
with OpenKey(hive, subkey, 0, regBitView | KEY_READ) as key:
return [EnumKey(key, i) for i in range(QueryInfoKey(key)[0])]
UNINSTALL_REG_KEY = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
programs32bit = get_programs(UNINSTALL_REG_KEY, KEY_WOW64_32KEY)
programs64bit = get_programs(UNINSTALL_REG_KEY, KEY_WOW64_64KEY)
print('32-bit programs:\n{}'.format(programs32bit))
print('')
print('64-bit programs:\n{}'.format(programs64bit))
非常感谢@eryksun提供的线索和许多实施策略建议。