pythonpython-3.xwinreg

How do I search for value/string in Windows Registry REG_Multi_SZ?


I've created a wrapper class to manipulate a multi string registry value. The class is not complete and it assumes that the item already exists in the registry.

The problem i've come up against is that I cannot search for a string within the values I get back from winreg for the Reg_Multi_SZ item.

I get this error enter image description here

pylint has it listed as E0001:invalid syntax (string, line 33) (33, 1)

Here's the code

class AffectedDevices(object):
    path = r'SYSTEM\CurrentControlSet\Services\HidGuardian\Parameters'
    name = "test"

    def list_devices(self):
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self.path, 0, winreg.KEY_READ)
        values = winreg.QueryValueEx(key, self.name)
        winreg.CloseKey(key)
        return values[0]

    def clear_devices(self):
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self.path, 0, winreg.KEY_WRITE)
        winreg.SetValueEx(key, self.name, 0, winreg.REG_MULTI_SZ, [])
        winreg.CloseKey(key)

    def add_device(self, hid):
        values = self.list_devices()

        if hid is not in values:
            values.append(hid)
            key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self.path, 0, winreg.KEY_WRITE)
            winreg.SetValueEx(key, self.name, 0, winreg.REG_MULTI_SZ, values)
            winreg.CloseKey(key)

list_devices returns a List that looks like this: ['item 1', 'item 2', 'item 3'] and what I want to do in the add_devices function is test to see if a string (hid) is already in the list. If it is not then add it to the list and write the new list back to the registry. Writing a list works fine but I cannot search the list for some reason.

Also to clarify in list_devices() the variable values is a tuple that contains a list (['item1', 'item2'], 7) and the return statement should be sending back the list inside the tuple.


Solution

  • Had to remove the 'is' in 'if hid is not in values:'.

    Thanks roganjosh!