Invalid access to winreg module

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 a value from 0 to 1 or from 1 to 0?

+4
source share
1 answer

Three things to try:

  • Add more 0to your options for res. You are not currently installing sam.

  • Use the registry editor to change key permissions to allow you as a user to have access.

Screenshot Regedit

  1. It's best to always ask for the minimum required access, so I recommend using wreg.KEY_SET_VALUEinstead wreg.KEY_ALL_ACCESS.

So the script will look like this:

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)
+4
source

Source: https://habr.com/ru/post/1667854/


All Articles