8

Here is my code:

import winreg as wreg
key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters',wreg.KEY_ALL_ACCESS)
wreg.SetValueEx(key,"IPEnableRouter", 0, wreg.REG_DWORD, 1)

When i run this script, it says

PermissionError: [WinError 5] Access is Denied

How to change the value as 0 to 1 or 1 to 0?

Dipiks
  • 3,818
  • 2
  • 23
  • 39
hckn0
  • 137
  • 1
  • 2
  • 12
  • try in an elevated shell. But not guaranteed it will work. – Jean-François Fabre Jan 26 '17 at 09:34
  • 1
    I tried to run cmd as administrator and run the script. But still the same error. UAC etc. is already lowest level i don't know if it is important – hckn0 Jan 26 '17 at 09:38
  • Are you attempting to access the 32 bit registry? or the 64 bit one? Maybe try using (KEY_WOW64_64KEY + KEY_QUERY_VALUE) or (KEY_WOW64_32KEY + KEY_QUERY_VALUE) for your search flag – Har Jan 26 '17 at 09:55
  • 1
    Well, i tried this "wreg.SetValueEx(key,"IPEnableRouter", wreg.REG_DWORD, 1,wreg.KEY_WOW64_64KEY)" and "key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters',wreg.KEY_ALL_ACCESS,wreg.KEY_WOW64_64KEY)" Both gives the same error. – hckn0 Jan 26 '17 at 10:00
  • any more idea? please – hckn0 Jan 26 '17 at 10:45

1 Answers1

9

Three things to try:

  1. Add an extra 0 to your parameters for the res. Currently you not setting the sam.

  2. Use the Registry Editor to change the permissions on the key to allow you as a user to have access.

Regedit screenshot

  1. It is better to always request the minimum required access, so I would recommend you use wreg.KEY_SET_VALUE instead of wreg.KEY_ALL_ACCESS.

So the script would be as follows:

import _winreg as wreg

key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters', 0, wreg.KEY_SET_VALUE)
wreg.SetValueEx(key, "IPEnableRouter", 1, wreg.REG_DWORD, 1)
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Thanks! It worked, i mean, there is no error. But i couldn't understand which integer sets the value. I changed to 0 all of them but regedit still says "0x000001(1)" – hckn0 Jan 26 '17 at 11:47
  • 1
    It is actually the last parameter that sets the value, see [`SetValueEx`](https://docs.python.org/2/library/_winreg.html?highlight=winreg#_winreg.SetValueEx). If you are looking in regedit, don't forget to refresh the display with F5 after you run the script. – Martin Evans Jan 26 '17 at 11:50
  • Ah.. my bad. Yes i forgot "f5", even i saw the correct value when i click. So thanks for your effoor and time – hckn0 Jan 26 '17 at 11:53