from _winreg import * """print r"*** Reading from SOFTWARE\Microsoft\Windows\CurrentVersion\Run ***" """ aReg = ConnectRegistry(None,HKEY_LOCAL_MACHINE) aKey = OpenKey(aReg, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") for i in range(1024): try: asubkey=EnumKey(aKey,i) val=QueryValueEx(asubkey, "DisplayName") print val except EnvironmentError: break
Could anyone please correct the error...i just want to display the "DisplayName" within the subkeys of the key the HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall This is the error i get..
Traceback (most recent call last): File "C:/Python25/ReadRegistry", line 10, in <module> val=QueryValueEx(asubkey, "DisplayName") TypeError: The object is not a PyHKEY object
We need to import the module named winreg into the python environment. In the below example we use the winreg module to first connect to the registry using the ConnectRegistry function and then access the registry using OpenKey function. Finally we design a for loop to print the result of the keys accessed.
Registry Editor isn't a program you download. Instead, it can be accessed by executing regedit from the Command Prompt or from the search or Run box from the Start menu.
Documentation says that EnumKey
returns string with key's name. You have to explicitly open it with _winreg.OpenKey
function. I've fixed your code snippet:
from _winreg import * aKey = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE) print(r"*** Reading from %s ***" % aKey) aKey = OpenKey(aReg, aKey) for i in range(1024): try: asubkey_name = EnumKey(aKey, i) asubkey = OpenKey(aKey, asubkey_name) val = QueryValueEx(asubkey, "DisplayName") print(val) except EnvironmentError: break
Please note, that not every key has "DisplayName" value available.
What about x86 on x64? Use 64-bit Specific Types
What if there's more than 1024 sub-keys in "Uninstall"? Use _winreg.QueryInfoKey(key)
Python 2:
import errno, os, _winreg proc_arch = os.environ['PROCESSOR_ARCHITECTURE'].lower() proc_arch64 = os.environ['PROCESSOR_ARCHITEW6432'].lower() if proc_arch == 'x86' and not proc_arch64: arch_keys = {0} elif proc_arch == 'x86' or proc_arch == 'amd64': arch_keys = {_winreg.KEY_WOW64_32KEY, _winreg.KEY_WOW64_64KEY} else: raise Exception("Unhandled arch: %s" % proc_arch) for arch_key in arch_keys: key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, _winreg.KEY_READ | arch_key) for i in xrange(0, _winreg.QueryInfoKey(key)[0]): skey_name = _winreg.EnumKey(key, i) skey = _winreg.OpenKey(key, skey_name) try: print _winreg.QueryValueEx(skey, 'DisplayName')[0] except OSError as e: if e.errno == errno.ENOENT: # DisplayName doesn't exist in this skey pass finally: skey.Close()
Python 3:
import errno, os, winreg proc_arch = os.environ['PROCESSOR_ARCHITECTURE'].lower() proc_arch64 = os.environ['PROCESSOR_ARCHITEW6432'].lower() if proc_arch == 'x86' and not proc_arch64: arch_keys = {0} elif proc_arch == 'x86' or proc_arch == 'amd64': arch_keys = {winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY} else: raise Exception("Unhandled arch: %s" % proc_arch) for arch_key in arch_keys: key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, winreg.KEY_READ | arch_key) for i in range(0, winreg.QueryInfoKey(key)[0]): skey_name = winreg.EnumKey(key, i) skey = winreg.OpenKey(key, skey_name) try: print(winreg.QueryValueEx(skey, 'DisplayName')[0]) except OSError as e: if e.errno == errno.ENOENT: # DisplayName doesn't exist in this skey pass finally: skey.Close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With