_winreg.saveKey question

R

RodneyWMcBride

Reply with quote Edit/Delete this post Delete this post
Post _winreg.SaveKey question
Does anyone know where I might find some sample code of using the
saveKey function? I am getting an error 5 access denied and I think
that it is coming from the way I am specifying the filename as in
_winreg.SaveKey(key, filename). The docs mention that if the key is to
a remote host (which it is in my case) that the path is relative. I
read this as meaning that I should use a unc path back to where I want
to save the registry key file. Here is a section of the code I am
using:

def remoteSaveKey(host, key, file):
rhostreg = _winreg.ConnectRegistry(host, _winreg.HKEY_LOCAL_MACHINE)
remotekey = _winreg.OpenKey(rhostreg, key)
_winreg.SaveKey(remotekey, file)


.... from main():

readfile = open('hostlist.txt', 'r')
hostfile = []
for host in readfile:
hostfile += [host.strip()]

for srvname in hostfile:
outputpath = destsrvr + srvname # destsrvr is server/share
outputfile = outputpath + "\\" + srvname
output = remoteSaveKey(hostname, regpath, outputfile)

I am able to connect to the registry and I believe that the key is
being specified correctly, but I am getting the following error:

Traceback (most recent call last):
File "main.py", line 40, in ?
output = remoteSaveKey(srvname, regpath, outputfile)
File "main.py", line 17, in remoteSaveKey
_winreg.SaveKey(remotekey, file)
WindowsError: [Errno 5] Access is denied

I have searched but have only been able to find documentation with
little to no examples on how to implement the SaveKey function with a
remote host. Any help will be appreciated.

thx
 
L

Larry Bates

remotekey = _winreg.OpenKey(rhostreg, key)


Try adding the sam=_winreg.KEY_SET_VALUE parameter.

Help on built-in function OpenKey in module _winreg:

OpenKey(...)
key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the
specified key.

key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that identifies the sub_key to open
res is a reserved integer, and must be zero. Default is zero.
sam is an integer that specifies an access mask that describes the desired
security access for the key. Default is KEY_READ

The result is a new handle to the specified key
If the function fails, an EnvironmentError exception is raised.


The default is KEY_READ so you can't update.

-Larry Bates
 
L

Larry Bates

remotekey = _winreg.OpenKey(rhostreg, key)


Try adding the sam=_winreg.KEY_SET_VALUE parameter.

Help on built-in function OpenKey in module _winreg:

OpenKey(...)
key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the
specified key.

key is an already open key, or any one of the predefined HKEY_* constants.
sub_key is a string that identifies the sub_key to open
res is a reserved integer, and must be zero. Default is zero.
sam is an integer that specifies an access mask that describes the desired
security access for the key. Default is KEY_READ

The result is a new handle to the specified key
If the function fails, an EnvironmentError exception is raised.


The default is KEY_READ so you can't update.

-Larry Bates
 
D

Dennis Lee Bieber

saveKey function? I am getting an error 5 access denied and I think
that it is coming from the way I am specifying the filename as in
_winreg.SaveKey(key, filename). The docs mention that if the key is to
a remote host (which it is in my case) that the path is relative. I
read this as meaning that I should use a unc path back to where I want
to save the registry key file. Here is a section of the code I am
using:

The docs ALSO mention:
The caller of this method must possess the SeBackupPrivilege
security privilege. Note that privileges are different than permissions
- see the Win32 documentation for more details.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top