1

I am wanting to add a registry key with python via this code:

import _winreg
from time import sleep
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\\Microsoft\\Windows\\CurrentVersion\\Run',_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(key,'Windows-Update',0,_winreg.REG_BINARY,'C:\Windows\System32\SystemSetting\Block.exe') 
key.Close()

But it shows up this error WindowsError: [Error 5] Access is denied.

Any solution?

EDIT - I have already run it as admin

EDIT2 - Is it related to KEY_ALL_ACCESS

  • Run Python interpreter as admin. – Łukasz Rogalski Apr 09 '16 at 12:42
  • @Rogalski Already done –  Apr 09 '16 at 12:46
  • Shouldn't the path string be `'C:\\Windows\\System32\\SystemSetting\\Block.exe'`? – martineau Apr 09 '16 at 13:25
  • @martineau You don't need that while adding to registry as if you put double `\\ ` then the actual path would also change with double `\\` –  Apr 09 '16 at 13:32
  • If you're running the 32-bit Python interpreter on 64-bit Windows OS, the issue may something to do with to way the [WoW64](https://en.wikipedia.org/wiki/WoW64#Registry_and_file_system) subsystem handles interactions with the Windows Registry. – martineau Apr 09 '16 at 13:34
  • @martineau Yes I know that but that is not a issue, that is an excellent solution to 32bit and 64bit related issues –  Apr 09 '16 at 13:35
  • I think you may be wrong about not needing the double backslashes — because the handling of backslashes in string literals is hardcoded into the Python interpreter. – martineau Apr 09 '16 at 13:46
  • @martineau No. I went to regedit after running the script and the path was correct –  Apr 09 '16 at 13:55
  • How could that be if the script isn't working? What line is giving you the `WindowsError`? – martineau Apr 09 '16 at 13:57
  • @martineau After I ran it CORRECTLY as administartor which I wasn't doing then, it gave me the correct path –  Apr 09 '16 at 14:02
  • Related: [How to avoid “WindowsError: \[Error 5\] Access is denied”](https://stackoverflow.com/q/37830326/3357935) – Stevoisiak Jun 13 '18 at 16:03

2 Answers2

1

Run the python program inside command prompt. There is a command prompt (Admin) program available in windows. Or simply right click on Command prompt and select Run as administrator. Ref

Mitty
  • 330
  • 2
  • 15
  • what python program? the script? –  Apr 09 '16 at 13:07
  • yup, store the program as a py file and run it in command prompt. – Mitty Apr 09 '16 at 13:10
  • As a sidenote I will be compiling this script and distributing it. So I think the `Run as Admin` option after right clicking will be sufficient. Right? –  Apr 09 '16 at 13:12
  • It depends upon the machine, so you have to test it in different machines to verify. – Mitty Apr 09 '16 at 13:20
1

It is not about runnig as admin. I tried runnig as admin and still got Acces is denied message.

You must use reserved integer which is 0 by default.

_winreg.OpenKey(key, sub_key[, res[, sam]]) ... res is a reserved integer, and must be zero. The default is zero.

So, it should be like this:

key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, "Software\\TestCompany\\TestProject",0, wreg.KEY_SET_VALUE)

You don't realy have to use KEY_ALL_ACCESS like suggested here. Just add that 0 before _winreg.KEY_SET_VALUE.

Hrvoje T
  • 3,365
  • 4
  • 28
  • 41