Z
Z
Zero None2021-08-19 10:47:12
Python
Zero None, 2021-08-19 10:47:12

Why is it impossible to add a file to autorun through the registry?

I have code

import winreg
KEY = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', winreg.KEY_SET_VALUE)
winreg.SetValueEx(KEY, 'NEWNAME', 0, winreg.REG_NONE, r'C:\Users\Username\Desktop\REG.py')
key.Close()

According to the idea, he should add a value with a file for autorun to the registry. I searched for examples on the Internet - I did not find anything sensible (except this code). What's the problem: If you use winreg on the fourth line. REG_BINARY - TypeError: Objects of type 'str' can not be used as binary registry values ​​. With winreg. REG_NONE is the same. When trying to use winreg. REG_SZ (a value that I saw in the registry itself) - gives an error PermissionError: [WinError 5] Access is denied .

I know about the Startup folder, but I'm interested in the registry option.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-08-19
@ZERRITO

Objects of type 'str' can not be used as binary registry values
to the translator
If you write a string - winreg.REG_SZ
Plus, the parameters were passed incorrectly
KEY = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 0, winreg.KEY_SET_VALUE)

or
KEY = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', access=winreg.KEY_SET_VALUE)

In general, winreg.KEY_SET_VALUEmust be set for the access. The method itself looks like this:
winreg.OpenKey(key, sub_key, reserved=0, access=KEY_READ)

And in your version, you winreg.KEY_SET_VALUEpassed in the parameterreserved

R
Ruslan., 2021-08-19
@LaRN

It looks like you do not have permission to write to this registry branch. If you are doing this on your machine, then here you can read how to get the right rights.
https://www.outsidethebox.ms/10539/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question