我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用win32com.shell.shell.ShellExecuteEx()。
def run_elevated(command, args, wait=True): """Run the given command as an elevated user and wait for it to return""" ret = 1 if command.find(' ') > -1: command = '"' + command + '"' if platform.system() == 'Windows': import win32api import win32con import win32event import win32process from win32com.shell.shell import ShellExecuteEx from win32com.shell import shellcon logging.debug(command + ' ' + args) process_info = ShellExecuteEx(nShow=win32con.SW_HIDE, fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, lpVerb='runas', lpFile=command, lpParameters=args) if wait: win32event.WaitForSingleObject(process_info['hProcess'], 600000) ret = win32process.GetExitCodeProcess(process_info['hProcess']) win32api.CloseHandle(process_info['hProcess']) else: ret = process_info else: logging.debug('sudo ' + command + ' ' + args) ret = subprocess.call('sudo ' + command + ' ' + args, shell=True) return ret
def UseCommandLine(*classes, **flags): unregisterInfo = '--unregister_info' in sys.argv unregister = '--unregister' in sys.argv flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv if unregisterInfo: return UnregisterInfoClasses(*classes, **flags) try: if unregister: UnregisterClasses(*classes, **flags) else: RegisterClasses(*classes, **flags) except win32api.error, exc: # If we are on xp+ and have "access denied", retry using # ShellExecuteEx with 'runas' verb to force elevation (vista) and/or # admin login dialog (vista/xp) if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \ or sys.getwindowsversion()[0] < 5: raise ReExecuteElevated(flags)
def UseCommandLine(*classes, **flags): unregisterInfo = '--unregister_info' in sys.argv unregister = '--unregister' in sys.argv flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv if unregisterInfo: return UnregisterInfoClasses(*classes, **flags) try: if unregister: UnregisterClasses(*classes, **flags) else: RegisterClasses(*classes, **flags) except win32api.error as exc: # If we are on xp+ and have "access denied", retry using # ShellExecuteEx with 'runas' verb to force elevation (vista) and/or # admin login dialog (vista/xp) if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \ or sys.getwindowsversion()[0] < 5: raise ReExecuteElevated(flags)
def connect_to_tcp_admin(): ASADMIN = 'asadmin' # Get admin permissions on Windows if sys.argv[-1] != ASADMIN: script = os.path.abspath(sys.argv[0]) params = ' '.join([script] + sys.argv[1:] + [ASADMIN]) shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params) connect_to_tcp() input('Press ENTER to exit')
def ExplorePIDL(): pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP) print "The desktop is at", shell.SHGetPathFromIDList(pidl) shell.ShellExecuteEx(fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, nShow=win32con.SW_NORMAL, lpClass="folder", lpVerb="explore", lpIDList=pidl) print "Done!"
def _elevate_permissions(): f = sys.executable if not __file__.split("\\")[-1].split(".")[-1] == "py": f = __file__ params = ' '.join(sys.argv[1:] + ["asadmin"]) else: script = os.path.abspath(sys.argv[0]) params = ' '.join([script] + sys.argv[1:] + ['asadmin']) print params if not sys.argv[-1] == 'asadmin': shell.ShellExecuteEx(lpVerb='runas', lpFile=f, lpParameters=params) sys.exit(0)
def ExplorePIDL(): pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP) print("The desktop is at", shell.SHGetPathFromIDList(pidl)) shell.ShellExecuteEx(fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, nShow=win32con.SW_NORMAL, lpClass="folder", lpVerb="explore", lpIDList=pidl) print("Done!")
def run_as_admin(): procInfo = ShellExecuteEx(nShow=win32con.SW_SHOWNORMAL, fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, lpVerb='runas', lpFile=sys.executable) procHandle = procInfo['hProcess'] obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE) return win32process.GetExitCodeProcess(procHandle)
def runAsAdmin(cmdLine=None, wait=True): if os.name != 'nt': raise RuntimeError, "This function is only implemented on Windows." import win32api import win32con import win32event import win32process from win32com.shell.shell import ShellExecuteEx from win32com.shell import shellcon python_exe = sys.executable if cmdLine is None: cmdLine = [python_exe] + sys.argv elif type(cmdLine) not in (types.TupleType, types.ListType): raise ValueError, "cmdLine is not a sequence." cmd = '"%s"' % (cmdLine[0],) # XXX TODO: isn't there a function or something we can call to massage command line params? params = " ".join(['"%s"' % (x,) for x in cmdLine[1:]]) cmdDir = '' #showCmd = win32con.SW_SHOWNORMAL showCmd = win32con.SW_HIDE lpVerb = 'runas' # causes UAC elevation prompt. # print "Running", cmd, params # ShellExecute() doesn't seem to allow us to fetch the PID or handle # of the process, so we can't get anything useful from it. Therefore # the more complex ShellExecuteEx() must be used. # procHandle = win32api.ShellExecute(0, lpVerb, cmd, params, cmdDir, showCmd) procInfo = ShellExecuteEx(nShow=showCmd, fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, lpVerb=lpVerb, lpFile=cmd, lpParameters=params) if wait: procHandle = procInfo['hProcess'] obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE) rc = win32process.GetExitCodeProcess(procHandle) # print "Process handle %s returned code %s" % (procHandle, rc) else: rc = None return rc
def runAsAdmin(cmdLine=None, wait=True): if os.name != 'nt': raise RuntimeError, "This function is only implemented on Windows." import win32api, win32con, win32event, win32process from win32com.shell.shell import ShellExecuteEx from win32com.shell import shellcon python_exe = sys.executable if cmdLine is None: cmdLine = [python_exe] + sys.argv elif type(cmdLine) not in (types.TupleType,types.ListType): raise ValueError, "cmdLine is not a sequence." cmd = '"%s"' % (cmdLine[0],) # XXX TODO: isn't there a function or something we can call to massage command line params? params = " ".join(['"%s"' % (x,) for x in cmdLine[1:]]) cmdDir = '' showCmd = win32con.SW_SHOWNORMAL #showCmd = win32con.SW_HIDE lpVerb = 'runas' # causes UAC elevation prompt. # print "Running", cmd, params # ShellExecute() doesn't seem to allow us to fetch the PID or handle # of the process, so we can't get anything useful from it. Therefore # the more complex ShellExecuteEx() must be used. # procHandle = win32api.ShellExecute(0, lpVerb, cmd, params, cmdDir, showCmd) procInfo = ShellExecuteEx(nShow=showCmd, fMask=shellcon.SEE_MASK_NOCLOSEPROCESS, lpVerb=lpVerb, lpFile=cmd, lpParameters=params) if wait: procHandle = procInfo['hProcess'] obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE) rc = win32process.GetExitCodeProcess(procHandle) #print "Process handle %s returned code %s" % (procHandle, rc) else: rc = None return rc