From d103b29f96cf5952649352bbb36d4533e92d11f0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 9 Oct 2020 22:32:05 +0530 Subject: [PATCH] Function to get process times on windows --- src/calibre/utils/windows/winutil.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/calibre/utils/windows/winutil.cpp b/src/calibre/utils/windows/winutil.cpp index 64b590212d..fd0e2cfc52 100644 --- a/src/calibre/utils/windows/winutil.cpp +++ b/src/calibre/utils/windows/winutil.cpp @@ -8,6 +8,7 @@ #define UNICODE #include +#include #include #include #include @@ -636,6 +637,28 @@ get_long_path_name(PyObject *self, PyObject *args) { return ans; } +static PyObject * +get_process_times(PyObject *self, PyObject *pid) { + HANDLE h; + if (pid == Py_None) { + h = GetCurrentProcess(); + } else if (PyLong_Check(pid)) { + h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, PyLong_AsUnsignedLong(pid)); + if (h == NULL) return PyErr_SetFromWindowsErr(0); + } else { + PyErr_SetString(PyExc_TypeError, "process pid must be None or an integer"); + return NULL; + } + FILETIME creation, exit, kernel, user; + BOOL ok = GetProcessTimes(h, &creation, &exit, &kernel, &user); + int ec = GetLastError(); + CloseHandle(h); + if (!ok) return PyErr_SetFromWindowsErr(ec); +#define T(ft) ((unsigned long long)(ft.dwHighDateTime) << 32 | ft.dwLowDateTime) + return Py_BuildValue("KKKK", T(creation), T(exit), T(kernel), T(user)); +#undef T +} + // Boilerplate {{{ static const char winutil_doc[] = "Defines utility methods to interface with windows."; @@ -645,6 +668,7 @@ static PyMethodDef winutil_methods[] = { M(create_named_pipe, METH_VARARGS), M(set_handle_information, METH_VARARGS), M(get_long_path_name, METH_VARARGS), + M(get_process_times, METH_O), {"special_folder_path", winutil_folder_path, METH_VARARGS, "special_folder_path(csidl_id) -> path\n\n"