_winreg problem

B

Brad Tilley

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)
 
B

Brad Tilley

Brad said:
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)

I figured it out. I'm using _winreg.OpenKey() as if it were
_winreg.CreateKey() and when one does that things don't work... it's
been a long day!

OpenKey(HKEY_LOCAL_MACHINE, x, 0, KEY_ALL_ACCESS)
CreateKey(HKEY_LOCAL_MACHINE, x)
 
L

Larry Bates

Brad said:
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
 
B

Brad Tilley

Larry said:
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

Thanks, besides using OpenKey() correctly, I found that the values did
indeed have to be ints... here's a working function:

def log_limits():
logs = ['Application', 'Security', 'System']
vals = {'Retention':0, 'MaxSize':10223616, 'RestrictGuestAccess':1}
for l in logs:
x = r"SYSTEM\CurrentControlSet\Services\Eventlog\%s" % l
key = OpenKey(HKEY_LOCAL_MACHINE, x, 0, KEY_SET_VALUE)
for n,v in vals.iteritems():
SetValueEx(key,n,0,REG_DWORD,v)
CloseKey(key)
 
G

Georges JEGO

Brad said:
Thanks, besides using OpenKey() correctly, I found that the values did
indeed have to be ints...

Because you fixed the type to REG_DWORD in the SetValueEx call...

print _winreg.SetValueEx.__doc__
will show you the other possible types, you probably want REG_SZ.

-- Georges
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top