两次打开同一个注册表? [英] Opens same registry twice?
问题描述
我正在尝试获取我的 Windows 计算机上所有已安装的程序,因此我读取了注册表.
I am trying to get all installed programs of my windows computer, therefore I read out the registry.
但不知何故,python 将 32 位程序读取了两次(即使我给了他另一个注册表项)
But somehow python reads the 32bit programs out twice (even though I give him another registry entry)
代码如下:
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位)两次并返回完全相同的列表?
Why does python open and read out the same registry (for 32 bit) twice and return the exactly same list?
推荐答案
这似乎有效,并在下面的评论中使用了 @eryksun 建议,关于只让重定向发生而不显式引用 Wow6432Node
注册表钥匙.中心思想是在打开卸载子项时只指定 KEY_WOW64_32KEY
或 KEY_WOW64_64KEY
标志并让魔法发生.
This appears to work and uses @eryksun suggestion in a comment below about just letting the redirection happen and not explicitly referencing the Wow6432Node
registry key. The central idea is to just specify either the KEY_WOW64_32KEY
or KEY_WOW64_64KEY
flag when opening the uninstall subkey and let the magic happen.
注意:我还Python化了get_programs()
函数中的代码.在我看来,这使它更短、更易读.
Note: I also Pythonized the code in the get_programs()
function some. This made it shorter and more readable in my opinion.
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 提供的线索和许多实施策略建议.
Many thanks to @eryksun for the clues and many implementation strategy suggestions.
这篇关于两次打开同一个注册表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!