Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';找不到Windows注册表项,但它';它在regedit中可见_Python_Python 3.x_Windows_Registry - Fatal编程技术网

Python Can';找不到Windows注册表项,但它';它在regedit中可见

Python Can';找不到Windows注册表项,但它';它在regedit中可见,python,python-3.x,windows,registry,Python,Python 3.x,Windows,Registry,我正在搜索特定发布者的卸载字符串,以自动卸载某些软件。我可以在regedit中看到条目。我正在搜索64位和32位注册表视图,这可以在我的代码中看到。我可以在“添加或删除程序”中查看应用程序。该软件是由环境研究系统研究所(Environmental Research Systems Institute,Inc.)发布的。我已经到处搜索了,不明白为什么我的代码找不到这把钥匙。任何帮助都将不胜感激。在函数getUninstallers中,在第一个try块中注释掉了一条if语句,该语句具有我试图查找的实

我正在搜索特定发布者的卸载字符串,以自动卸载某些软件。我可以在regedit中看到条目。我正在搜索64位和32位注册表视图,这可以在我的代码中看到。我可以在“添加或删除程序”中查看应用程序。该软件是由环境研究系统研究所(Environmental Research Systems Institute,Inc.)发布的。我已经到处搜索了,不明白为什么我的代码找不到这把钥匙。任何帮助都将不胜感激。在函数
getUninstallers
中,在第一个try块中注释掉了一条if语句,该语句具有我试图查找的实际密钥名。在代码下面,我包含了一个regedit的屏幕截图,其中键可见

import winreg 
aKeyName = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
bKeyName = "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

def getUninstallers(keyName, bitView):
    if ( bitView == 32 ):
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keyName, 0,
                (winreg.KEY_WOW64_32KEY + winreg.KEY_READ))
    elif ( bitView == 64 ):
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keyName, 0,
                (winreg.KEY_WOW64_64KEY + winreg.KEY_READ))
    subKeyIndex = 0
    subkeys = list()
    while True:
        try:
            subKeyName = winreg.EnumKey(key,subKeyIndex)
            #if(subKeyName.count(r'{D43E583A-A698-4227-8F83-3084172F12F0}') > 0):
            #    print(subKeyName)
            subKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keyName +
                        "\\" + subKeyName)
            valueIndex = 0
            subKeyList = list()
            while True:
                try:
                    aValue = winreg.EnumValue(subKey,valueIndex)
                    if(aValue[0] == 'Publisher' or 
                       aValue[0] == 'UninstallString'):
                        subKeyList.append(aValue)
                    valueIndex+=1
                except OSError:#[WinError 259] No more data is available
                    #These errors are supposed to happen, see winreg 
                    #documentation at docs.python.org/3/library/winreg.html
                    break
            if(len(subKeyList) > 0):
                subkeys.append(subKeyList)
            subKeyIndex += 1
        except OSError:#[WinError 259] No more data is available
            #These errors are supposed to happen, see winreg documentation
            #at docs.python.org/3/library/winreg.html
            break
    return subkeys

def getUninstallersByPublisher(publisher):
    theKeys = [getUninstallers(aKeyName, 32), 
               getUninstallers(bKeyName, 32), 
               getUninstallers(aKeyName, 64), 
               getUninstallers(bKeyName, 64)]
    uninstallers = list()
    for aKey in theKeys:
        for subKey in aKey:
            for aValue in subKey:
                for aString in aValue:
                    if(str(aString).count(publisher) > 0):
                        print("Found key for publisher: " + publisher)
                        for someValue in subKey:
                            for someString in someValue:
                                if(str(someString).count("Uninstall") > 0):
                                    try:
                                        print("Found UninstallString: " +
                                            someValue[1])
                                        uninstallers.append(someValue[1])
                                    except Exception as e:
                                        print("exception: " + str(e))
    # Delete duplicates in list                                        
    uninstallers = list(set(uninstallers))
    return uninstallers


print(getUninstallersByPublisher(
        "Environmental Systems Research Institute, Inc."))

这个问题被问了无数次。搜索注册表重定向程序。在第二次打开时,您错误地使用了一个相对于“\registry\Machine”键(即HKLM)伪句柄的路径,但没有所需的
key\u WOW64\u 32KEY
key\u WOW64\u 64KEY
标志。它应该与父项相关,例如
subKey=winreg.OpenKey(Key,subKeyName)
。此外,请删除
bKeyName
及其所有引用。不要直接使用“Wow6432Node”键。这是API的一个实现细节,您已经在使用上面提到的标志。@eryksun感谢下面的提示和工作代码。同样,内部
OpenKey
应该使用带有相对子键路径的外部
句柄,例如
winreg.OpenKey(键,子键名,0,访问=(winreg.KEY_WOW64_64KEY | winreg.KEY_READ))
。请注意,标志应与按位OR组合,而不是加法。这一点非常重要,尤其是在可能已启用的变量中启用标志时(例如右:
1 | 1
。错误:
1+1
)。@eryksun编辑并测试,再次感谢您。非常感谢您的帮助!
import winreg
aKeyName = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

def getUninstallers(keyName, bitView):
    if ( bitView == 32 ):
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keyName, 0,
         access=(winreg.KEY_WOW64_32KEY | winreg.KEY_READ))
    elif ( bitView == 64 ):
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, keyName, 0,
         access=(winreg.KEY_WOW64_64KEY | winreg.KEY_READ))
    subKeyIndex = 0
    subkeys = list()
    while True:
        try:
            subKeyName = winreg.EnumKey(key,subKeyIndex)
            if( bitView == 32 ):
                subKey = winreg.OpenKey(key, subKeyName, 0,
                  access=(winreg.KEY_WOW64_32KEY | winreg.KEY_READ))
            elif( bitView == 64 ):
                subKey = winreg.OpenKey(key, subKeyName, 0,
                  access=(winreg.KEY_WOW64_64KEY | winreg.KEY_READ))
            valueIndex = 0
            subKeyList = list()
            while True:
                try:
                    aValue = winreg.EnumValue(subKey,valueIndex)
                    if(aValue[0] == 'Publisher' or aValue[0] == 'UninstallString'):
                        subKeyList.append(aValue)
                    valueIndex+=1
                except OSError as e:#[WinError 259] No more data is available
                    # These errors are supposed to happen, see winreg documentation
                    # at docs.python.org/3/library/winreg.html
                    if str(e).count('WinError 259') is 0:
                        print("OSError 1: " + str(e) + "\r" + subKeyName)
                    break
            if(len(subKeyList) > 0):
                subkeys.append(subKeyList)
            subKeyIndex += 1
        except OSError as e:#[WinError 259] No more data is available
            # These errors are supposed to happen, see winreg documentation
            # at docs.python.org/3/library/winreg.html
            if str(e).count('WinError 259') is 0:
                print("OSError 2: " + str(e) + "\r" + subKeyName)
            break
    return subkeys

def getUninstallersByPublisher(publisher):
    theKeys = [getUninstallers(aKeyName, 32), getUninstallers(aKeyName, 64)]
    uninstallers = list()
    for aKey in theKeys:
        for subKey in aKey:
            for aValue in subKey:
                for aString in aValue:
                    if(str(aString).count(publisher) > 0):
                        print("Found key for publisher: " + publisher)
                        for someValue in subKey:
                            for someString in someValue:
                                if(str(someString).count("Uninstall") > 0):
                                    try:
                                        print("Found UninstallString: " + someValue[1])
                                        uninstallers.append(someValue[1])
                                    except Exception as e:
                                        print("exception: " + str(e))
    # Delete duplicates in list                                        
    uninstallers = list(set(uninstallers))
    return uninstallers

somePublisher = "Microsoft Corporation"
print(getUninstallersByPublisher(somePublisher))