def _run_shortcut(self, shortcut): try: if shortcut.type == SHORTCUT_TYPE_CONTROL_PANEL: target = shortcut.target logger.info("Executing '%s'", target) # os.startfile does not work with .cpl files, # ShellExecute have to be used. #FIXME: Replace with regexp, there will be probably more such things if target.startswith("mshelp://") or target.startswith("ms-help://"): params = None work_dir = None else: target, params = utils.splitcmdline(target) target = os.path.normpath(utils.expand_win_path_variables(target)) params = " ".join( ( ('"%s"' % p if ' ' in p else p) for p in (utils.expand_win_path_variables(p) for p in params) ) ) work_dir = os.path.dirname(target) try: _ = win32api.ShellExecute( 0, 'open', target, params, work_dir, win32con.SW_SHOWDEFAULT) except Exception, e: #IGNORE:W0703 logger.error(e) try: os.startfile(target) except WindowsError, e: logger.error("%d: %s", e.errno, e)
def open_with_shortcut(shortcut, targets): # User did not select any application. Offer system "Open with..." dialog if not shortcut: for file_name in targets: try: _ = win32api.ShellExecute( 0, 'open', "rundll32.exe", "shell32.dll,OpenAs_RunDLL %s" % file_name, None, win32con.SW_SHOWDEFAULT) except Exception as e: # IGNORE:W0703 logger.error(e) return executable = utils.expand_win_path_variables(shortcut.target) workdir = dirname(executable) _, ext = splitext(executable) # If it is a shortcut, extract the executable info # for to be able to pass the command-line parameters if ext.lower() == ".lnk": sl = win_shortcuts.PyShellLink(executable) executable = sl.get_target() workdir = sl.get_working_dir() if not workdir: workdir = dirname(executable) # print executable, workdir params = u" ".join((u'"%s"' % file_name for file_name in targets)) # print params try: win32api.ShellExecute(0, 'open', "\"" + executable + "\"", params, workdir, win32con.SW_SHOWDEFAULT) except Exception as e: # IGNORE:W0703 logger.error(e)
def read_mui_string_from_dll(id): assert id.startswith("@") and ",-" in id, \ "id has invalid format. Expected format is '@dllfilename,-id'" m = re.match("@([^,]+),-([0-9]+)(;.*)?", id) if m: dll_filename = utils.expand_win_path_variables(m.group(1)) string_id = long(m.group(2)) else: raise Exception("Error parsing dll-filename and string-resource-id from '%s'" % id) h = win32api.LoadLibraryEx( dll_filename, None, win32con.LOAD_LIBRARY_AS_DATAFILE | win32con.DONT_RESOLVE_DLL_REFERENCES) if h: s = win32api.LoadString(h, string_id) return s return None
def read_mui_string_from_dll(id): assert id.startswith("@") and ",-" in id, \ "id has invalid format. Expected format is '@dllfilename,-id'" m = re.match("@([^,]+),-([0-9]+)(;.*)?", id) if m: dll_filename = utils.expand_win_path_variables(m.group(1)) string_id = long(m.group(2)) else: raise Exception( "Error parsing dll-filename and string-resource-id from '%s'" % id) h = win32api.LoadLibraryEx( dll_filename, None, win32con.LOAD_LIBRARY_AS_DATAFILE | win32con.DONT_RESOLVE_DLL_REFERENCES) if h: s = win32api.LoadString(h, string_id) return s return None
try: _ = win32api.ShellExecute( 0, 'open', target, params, work_dir, win32con.SW_SHOWDEFAULT) except Exception, e: #IGNORE:W0703 logger.error(e) try: os.startfile(target) except WindowsError, e: logger.error("%d: %s", e.errno, e) else: target = os.path.normpath(utils.expand_win_path_variables(shortcut.shortcut_filename)) logger.info("Executing '%s'", target) try: os.startfile(target) except WindowsError, e: #TODO: Why am I getting 'bad command' error on Win7 instead of 'not found' error? if e.errno in (winerror.ERROR_FILE_NOT_FOUND, winerror.ERROR_BAD_COMMAND): ensoapi.display_message(u"File has not been found. Please adjust the shortcut properties.") logger.error("%d: %s", e.errno, e) try: _ = win32api.ShellExecute( 0, 'properties', target, None,
def run_shortcut(shortcut): try: if shortcut.type == SHORTCUT_TYPE_CONTROL_PANEL: target = shortcut.target # os.startfile does not work with .cpl files, # ShellExecute have to be used. # FIXME: Replace with regexp, there will be probably more such # things if target.startswith("mshelp://") or target.startswith( "ms-help://"): params = None work_dir = None else: target, params = utils.splitcmdline(target) target = normpath(utils.expand_win_path_variables(target)) params = " ".join((('"%s"' % p if ' ' in p else p) for p in (utils.expand_win_path_variables(p) for p in params))) # Fix params if there is special Control Panel syntax like: # shell32.dll,Control_RunDLL C:\Windows\system32\FlashPlayerCPLApp.cpl,@0 # where utils.splitcmdline erroneously separate last part before , as: # shell32.dll,Control_RunDLL # C:\Windows\system32\FlashPlayerCPLApp.cpl ,@0 if ".cpl" in params: params = re.sub(r"(.*) (,@[0-9]+)$", "\\1\\2", params) work_dir = dirname(target) logger.info("Executing '%s%s'", target, " " + params if params else "") try: _ = win32api.ShellExecute(0, 'open', target, params, work_dir if work_dir else None, win32con.SW_SHOWDEFAULT) except Exception as e: # IGNORE:W0703 logger.error(e) try: os.startfile(target) except WindowsError as e: logger.error("%d: %s", e.errno, e) elif shortcut.type == SHORTCUT_TYPE_FOLDER: try: os.startfile(shortcut.target) except WindowsError as e: logger.error("%d: %s", e.errno, e) else: target = normpath( utils.expand_win_path_variables(shortcut.shortcut_filename)) logger.info("Executing '%s'", target) try: os.startfile(target) """ subprocess.Popen( target, shell=True, #stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, creationflags=win32process.DETACHED_PROCESS | win32process.CREATE_NEW_PROCESS_GROUP ) """ except WindowsError as e: # TODO: Why am I getting 'bad command' error on Win7 instead of # 'not found' error? if e.errno in (winerror.ERROR_FILE_NOT_FOUND, winerror.ERROR_BAD_COMMAND): ensoapi.display_message( u"File has not been found. Please adjust the shortcut properties." ) logger.error("%d: %s", e.errno, e) try: _ = win32api.ShellExecute(0, 'properties', target, None, None, win32con.SW_SHOWDEFAULT) except Exception as e: # IGNORE:W0703 logger.error(e) elif e.errno == winerror.ERROR_NO_ASSOCIATION: # No application is associated with the specified file. # Open system "Open with..." dialog: try: _ = win32api.ShellExecute( 0, 'open', "rundll32.exe", "shell32.dll,OpenAs_RunDLL %s" % target, None, win32con.SW_SHOWDEFAULT) except Exception as e: # IGNORE:W0703 logger.error(e) else: logger.error("%d: %s", e.errno, e) return True except Exception as e: # IGNORE:W0703 logger.error(e) return False