Replace another use of pywin32

This commit is contained in:
Kovid Goyal 2020-10-14 21:48:30 +05:30
parent 7f4859f8d3
commit 384e3a02be
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 10 deletions

View File

@ -68,9 +68,8 @@ if iswindows:
# Windows {{{
from calibre.utils.winreg.default_programs import find_programs, friendly_app_name
from calibre.utils.open_with.windows import load_icon_resource, load_icon_for_cmdline
from win32process import CreateProcess, STARTUPINFO
from win32event import WaitForInputIdle
import win32con
from calibre.constants import plugins
import subprocess
oprefs = JSONConfig('windows_open_with')
def entry_sort_key(entry):
@ -136,20 +135,17 @@ if iswindows:
def run_program(entry, path, parent): # noqa
import re
cmdline = entry_to_cmdline(entry, path)
flags = win32con.CREATE_DEFAULT_ERROR_MODE | win32con.CREATE_NEW_PROCESS_GROUP
flags = subprocess.CREATE_DEFAULT_ERROR_MODE | subprocess.CREATE_NEW_PROCESS_GROUP
if re.match(r'"[^"]+?(.bat|.cmd|.com)"', cmdline, flags=re.I):
flags |= win32con.CREATE_NO_WINDOW
flags |= subprocess.CREATE_NO_WINDOW
console = ' (console)'
else:
flags |= win32con.DETACHED_PROCESS
flags |= subprocess.DETACHED_PROCESS
console = ''
print('Running Open With commandline%s:' % console, repr(entry['cmdline']), ' |==> ', repr(cmdline))
try:
with sanitize_env_vars():
process_handle, thread_handle, process_id, thread_id = CreateProcess(
None, cmdline, None, None, False, flags,
None, None, STARTUPINFO())
WaitForInputIdle(process_handle, 2000)
plugins['winutil'][0].run_cmdline(cmdline, flags, 2000)
except Exception as err:
return error_dialog(
parent, _('Failed to run'), _(

View File

@ -819,6 +819,22 @@ parse_cmdline(PyObject *self, PyObject *args) {
return ans;
}
static PyObject*
run_cmdline(PyObject *self, PyObject *args) {
wchar_raii cmdline;
unsigned long flags;
unsigned long wait_for = 0;
if (!PyArg_ParseTuple(args, "O&k|k", py_to_wchar_no_none, &cmdline, &flags, &wait_for)) return NULL;
STARTUPINFO si = {0};
si.cb = sizeof(si);
PROCESS_INFORMATION pi = {0};
if (!CreateProcessW(NULL, cmdline.ptr(), NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) return PyErr_SetFromWindowsErr(0);
if (wait_for) WaitForInputIdle(pi.hProcess, wait_for);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
Py_RETURN_NONE;
}
// Icon loading {{{
#pragma pack( push )
#pragma pack( 2 )
@ -952,6 +968,7 @@ static const char winutil_doc[] = "Defines utility methods to interface with win
#define M(name, args) { #name, name, args, ""}
static PyMethodDef winutil_methods[] = {
M(run_cmdline, METH_VARARGS),
M(get_dll_directory, METH_NOARGS),
M(create_mutex, METH_VARARGS),
M(get_async_key_state, METH_VARARGS),
@ -1206,6 +1223,7 @@ CALIBRE_MODINIT_FUNC PyInit_winutil(void) {
PyModule_AddIntConstant(m, "DONT_RESOLVE_DLL_REFERENCES", DONT_RESOLVE_DLL_REFERENCES);
PyModule_AddIntConstant(m, "LOAD_LIBRARY_AS_DATAFILE", LOAD_LIBRARY_AS_DATAFILE);
PyModule_AddIntConstant(m, "LOAD_LIBRARY_AS_IMAGE_RESOURCE", LOAD_LIBRARY_AS_IMAGE_RESOURCE);
PyModule_AddIntConstant(m, "INFINITE", INFINITE);
return m;
}