#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Reading/Editing Registry Values


    Can anyone show me a way to edit an existing value, whether it be a string, dword, etc.? Right now Im using the following code, but I don't nkow how to set it up correctly.
    Code:
    import _winreg as wreg
    import cPickle as pickle
    class WindowsRegistry:
    
        def __init__(self, company="Microsoft", project="Internet Explorer", write=0):
            """
            handle registry access
            """
            self.write = write
            self.company = company
            self.project = project
            self.keyname = "Software\\%s\\%s" % (self.company, self.project)
    
            try:
                self.key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, self.keyname)
            except:
                if self.write:
                    self.key = wreg.CreateKey(wreg.HKEY_CURRENT_USER, self.keyname)
    
        def set(self, name, value):
            " set value in registry "
            if not self.write:
                raise Exception, "registry is read only"
            wreg.SetValue(self.key, name, wreg.REG_SZ,str(value))
    
        def pset(self, name, value):
            " set using pickle "
            self.set(name, pickle.dumps(value))
    
        def get(self, name):
            " get value out of registry "
            return wreg.QueryValue(self.key, name)
    
        def pget(self, name):
            " get using pickle "
            return pickle.loads(self.get(name))
    
        def close(self):
            " close the key finally "
            self.key.Close()
    
        def __del__(self):
            self.close()
    
    if __name__=="__main__":
        r = WindowsRegistry(project="Internet Explorer\Main", write=0)
        r.get("FullScreen.reg")
    Now, I want it to read HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FullScreen
    Eventually I want to edit its binary data. But I'm not sure if this code has that ability.

    Update: I just tried doing,
    Code:
    WindowsRegistry.set('HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main', 'FullScreen', 'yes')
    I got the following error:
    Code:
    Traceback (most recent call last):
      File "C:/Documents and Settings/Master/Desktop/IEEdit.py", line 50, in -toplevel-
        WindowsRegistry.set('HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main', 'FullScreen', 'yes')
    TypeError: unbound method set() must be called with WindowsRegistry instance as first argument (got str instance instead)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    With
    Code:
    WindowsRegistry.set('HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main', 'FullScreen', 'yes')
    you are calling the method of a class. If you have a look at the method definition, the first parameter passed to the method is "self". This "self" is passed automatically if you are calling the method of an instance of this class (i.e. an object). But if you are calling the method of a class, you must provide an instance of this class as the first parameter.
    Usually it's best to create an instance of a class and then call the method(s) of the object. This can be done by creating an anonymous object on the fly like this:
    Code:
    WindowsRegistry().set('HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FullScreen', 'yes')
    or if you want to keep the object for further use:
    Code:
    wr = WindowsRegistry()
    wr.set('HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FullScreen', 'yes')
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I'm still having problems editing the registry. With the same WindowsRegistry class, here is the code I'm using.
    Code:
    if __name__=="__main__":
        try:
            print 'Setting registry...'
            WindowsRegistry().set('HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon\Empty', 'C:\WINDOWS\Resources\Icons\Windows Black\Desktop.ico,0')
            print 'Set registry!'
        except:
            print traceback.print_exc()
    I want the Recycle Bin icon to change to 'C:\WINDOWS\Resources\Icons\Windows Black\Desktop.ico, when it is Empty. When the program runs it prints out Setting registry... and Set registry!, but it doesn't really edit it. Is there something else wrong?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Try modifying the WindowsRegistry class in that the winreg.OpenKey function opens the key with write access if 'write' is set, e.g.
    Code:
    ...
    ...
    class WindowsRegistry:
    
        def __init__(self, company="Microsoft", project="Internet Explorer", write=0):
            """
            handle registry access
            """
            self.write = write
            self.company = company
            self.project = project
            self.keyname = "Software\\%s\\%s" % (self.company, self.project)
    
            try:
                access = wreg.KEY_READ
                if self.write:
                    access |= wreg.KEY_SET_VALUE|wreg.KEY_WRITE
                self.key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, self.keyname, sam = access)
            except:
                if self.write:
    ...
    ...
    For setting a value, create the WindowsRegistry object with write access:
    Code:
    WindowsRegistry(write=1).set('HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon\Empty', 'C:\WINDOWS\Resources\Icons\Windows Black\Desktop.ico,0')
    Note that i didn't check the code on my computer because I currently don't want to mess around with my registry, but I think it should work.

IMN logo majestic logo threadwatch logo seochat tools logo