From 1516d709fb149bc0d586efc754a0bef083255398 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Apr 2021 14:06:14 +0530 Subject: [PATCH] Clean up wpd utils --- setup/extensions.json | 2 +- src/calibre/devices/mtp/windows/utils.cpp | 22 +++------------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/setup/extensions.json b/setup/extensions.json index 4900ff5b88..5e76a922d2 100644 --- a/setup/extensions.json +++ b/setup/extensions.json @@ -162,7 +162,7 @@ "name": "wpd", "only": "windows", "sources": "calibre/devices/mtp/windows/utils.cpp calibre/devices/mtp/windows/device_enumeration.cpp calibre/devices/mtp/windows/content_enumeration.cpp calibre/devices/mtp/windows/device.cpp calibre/devices/mtp/windows/wpd.cpp", - "headers": "calibre/devices/mtp/windows/global.h", + "headers": "calibre/devices/mtp/windows/global.h calibre/utils/windows/common.h", "libraries": "ole32 oleaut32 portabledeviceguids user32", "cflags": "/X" }, diff --git a/src/calibre/devices/mtp/windows/utils.cpp b/src/calibre/devices/mtp/windows/utils.cpp index 4e5b89f963..363425aca5 100644 --- a/src/calibre/devices/mtp/windows/utils.cpp +++ b/src/calibre/devices/mtp/windows/utils.cpp @@ -6,28 +6,12 @@ */ #include "global.h" +#include "../../../utils/windows/common.h" using namespace wpd; PyObject *wpd::hresult_set_exc(const char *msg, HRESULT hr) { - PyObject *o = NULL, *mess; - LPWSTR desc = NULL; - - FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&desc, 0, NULL); - if (desc == NULL) { - o = PyUnicode_FromString("No description available."); - } else { - o = PyUnicode_FromWideChar(desc, wcslen(desc)); - LocalFree(desc); - } - if (o == NULL) return PyErr_NoMemory(); - mess = PyUnicode_FromFormat("%s: hr=%lu facility=%u error_code=%u description: %U", msg, hr, HRESULT_FACILITY(hr), HRESULT_CODE(hr), o); - Py_XDECREF(o); - if (mess == NULL) return PyErr_NoMemory(); - PyErr_SetObject(WPDError, mess); - Py_DECREF(mess); - return NULL; + return error_from_hresult(hr, msg); } wchar_t *wpd::unicode_to_wchar(PyObject *o) { @@ -46,7 +30,7 @@ wchar_t *wpd::unicode_to_wchar(PyObject *o) { PyObject *wpd::wchar_to_unicode(const wchar_t *o) { PyObject *ans; if (o == NULL) return NULL; - ans = PyUnicode_FromWideChar(o, wcslen(o)); + ans = PyUnicode_FromWideChar(o, -1); if (ans == NULL) PyErr_NoMemory(); return ans; }