_winreg problem

Larry Bates lbates at syscononline.com
Thu Oct 7 18:43:12 EDT 2004


Brad Tilley wrote:
> The below function fails with this error:
> ValueError: Could not convert the data to the specified type.
> 
> The problem appears to be the v variable. When I make v an int, I get:
> WindowsError: [Error 5] Access is denied. This makes sense to me as the 
> _winreg docs says that value (v) in SetvalueEx should be a string, but I 
> do not understand why it  gives the "ValueError" error when I make it a 
> string. Also, I can do one key (Log) at a time, but not iterate over all 
> of them like I'd like to do. I've checked it with print statements... 
> every thing looks right.
> 
> Any suggestions?
> 
> def log_limits():       ## 18
>     ## For winXP SP2 & Python 2.3.4
>     ## set log limits on EventLogs, keep guests out of logs and set them 
> to overwrite as needed.
>     logs = ['Application', 'Security', 'System']
>     vals = {'Retention':'0', 'MaxSize':'10223616', 
> 'RestrictGuestAccess':'1'}
>     for l in logs:
>         x = r"SYSTEM\CurrentControlSet\Services\Eventlog\%s" % l
>         ## This print shows I'm iterating of the logs correctly.
>         print x
>         key = OpenKey(HKEY_LOCAL_MACHINE, x)
>         for n,v in vals.iteritems():
>             ## This print shows I'm associating the value pairs with 
> each Log
>             print n,v
>             ## Here is where the failure occurs... the v at the end:
>             SetValueEx(key,n,0,REG_DWORD,v)
>         CloseKey(key)



It took me DAYS to find this on the Net.  The "trick" is you need
extra parameters on your OpenKey call, otherwise you get your error.

Try following, I think it will help.:

     key = OpenKey(HKEY_LOCAL_MACHINE, x, 0, _winreg.KEY_SET_VALUE)

Good Luck,

Larry Bates



More information about the Python-list mailing list