Python Forum
Registry Key Access - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Registry Key Access (/thread-11767.html)

Pages: 1 2


Registry Key Access - malonn - Jul-25-2018

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.


RE: Registry Key Access - gontajones - Jul-25-2018

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


RE: Registry Key Access - malonn - Jul-25-2018

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.


RE: Registry Key Access - gontajones - Jul-25-2018

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?


RE: Registry Key Access - malonn - Jul-25-2018

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.


RE: Registry Key Access - gontajones - Jul-25-2018

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.


RE: Registry Key Access - malonn - Jul-25-2018

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.


RE: Registry Key Access - gontajones - Jul-25-2018

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. =)


RE: Registry Key Access - malonn - Jul-25-2018

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...?


RE: Registry Key Access - gontajones - Jul-25-2018

Nice!

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