Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Registry Key Access
#1
How can I get access to an administrator-level access registry key via code? I read this article linked in the official docs, but I'm not sure where to go. The straight forward method is not working. Apparently I need to take ownership of the key? Here's the code that returns an access is denied error:
import winreg, ctypes

reg_key = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList'
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software', 0, winreg.KEY_ALL_ACCESS) as k:
    print(winreg.EnumKey(k, 0))
Google hasn't been too helpful. And before someone suggests it, I know I can give permission via Windows (regedit) and running the command prompt as admin can be done as well. I haven't tried either, but I don't want to do it that way, I want to do it via code. Hopefully someone here is familiar with registry related coding.
Reply
#2
Is it ok to ask for UAC permission?

import ctypes, sys

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    input("Now it is running with Admin's privileges!")
else:
    # Call the same script ar another one with Admin's Privileges
    ctypes.windll.shell32.ShellExecuteW(None, "my_script_name", sys.executable, __file__, None, 1)
From: request-uac-elevation-from-within-a-python-script
Reply
#3
Well, it's not working, @gontajones. Running it this way:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
causes weird behavior. Windows prompts for elevation, but then the cmd window repeatedly opens and closes so quickly that task manager can't close it. The only solution is a reboot.

This:
ctypes.windll.shell32.ShellExecuteW(None, "my_script_name", sys.executable, __file__, None, 1)
just returns a "FileNotFound" error. I've entered my script name alone ('module1.py) and the full path ('C:\\<path to>\module1.py')

I'm not sure what is wrong.
Reply
#4
Try this (without ".py"):

ctypes.windll.shell32.ShellExecuteW(None, "module1", sys.executable, __file__, None, 1)
Even the "basic" logic did not work for you? Using the input() like I did in my example?
Reply
#5
Yes, that code works fine. The function returns False (0) for me. I did cut it down to just the business end, however, which is the line of code I've been posting.

But, your suggestion of leaving out the extension did not work. I still get a "FileNotFound" error.

Programming can be frustrating.
Reply
#6
I made a mistake, you have to keep 'runas' as a parameter.

ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'my_next_script.py', None, 1)
my_next_script.py is the script to interact with the Registry Keys.
Put inside it ate last one input() to stop the execution and to check if it is doing what you want.
Reply
#7
First @gontajones, thanks for hanging in there with me. I don't just try what is posted and then post back here right away; I do expand on your suggestions. Here is what I've done (and I think you've maybe misunderstood a little what I'm trying to do[?]):
ctypes.windll.shell32.ShellExecuteW(None, 'module1', sys.executable, __file__, None, 1)
the line executes without an error, but doesn't solve the problem. Anyway, you said that that's wrong. Next I tried this:
ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, sys.argv[0], None, 1)
and
ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, __file__, None, 1)
The above two lines cause weird behavior. The command window opens and closes very quickly and focus switches between open windows very quickly. It can't be stopped except by logging the account off or rebooting.

I haven't tried your latest suggestion yet, but I fear the same thing will happen. But, let me say what I'm trying to do is run the script as admin from within itself. I suppose I could put it in a different script and execute it, but I don't want to do it that way. Also, I tried running from an elevated command prompt and still get the access denied.

Hey! Progress! I guess running the script and then raising rights right away with no conditions causes a loop. This at least allows the script to run as admin:
if ctypes.windll.shell32.IsUserAnAdmin():
    print('admin')
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'module1.py', None, 1)
I should have followed your whole code instead of trying to cut corners. Anywho, no dice. I still get an access denied error with this:
if ctypes.windll.shell32.IsUserAnAdmin():
    print('admin')
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'module1.py', None, 1)

with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'BCD00000000', 0, winreg.KEY_ALL_ACCESS) as k:
    print(k)
Back to square one, I guess.
Reply
#8
Even when it prints 'admin'?

Try to investigate it like this:
if ctypes.windll.shell32.IsUserAnAdmin():
    print('admin')
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'BCD00000000', 0, winreg.KEY_ALL_ACCESS) as k:
        print(k)
    input('Check your Regedit.')
else:
    ctypes.windll.shell32.ShellExecuteW(None, 'runas', sys.executable, 'module1.py', None, 1)
I'm not on a Windows machine right now but in a few hours I'll be. Then I'll be able to help you more efficiently. =)
Reply
#9
Heeey it works! I "see" the code now. But, yep, that did not throw an error. I got a result. Thank you very much, @gontajones!

Now I just have to work out how to pass the result I want to an tkinter GUI I'm cobbling together.

Feel like helping more...?
Reply
#10
Nice!

Show us what you've got so far with Tkinter.
But I think it would be better in a new thread, your call.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to add Python folder in Windows Registry ? Touktouk 1 278 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Read complete windows registry? fredep57 3 957 Mar-15-2023, 08:14 PM
Last Post: buran
  How to add new registry key shlomi27 3 10,331 Feb-04-2021, 05:45 PM
Last Post: reidnax
  Automatic registering python to registry kozaizsvemira 1 2,200 Oct-22-2019, 11:23 AM
Last Post: kozaizsvemira
  How to ignore - ERROR: The system was unable to find the specified registry key or va asheru93 9 6,669 Feb-04-2019, 06:35 AM
Last Post: asheru93
  Looking for advice about "registry" implementation voltron 0 2,301 Feb-06-2018, 07:26 AM
Last Post: voltron
  Python 3.x Windows 7 registry file associations broken pixhellmann 2 5,074 Sep-12-2017, 09:14 PM
Last Post: Larz60+
  [WinReg]>Issue when reading a registry key CSA75 4 8,939 Mar-28-2017, 03:35 PM
Last Post: CSA75

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020