pyuac icon indicating copy to clipboard operation
pyuac copied to clipboard

requires the script absolute path

Open ccoles146 opened this issue 1 year ago • 3 comments

It took me some time to troubleshoot and figure out why the script was producing an error and didn't seem to be behaving as expected so there are two issues:

  1. The wait for handle does not work. If WAIT is true then it fails because the handle is invalid. If WAIT is false then it still waits but it does not attempt to read the process handle so no errors are returned.
  2. The reason it wasn't working as expected is because it does not know the path of the script. To solve these issues I used the following:
        sys.argv[0] = os.path.abspath(__file__)
        cmdLine = [sys.executable] + sys.argv
        pyuac.runAsAdmin(cmdLine,wait=False)

ccoles146 avatar Feb 15 '24 12:02 ccoles146

In case it helps anyone else, I use the following lines to help with troubleshooting:

sys.argv[0] = os.path.abspath(__file__)
cmd = ["C:\Windows\System32\cmd.exe", "/K"]
cmdLine = cmd + [sys.executable] + sys.argv
pyuac.runAsAdmin(cmdLine,wait=False)

ccoles146 avatar Feb 15 '24 15:02 ccoles146

I think I am missing some context here -- how were you invoking the python script?

Preston-Landers avatar Feb 15 '24 16:02 Preston-Landers

Hi, ok, so after further testing it seems that the absolute path is not needed but I do need the wait=False parameter, otherwise I get the following error.

  File "C:\Python310\lib\site-packages\pyuac\admin.py", line 90, in runAsAdmin
    _ = win32event.WaitForSingleObject(procHandle, win32event.INFINITE)
pywintypes.error: (6, 'WaitForSingleObject', 'The handle is invalid.')

It seems like the original script that calls runAsAdmin waits whilst the script executes as admin, once the admin script completes and closes then the handle is invalid and I get the following error. The full test script is below:

import pyuac    # pip install pyuac && pip install pypiwin32
import sys  # for basic command line parsing
import os
from time import sleep

def main(src, dst):
    print("Running Install Main with source: {} and destination: {}".format(src,dst))
    sleep(10)  

if __name__ == "__main__":

    # Check for necessary information
    if len(sys.argv) == 1:
        # Default source and destination
        src = "C:\\temp\\temp.zip"
        dst = "C:\\temp"
    # Can specify an ini file or just a source file
    elif len(sys.argv) == 2:
        if sys.argv[1].endswith('.ini'):
            src = '' # we'll get the source from the ini later
        else:
            src = sys.argv[1]   # first argument is the source file
    # Source and Destination specified
    elif len(sys.argv) == 3:
        src = sys.argv[1]   # first argument is the source file    
        dst = sys.argv[2]   # second argument is the destination folder

    if not pyuac.isUserAdmin():
        print("Re-launching as admin!")
        #sys.argv[0] = os.path.abspath(__file__)
        cmd = ["C://Windows//System32//cmd.exe", "/K"]
        cmdLine = cmd + [sys.executable] + sys.argv
        pyuac.runAsAdmin()
    else:
        main(src, dst)  # Running as admin

ccoles146 avatar Feb 16 '24 07:02 ccoles146